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.

Daniil Rose
  • 123
  • 1
  • 9
  • where is the loop? There is no `while` or `for` in your code – Walter Tross Sep 07 '19 at 20:24
  • Well, that's what I don't know. Wasn't sure the best way to do that. – Daniil Rose Sep 07 '19 at 20:26
  • Possible duplicate of [Python: How to use user input to close or continue a while loop](https://stackoverflow.com/questions/44751720/python-how-to-use-user-input-to-close-or-continue-a-while-loop) – Heath Raftery Sep 07 '19 at 20:35
  • It is, however, I feel the answer is better here. Sorry, worded the question wrong when I was searching. – Daniil Rose Sep 07 '19 at 20:39
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – martineau Sep 07 '19 at 21:13

2 Answers2

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 ")
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • 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? – Daniil Rose Sep 07 '19 at 20:28
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
elmo26
  • 150
  • 8