+ 1

Why does this code not work?

print('the year that you are in right now is, let me guess:' + int(input("Enter your age: ")) + int(input("Enter your birth year: ")))

1st Nov 2017, 9:32 AM
violett
20 Answers
+ 4
If you're ok with having your inputs be on the line below each of your input prompts then you can add the '\n' character to your input prompt strings. It won't look the same when you run this on your computer as it will in the code playground though. print('the year that you are in right now is, let me guess:' + str(int(input("Enter your age: \n")) + int(input("Enter your birth year: \n"))))
1st Nov 2017, 10:16 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Then you'll need to wrap each input in an int function and both of them together in 1 str() function. print('the year that you are in right now is, let me guess:' + str(int(input("Enter your age: ")) + int(input("Enter your birth year: "))))
1st Nov 2017, 9:55 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Input doesn't output a newline, so the next input is printed right after it and then the print statement is. your code would probably be better if you spread it out to get the result you want. age = int(input("Enter your age: ")) print() year = int(input("Enter your birth year: ")) print() print('the year that you are in right now is, let me guess:' + str(age + year))
1st Nov 2017, 10:03 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
I suggest you try running the code on your PC in the command line or Idle or in an IDE like PyCharm so that you can see the actual way the program would interact. The prompt for the input function is optional and can be ommitted if you wish.
1st Nov 2017, 10:47 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Instead of wrapping the inputs with the int() function and then wrapping those in a str() function you can just remove the int() function entirely. input() returns a str type as it is. No need for redundant multiple conversions. print('the year that you are in right now is, let me guess:' + input("Enter your age: ") + input("Enter your birth year: "))
1st Nov 2017, 9:50 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Ah I see, wasn't aware you wanted to add them together first. Sorry about that. Didn't read it very well. lol
1st Nov 2017, 9:53 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
@violet, you most likely got that error because you didn't input all your inputs at the prompt in the playground. You need to input both of them at the same time on a new line like: 43 1974 This is a limitation of the code playground.
1st Nov 2017, 10:06 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Personally, I would spread the code out. I'm a little OCD about the presentation to the end user and like my inputs to usually be at the end of the prompt on the same line separated by a space, but it's your code. lol
1st Nov 2017, 10:21 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
int(1000))+ int(1)) You don't need to wrap these int literals with the int() function. I think you want 100 not 1000 BTW. You can also skip out a pair of () parentheses if you wish. age= input("Enter your age:") birth_year= input("Enter your birth year:") print("You are alive in the " + str((int(age)+int(birth_year))//100 + 1) + "st" + " century!")
1st Nov 2017, 12:04 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Then you may have missed a parenthesis or something. It worked fine for me when I copied and pasted it into the playground.
1st Nov 2017, 10:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Yep, it's the parenthesis, sorry. Thanks a lot! One final thing: if I would want to get rid of the "Enter your age: Enter your birth year: " part of the output, are you advising me to go with separate codes as the one you have shown above?
1st Nov 2017, 10:12 AM
violett
0
Ok, but then it operates as a string, not an integer. So, for example, if I type 20 and 1997 as the input, I can't seem to get 2017. Instead, I get 201997. There is also another problem: why can I not directly get the number I want? Meaning, why do I also get the line Enter your age: Enter your birth year: the year that you are in right now is, let me guess:201997 as an output?
1st Nov 2017, 9:51 AM
violett
0
That made more sense at first, but nope, not working: File "..\Playground\", line 2 ^ SyntaxError: unexpected EOF while parsing
1st Nov 2017, 10:00 AM
violett
0
Alrighty, thanks! But is there no way of a compound code doing the same thing?
1st Nov 2017, 10:05 AM
violett
0
Nope, I've done that when it asked for input. That's not the problem.
1st Nov 2017, 10:06 AM
violett
0
Thinking about a typical questionnaire, the questions would be visible as to receive input, however they wouldn't be visible as a output. How do I make these two: Enter your age: Enter your birth year: lines completely invisible for the output? Is there something I don't understand about the concept? I'm very sorry for my stubbornness and the burden I cause you, but I want to comprehend whatever I'm learning to the fullest :| print("Thank you so much for putting up with me!")
1st Nov 2017, 10:29 AM
violett
0
Ok dok! Thank you!
1st Nov 2017, 10:41 AM
violett
0
Good idea! Thanks!
1st Nov 2017, 11:06 AM
violett
0
I have another code if you could help me out: age= input("Enter your age:") birth_year= input("Enter your birth year:") print("You are alive in the " + str(((int(age)+int(birth_year))//int(1000))+ int(1))+"st" + " century!")
1st Nov 2017, 11:48 AM
violett