patternpythonMinor
Simple age converter
Viewed 0 times
simpleconverterage
Problem
I want you to pick my code apart and give me some feedback on how I could make it better or more simple. In my opinion this is the best code I have written, being that I'm new to programming. The program is a basic "converter" that has the user input his/her age and it tells how many days, hours, and minutes the user has been alive. Let me know what you think!
NOTE: If you make a change, please explain to me why and how it works in the most simple way you can, so I understand it better.
import time
import sys
def name():
name = input("What is your name? ")
while name.isdigit():
print("Make sure to enter an actual name.")
name = input("What is your name? ")
return name
def user_input():
while True:
age = input("What is your age? ")
try:
return int(age)
break
except ValueError:
try:
return float(age)
break
except ValueError:
print("Make sure to enter a real age.")
def again():
while True:
again = input("Play again? Yes/No ").lower()
if again == "no":
sys.exit(0)
elif again == "yes":
break
else:
print("That was not an option.")
while True:
the_name = name()
age = user_input()
Days = age * 365
Hours = Days * 24
Minutes = Hours * 60
print("Ok {}, I have your results.".format(the_name))
time.sleep(2)
print("If you are {} years of age....".format(age))
time.sleep(2)
print("You are {} days old.".format(Days))
time.sleep(2)
print("You are {} hours old.".format(Hours))
time.sleep(2)
print("You are {} minutes old!".format(Minutes))
again()NOTE: If you make a change, please explain to me why and how it works in the most simple way you can, so I understand it better.
Solution
This:
means that "1" is not a real name, but "a" and "17xy" are. I think it might be better to use some kind of a regular expression, or - better - just accept whatever nonempty string the user chooses to give you as his/her name.
In your
Also, in the same function, your
I think it'd be a much better program if the input was date of birth. Do you know your current age, as anything more precise than "between x and x+1 years old"?
def name():
name = input("What is your name? ")
while name.isdigit():
print("Make sure to enter an actual name.")
name = input("What is your name? ")
return namemeans that "1" is not a real name, but "a" and "17xy" are. I think it might be better to use some kind of a regular expression, or - better - just accept whatever nonempty string the user chooses to give you as his/her name.
In your
user_input(), you try to convert the input to int, and - if that fails - to float. Why not straight to float? I don't see how is 17 better than 17.0.Also, in the same function, your
break statements will never execute, as the return statement terminates the function.I think it'd be a much better program if the input was date of birth. Do you know your current age, as anything more precise than "between x and x+1 years old"?
Code Snippets
def name():
name = input("What is your name? ")
while name.isdigit():
print("Make sure to enter an actual name.")
name = input("What is your name? ")
return nameContext
StackExchange Code Review Q#31328, answer score: 6
Revisions (0)
No revisions yet.