2016-11-30 6 views
0

Pythonプログラムで少し問題があります。それは "統計"のようなコマンドを入力することができ、それは "天気:"、 "日:"、 "気温:"を返すか、または日を設定するために "日"を入力することができ、設定する "天気"天気ですので、あなたは "統計"で見ることができます。すべてのコマンドの最後に、「コマンド入力」が再び表示されます。最初のコマンドを入力すると、コマンドが正常に表示され、 "command input"が再び表示されます(別のコマンドを入力したとき)。別のコマンドを入力すると、入力したものが出力されます。Pythonプログラムでエラーが発生しました

temp = ""; 
wea = ""; 
day = ""; 
input1 = input("What do you want: "); 
if input1 == "stats": 
    print ("Day: " + day) 
    print ("Temperature: " + temp); 
    print ("Weather: " + wea); 
    input1 = input("What do you want: "); 
elif input1 == "day": 
    input2 = input("Set a day: "); 
    day=input2; 
    input1 = input("What do you want: "); 
elif input1 == "weather": 
    input3 = input("Set the weather: "); 
    wea=input3; 
    input1 = input("What do you want: "); 
elif input1 == "temperature": 
    input4 = input("Set the temperature: "); 
    temp=input4; 
    input1 = input("What do you want: "); 
elif input1 == "commands": 
    print ("Commands: "); 
    print ("day"); 
    print ("weather"); 
    print ("temperature"); 
    input1 = input("What do you want: "); 
else: 
    print ("Unknow Command! Try the commmand \"commands\"."); 
    input1 = input("What do you want: "); 
+0

は、コードの最初の行を再実行します何の制御構造がありません。ループを調べてください。これは 'while'ループのように見えます。 –

+1

コードをループに入れて繰り返す必要があるようです。例えば。 while(input1!= "exit"): ... –

答えて

1

あなたの過ち:

1)Pythonであなたがいけないの文を終了する;を使用する必要があります。

2)は、「quit」(あなたがを望む何か他のもので、「終了」置き換えることができます)と入力した場合whileループが終了します)

3をループし続けるために、whileループを使用します。

4)また、タイプミスがありました。

5)whileループでループする場合は、input()を何度も書く必要はありません。

希望はこのことができます:

temp = "" 
wea = "" 
day = "" 
while True: 
    input1 = input("What do you want: ","\n","Press (q) to quit.") 
    if input1 == "stats": 
     print("Day: " + day) 
     print("Temperature: " + temp) 
     print("Weather: " + wea) 
    elif input1 == "day": 
     input2 = input("Set a day: ") 
     day = input2 
    elif input1 == "weather": 
     input3 = input("Set the weather: ") 
     wea = input3 
    elif input1 == "temperature": 
     input4 = input("Set the temperature: ") 
     temp = input4 
    elif input1 == "commands": 
     print("Commands: ") 
     print("day") 
     print("weather") 
     print("temperature") 
     print("quit') 
    elif input1 == "quit": 
     exit() 
    else: 
     print("Unknown Command! Try the commmand \"commands\".") 
0

ループがありません。

temp = ""; 
wea = ""; 
day = ""; 
input1 = input("What do you want: "); 
while not input1 == "exit": 
    if input1 == "stats": 
    ... 
    ... 

これは、あなたが探しているものを与えるはずです。ループの詳細については、hereを参照してください。

関連する問題