-2

i'm new to python and i'm trying to ask the user if they want to go again for my age calculator tool. Code:

while True:
    import datetime
    birth_year = int(input("What year were you born? "))
    current_year = datetime.datetime.now().year
    age = current_year - birth_year
    print(f"You are {age} years old now.")
    input()
    choice = input("Do you want to go again? (yes or no) ")
if "choice" == yes:
    print("enjoy")
elif "choice" == no:
    print ("Ok, quitting now")
    quit()
else:
    print("i'll assume that means yes")
User123
  • 476
  • 7
  • 22
lad12345
  • 13
  • 4

2 Answers2

2
import datetime
while True:
    birth_year = int(input("What year were you born? "))
    current_year = datetime.datetime.now().year
    age = current_year - birth_year
    print(f"You are {age} years old now.")
    choice = input("Do you want to go again? (yes or no) ")
    if choice == 'yes':
        print("enjoy")
    elif choice == 'no':
        print ("Ok, quitting now")
        break
    else:
        print("i'll assume that means yes")

You can just take everything to infinite loop and break it whenever the user writes 'no'.

User123
  • 476
  • 7
  • 22
0

Your error is that you called the variable choice with quotes around it, making it the string "choice". Instead you should check if the variable choice contains the string 'yes' or 'no'.
To do so we can use two operators, == or the is operator.
Here is an example:

import datetime
birth_year = int(input("What year were you born? "))
current_year = datetime.datetime.now().year
age = current_year - birth_year
print(f"You are {age} years old now.")
input()
choice = input("Do you want to go again? (yes or no) ")
if choice == 'yes': # Example using the `==` operator.
    print("enjoy")
elif choice is 'no': # Example using the `is` operator.
    print ("Ok, quitting now")
    exit() # This should be `exit` not `quit`.
else:
    print("i'll assume that means yes")
Raz Kissos
  • 295
  • 1
  • 7