What have I done wrong? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: What have I done wrong? (/thread-21284.html) |
What have I done wrong? - Gandalf - Sep-23-2019 So I am new to learning Phyton and I don't know what I did wrong here, I would appreciate it if you could help me and also if you could simplify your explanation a bit. This is the code: birthyear = input(' in what year were you born? ') age = 2019 - int(birthyear) print(' you are ' + age + ' years old. ')And this is the error I get:
RE: What have I done wrong? - micseydel - Sep-23-2019 The problem is that the + operator can't be used between a string (' you are ') and an int, such as age. So you need to either convert the int to a string before using the plus operator, like this: print(' you are ' + str(age) + ' years old. ')or better yet, use string formatting print(' you are {} years old. '.format(age))or if you're using the latest version of Python, you can even do this print(f' you are {age} years old. ') RE: What have I done wrong? - Gandalf - Sep-23-2019 Thanks for your help, and what does the f stand for on your last example? RE: What have I done wrong? - perfringo - Sep-23-2019 (Sep-23-2019, 02:07 AM)Gandalf Wrote: Thanks for your help, and what does the f stand for on your last example? f stands for 'formatted (string literal)'. It's available from Python 3.6 and you can read about in PEP 498 -- Literal String Interpolation RE: What have I done wrong? - Gandalf - Sep-23-2019 Got it thanks again for your help. |