我的python程序有点麻烦。这是一个非常简单的程序,你可以输入命令,如" stats"它返回"天气:","日:","温度:"或者你可以输入命令" day"设定一天,"天气"设置天气,这样你就可以看到" stats"。在每个命令的末尾,"命令输入"应该再次出现。当您输入第一个命令时,命令会成功显示,并且"命令输入"再次出现(就像我想要的那样),但是当你再次输入另一个命令时,它会简单地打印出你刚输入的内容,命令不会执行而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 :(得分:1)
你的错误:
1)在Python中,您不需要使用;
来结束语句。
2)使用while循环继续循环
3)while
循环将退出,如果键入“quit
”(您可以用您想要的任何其他内容替换“退出”)。
4)还有一个错字。
5)如果在循环中循环,你不需要多次写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\".")
答案 1 :(得分:0)
你似乎错过了一个循环。尝试以下方面:
temp = "";
wea = "";
day = "";
input1 = input("What do you want: ");
while not input1 == "exit":
if input1 == "stats":
...
...
这应该会给你你想要的东西。有关循环的更多信息,请参阅here。