0

I am trying to make a program which allows you to enter names and ages and then prints those values.

I have this code:

print("This is a program to create and edit lists of a maximum of 10 people ")
n1 = input("Enter the name of the first person here: ")
a1 = int(input("Enter the age here: "))
n2 = input("Enter the name of the second person here: ")
a2 = int(input("Enter the age here: "))
n3 = input("Enter the name of the third person here: ")
a3 = int(input("Enter the age here: "))
n4 = input("Enter the name of the fourth person here: ")
a4 = int(input("Enter the age here: "))
n5 = input("Enter the name of the fifth person here: ")
a5 = int(input("Enter the age here: "))

print("Forming your list: ")

p1 = (n1 , " your age is ", a1 + "years old")
p2 = (n2 , " your age is ", a2 + "years old")
p3 = (n3 , " your age is ", a3 + "years old")
p4 = (n4 , " your age is ", a4 + "years old")
p5 = (n5 , " your age is ", a5 + "years old")
print(p1,p2,p3,p4,p5)


ans = input("Would you like to keep adding people? ")

if ans == "yes" :
    n6 = input("Enter the name of the sixth person here: ")
    a6 = int(input("Enter the age here: "))
    n7 = input("Enter the name of the seventh person here: ")
    a7 = int(input("Enter the age here: "))
    n8 = input("Enter the name of the eighth person here: ")
    a8 = int(input("Enter the age here: "))
    n9 = input("Enter the name of the ninth person here: ")
    a9 = int(input("Enter the age here: "))
    n10= input("Enter the name of the tenth person here: ")
    a10: int= int(input("Enter the age here: "))

print("Forming your list: ")

p1 = (n1 , " your age is " , a1 + "year s old")
p2 = (n2 , " your age is " , a2 + "years old")
p3 = (n3 , " your age is " , a3 + "years old")
p4 = (n4 , " your age is " , a4 + "years old")
p5 = (n5 , " your age is " , a5 + "years old")
p6 = (n6 , " your age is " , a6 + "years old")
p7 = (n7 , " your age is " , a7 + "years old")
p8 = (n8 , " your age is " , a8 + "years old")
p9 = (n9 , " your age is " , a9 + "years old")
p10= (n10 , " your age is " , a10 + "years old")

print(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
if ans == "no" :
    print("Thank you for using this service and hope you enjoyed :) ")

I get an error message that says:

 File "C:\Users\user1\Documents\Code\Python\HelloWorld\List.py", line 15, in <module>
    p1 = (n1 , " your age is ", a1 + "years old")
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Why? What is wrong with the code, and how can I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
rSyphex
  • 9
  • 1
  • You can't add strings and numbers using `+`. Convert the number to a string with `str(...)` – Silvio Mayolo Sep 20 '22 at 15:25
  • better yet, use [f-strings](https://realpython.com/python-f-strings/), then you don't need to use `str(...)` too much. – rv.kvetch Sep 20 '22 at 15:26
  • Welcome to Stack Overflow. " I just could not figure out what was wrong with my code even though I use PyCharm and it tells me what my mistake is. Here is the error it shows me:" Well, let's start by **reading it**. "TypeError: unsupported operand type(s) for +: 'int' and 'str'" - what do you think this **means**? notice how it says there is a problem that has to do with the `+`. Do you see a `+` on the line of code it's talking about? What's on either side of the `+`? Does it make sense to add those two things together? (Hint: what does `int` mean? What does `str` mean?) – Karl Knechtel Sep 20 '22 at 15:27
  • For future reference: please read [ask] and note well that this is **not a discussion forum**. We are **not interested** in your level of experience, or your struggles as a programmer; we are interested in **the question**; and we [do not need or want](https://meta.stackoverflow.com/questions/288160) your thanks - it is already assumed that everyone here is trying to answer the question, once it is properly asked. I [edit]ed the question to show how questions should look - but also, the code example should be trimmed to a [mre] (please read that link and carefully study the advice). – Karl Knechtel Sep 20 '22 at 15:31
  • There are a few other problems in the code here. I **strongly** advise you to try to write much less code at a time, and **make sure it works** before moving forward. For example, try to make the program input *one* name and *one* age. Then learn to use *functions* to organize the code. – Karl Knechtel Sep 20 '22 at 15:35
  • Yes Karl I will definitely do that and I appreciate your help! – rSyphex Sep 20 '22 at 15:51

0 Answers0