Id like to create a basic input and output in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Id like to create a basic input and output in Python

if the user inputs 25, it will print "you are 25 years old" if the user inputs 34, it will print "you are 34 years old" if the user inputs 45, it will print "you are 45 years old" can you give me a code to help me, please?

20th Dec 2016, 1:23 PM
Edwin Pratt
Edwin Pratt - avatar
6 Answers
+ 5
if input("input your age") == 25: print("you are 25")
20th Dec 2016, 1:30 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 5
age=input() print("you are " +age+ " years old") simple but works as long as an age is inserted
20th Dec 2016, 2:13 PM
Sophia
+ 4
Do the same for every choice or use loop
20th Dec 2016, 2:14 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
what if I have 3 choices?
20th Dec 2016, 2:06 PM
Edwin Pratt
Edwin Pratt - avatar
+ 3
in a loop and I recommend the format method answer = ”” while true: answer = input ("Enter Age : ") #quit by typing 'q' if answer == 'q': break try: age=int(answer) except ValueError: print("{} is not a valid number".format(answer)) else: print("You are {}".format(age)) sorry little thorough but that will make sure people only enter valid numbers and display the full number. if not it will warn them it's not a valid number. format is also very versatile and a great thing to get used to using. note have not tested the above but it should work.
21st Dec 2016, 10:10 AM
Elric Hindy
Elric Hindy - avatar
+ 1
not sure on what you meant by 3 choices. if they only can choose those three ages(which seems an odd choice) then you can do: if age in [25, 34, 45]: print age or if you only want to ask it the times then use a for loop. for i in range(3)
23rd Dec 2016, 1:14 AM
Elric Hindy
Elric Hindy - avatar