Why does this code not work? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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
19 ответов
+ 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