Jump to content

Silly Python3 Strings

straight_stewie
Go to solution Solved by Mr_KoKa,

No, input functions will print what they prompt first, before print function ends, when you run your print function with input function as argument, it will print only after input function returns so this is why you have your prompts printed before print function output.

 

More or less, print has to have all arguments available to print them, and input has to print to prompt for input. So you would kllike to have data before you ask for them. Your two line attempts is the best you can get.

I want the output to look something like this: 

Capture.PNG

 

Obviously I can do that by writing this:

print("ID Card Printing\n================\n\nPlease enter your personal information below\n")
print("%s %s %d" % (input("Please enter your first name: "), input("Please enter your last name: "), 2016-int(input("Please enter your four digit birthyear: "))))

# Sorry for the long line

 

I want to do it in one line though, instead of two. I've tried the following below, none of which work:

#Sorry for the long lines. That's what one-liners are all about

# One Liner attempt number 1
print("%s %s %s %d", % ("ID Card Printing\n================\n\nPlease enter your personal information below\n", input("Please enter your first name: "), input("Please enter your last name: "), 2016-int(input("Please enter your four digit birthyear: ")))

#One Line attempt number 2
print("{} {} {} {}" .format("ID Card Printing\n================\n\nPlease enter your personal information below\n", input("Please enter your first name: "), input("Please enter your last name: "), 2016-int(input("Please enter your four digit birthyear: "))))

These programs output this:
Capture0.PNG

 

Is there any way that I can get the program to output the correct output in the correct order in one line?

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

No, input functions will print what they prompt first, before print function ends, when you run your print function with input function as argument, it will print only after input function returns so this is why you have your prompts printed before print function output.

 

More or less, print has to have all arguments available to print them, and input has to print to prompt for input. So you would kllike to have data before you ask for them. Your two line attempts is the best you can get.

Link to comment
Share on other sites

Link to post
Share on other sites

print("\n----------------\n\nName: {last}, {first}\n\nAge: {age}\n\n----------------" .format(first=input("ID Card Printing\n================\n\nPlease enter your personal information below\n\n" + "Please enter your first name: "), last=input("Please enter your last name: "), age=2016-int(input("Please enter your four digit birthyear: "))))

Though that's horrible and you should never actually do that.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, fizzlesticks said:

Though that's horrible and you should never actually do that.

I'm well aware. I just enjoy trying to solve the early Intro-P assignments on one line. This one will have to be a two liner though.

 

6 minutes ago, Mr_KoKa said:

No, input functions will print what they prompt first, before print function ends, when you run your print function with input function as argument, it will print only after input function returns so this is why you have your prompts printed before print function output.

Well damn. Thank's for the input guys.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, straight_stewie said:

I'm well aware. I just enjoy trying to solve the early Intro-P assignments on one line. This one will have to be a two liner though.

 

Well damn. Thank's for the input guys.

Is there something wrong with the 1 liner I posted?

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

OP might not noticed the difference (the first line is included into first input) as it is (as you mentioned) quite not readable :P

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Mr_KoKa said:

OP might not noticed the difference (the first line is included into first input) as it is (as you mentioned) quite not readable :P

Actually, OP was in a rush to get to class and mistakenly thought that @fizzlesticks code was a quote.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×