0

To clarify, this is similar to another question but I feel the answer is easier to understand.

I am making a very simple program for saying what year you will turn "x" years old. (It's a practice from Practice Python... Starting to relearn Python after a while) I would like the program to ask the user what age they want to know, and it does so. This works fine, but I do not remember how to have it keep asking until they write "n" and otherwise keep asking. Any ideas?

Thanks for the help! Code Below:

I've tried using a Java-esque loop, but this isn't Java, and I don't know what I'm doing. Up to any ideas.

# Libraries
import time

# Initial Code
name = input("What's your name? ")
print("Thank you " + name + "!")
age = int(input("How old are you? "))
year = int(input("Now, just for clarification, what year is it? "))
new_age = input("Now enter what age you would like to know! ")
print("Thank you! Now, I'll tell you the year you will turn " +new_age+ "!")
time.sleep(3)
print("Great, that calculation will only take a second or so!")
time.sleep(1.5)
math_year = year - age
answer = int(math_year) + int(new_age)
print(name + ", you will turn " + str(new_age) + " years old in " + str(answer) +"!")
time.sleep(3)

# Loop Code
again = input("Would you like to know another age? Y/n ")
if again == 'Y':
    new_age = input("Awesome! What age would you like to know? ")
    print("Great, that calculation will only take a second or so!")
    time.sleep(1.5)
    math_year = year - age
    answer = int(math_year) + int(new_age)
    print(name + ", you will turn " + str(new_age) + " years old in " + str(answer) +"!")

All results work, it just can't loop after the second time.

5

2 Answers 2

0

You can use while instead of if. Whereas if executes its block of code once, while will execute it again and again until the condition becomes false.

# Loop Code
again = 'Y'
while again == 'Y':
    new_age = input("Awesome! What age would you like to know? ")
    print("Great, that calculation will only take a second or so!")
    time.sleep(1.5)
    math_year = year - age
    answer = int(math_year) + int(new_age)
    print(name + ", you will turn " + str(new_age) + " years old in " + str(answer) +"!")
    again = input("Would you like to know another age? Y/n ")

As of python 3.8 (so, sometime after late October this year), you'll be able to use the assignment operator := to take care of those first two lines at once:

# Loop Code
while (again := 'Y'):
    ...
    again = input("Would you like to know another age? Y/n ")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works fine! I just wasn't sure what to do, sorry for the dumb question! I'll accept as soon as I can. That'll be cool once it comes out. That's similar to other languages, isn't it?
0

hello try something like

while True:
    """
    your code
    """
    answer = input("again"?)
    if answer == 'Y':
        continue
    elif answer == 'N':
        break
    else:
        # handle other input as you want to
        pass

1 Comment

That works as well, however, the other answer was easier to follow. Thank you anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.