Python Forum
New to Coding, help please?!
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to Coding, help please?!
#1
Write a python program that prompts the user to enter in their age. Convert the keyboard input into an integer using the int( ) function and store the result into a variable. Don't worry about your program crashing on invalid input.
Use an if-elif chain (chained conditionals) to test the user age against a series of ranges. The program should print a message classifying the user's age. Use the following age ranges to determine what message to print.

0 - Baby
1, 2 = Toddler
3,4 = Infant
5, 12 = Child

Enter your age:  7
You are a child of age 7.
You have 6 more years until your are a teenager!


Please help me with my coding. I know how to do the Toddler, Infant, and Child coding. Also, how can I get it to tell how many years until they move up to the next age bracket?
age = int(input("Enter your age:)
if age > 0 and age <1:
print("You are a Baby")
Moderator sparkz_alot:
Moved from General Coding, add code tags.  In the future use the proper code tags, post in the correct section and title your thread to reflect your question. 

Refer to the HELP Document.
Reply
#2
something like this will suffice, I think. If I understood the question.
If you are using python 2.xxx,... user raw_input instead. if using python 3.xxxx "input is fine"

since I am using 2.xx , I used raw_input... since the question asked to store it in variable, I chose to use int within the variable.
in the future, if multiple user input need to be store, you will be using .split() , but you do not need it here.
ALSO, if you are curious about keeping the code functioning prompting the user to enter correct number again, if the user entered out of bound numbers, you can add a loop (just a tip)

here is a functioning code, that does not necessarily answer your question completely, but gives you a general idea of the if and elif.



user_input = (raw_input("Enter your Age"))
age = int(user_input)   # the storing part, but I prefer combining int on the first line
if age == 0:
 print "You are a Baby!" 
elif age == 1 or age == 2:
 print "You are a toddler" 
elif age == 3 or age == 4:
 print "You are Infant"
elif age >= 5 and age <= 12:
 print "You are a Child"
else :
 print "Enter a whole number between 0 and 12"
Reply
#3
age = int(input("Enter your age: "))

if age  <= -1:
    print ("You are time traveler of age", age)
elif age >= 0 and age <= 1:
    print("You are a baby of age",age)
elif age < 3:
    print("You are a toddler of age", age)
elif age < 5:
    print("You are an infant of age", age)


"You have 6 more years until your are a teenager!" How do I figure this part out? I need my program to tell me how many years it will take to move up to the next age group.
Reply
#4
I am assuming teenage start at 13, right?
do basic arithmetic

teenage_left = 13-x, where x is the user input age
if you want adult
adult_left = 18-x, where x is the user input age

FYI: A lot of coding will involve basic math, so try to
here is an example
user_input = (raw_input("Enter your Age"))
age = int(user_input)
teenage_left = 13-age
if age == 0:
  print "You are a Baby!"
  print "You have", teenage_left, " more years until you are teenager"
elif age == 1 or age == 2:
  print "You are a toddler"
  print "You have", teenage_left, " more years until you are teenager"
elif age == 3 or age == 4:
  print "You are Infant"
  print "You have", teenage_left, " more years until you are teenager"
elif age >= 5 and age <= 12:
  print "You are a Child"
  print "You have", teenage_left, " more years until you are teenager"
else :
  print "Enter a whole number between 0 and 12"
OR ,

you can make it one line at the end, depends on your preference, but I prefer this one, cleaner!

user_input = (raw_input("Enter your Age"))
age = int(user_input)
teenage_left = 13-age
if age == 0:
  print "You are a Baby!"
elif age == 1 or age == 2:
  print "You are a toddler"
elif age == 3 or age == 4:
  print "You are Infant"
elif age >= 5 and age <= 12:
  print "You are a Child"
else :
  print "Enter a whole number between 0 and 12"
print "You have", teenage_left, " more years until you are teenager"
Reply
#5
Wow, I totally spaced it on the math portion. Thanks for much for your help!

Also, how do you post your python code in the box?
Reply
#6
just remember, the second code will give a negative number if the user entered a larger number than 13. If you want to use the second code, tweak it, to prevent negative number (hint: if statement, or make a function). the first code will not give you negative  number.

hover your mouse over the symbols and you will see "insert python tage"

Python in a bracket at the beginning of the code, and then /python inside a bracket at the end. similar to html markings
Reply
#7
age == 1 or age == 2:
There are 2 possible shorter versions
age in (1,2)
or
1 <= age <= 2
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020