Python 程序出错

Error at Python program

我的 python 程序有点问题。这是一个非常简单的程序,您可以输入 "stats" 和 returns "Weather: "、"Day: "、"Temperature: " 或者您可以输入命令 "day" 设置一天,"weather" 设置天气,所以您可以在 "stats" 看到。在每个命令的末尾,"command input" 应该再次出现。当您输入第一个命令时,该命令成功出现并且 "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: ");

您似乎漏掉了一个循环。尝试以下方式:

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

这应该能满足您的需求。有关循环的更多信息,请参阅 here

你的错误:

1) 在 Python 中你不需要使用 ; 来结束语句。

2) 使用while循环继续循环

3) 如果键入“quit”,while 循环将退出(您可以将 "quit" 替换为您想要的任何其他内容)。

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\".")