3

プログラムのポイントは、ユーザーの名前を尋ねることです(最初の文字を自動的に大文字にします)。

次に、年齢と性別を尋ねます。年齢が130歳以上またはマイナスの場合、エラーがスローされます

プログラムはすべての情報を出力することになっていますが、whileループの状態がわかりません。誰かがwhileループの状態を理解するのを手伝ってもらえますか?

-編集-Pastebinのリンクは編集されましたが、そこには重要な情報があると思います。だから、私はまだあなたにリンクを与えるでしょう: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, ".")
4

2 に答える 2

1

有効な入力があった場合にオフ/オンに設定されるフラグを持つことができます。whileそれはあなたのループの問題を解決します

 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, ".")
于 2012-10-28T01:21:28.980 に答える
0

通常はここで を使用します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

これにより、受け入れ可能な回答が得られるまで質問が繰り返されます。回答が得られた場合、breakはループから抜け出します。

完全を期すために、彼らが何かを入力したこと、および数字を入力したことも確認する必要があります。ここでも を使用しますcontinue。これは、ループを最初から再開し、残りのコードを無視します。これは良い例になります:

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
于 2012-10-28T04:14:58.963 に答える