user1780169
user1780169

Reputation: 31

Newbie stuck with easy "while" repetition in Python

The point of the program is to ask the user's name (automatically capitalizing the first letter).

It will then ask for age and gender. If age is over 130 or negative, it will throw an error

The program is supposed to print out all the information, but I can't figure out the while loop condition. Can anyone help me figure out the while loop condition?

-edit- Though the Pastebin's link was edited out, but I think there's important information there. So, I would still give you the link: http://pastebin.com/UBbXDGSt

name = input("What's your name? ").capitalize()
age = int(input("How old are you "))
gender = input("From what gender are you? ").capitalize()

while #I guess I should write something behind the "while" function. But what?
    if age >= 130:
        print("It's impossible that you're that old. Please try again!")
    elif age <= 0:
        print('''It should be logical that ages are written in positive numbers! Well, try again! =)''')

age = int(input("How old are you? "))   

print("Your name is ",name, ". You are ", age, "years old." "\nYou are ", gender, ".")

Upvotes: 3

Views: 466

Answers (2)

Lennart Regebro
Lennart Regebro

Reputation: 172219

Usually here you use while True.

while True:
    age = int(input("How old are you? "))

    if age >= 130:
        print("It's impossible that you're that old. Please try again!")
    elif age <= 0:
        print('''It should be logical that ages are written in positive numbers! Well, try again! =)''')
    else:
        break

This will repeat the question until it gets an acceptable answer, in which case the break will break out of the loop.

For completeness you should also check that they entered anything, and that they entered a number. Here I'll also use continue, which will restart the loop from the start, ignoring the rest of the code. This makes for a good example:

while True:
    age = input("How old are you? ")
    if not age:
        print("Please enter your age.")
        continue
    try:
        age = int(age)
    except ValueError:
        print("Please use numbers.")
        continue

    if age >= 130:
        print("It's impossible that you're that old. Please try again!")
    elif age <= 0:
        print('''It should be logical that ages are written in positive numbers! Well, try again! =)''')
    else:
        break

Upvotes: 0

Aniket Inge
Aniket Inge

Reputation: 25695

You can have a flag that's set off/on if there was a valid input. That will solve the problem of your while loop

 name = input("What's your name? ").capitalize()
 gender = input("From what gender are you? ").capitalize()
 ok = False #flag
 while not ok:
    age = int(input("How old are you "))
    if age >= 130:
       print("It's impossible that you're that old. Please try again!")
    elif age <= 0:
       print('''It should be logical that ages are written in positive numbers! Well, try again! =)''')
    else:
       ok = True
 print("Your name is ",name, ". You are ", age, "years old." "\nYou are ", gender, ".")

Upvotes: 1

Related Questions