Rafael Borges Rafael Borges - 3 days ago 5
Python Question

Error at Python program

I'm having a little trouble at my python program. It's a really simple program that you can input commands like "stats" and it returns "Weather: ", "Day: ", "Temperature: " or you can enter the command "day" to set a day, "weather" to set a weather, so you can see at "stats". On the end of every command the "command input" should appear again. When you enter the first command the command appears successfuly and the "command input" appears again (like what i want), but when you enter another command again it simples print what you just type, the command does not execute and python compiler closes.

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: ");

Answer

Your mistakes:

1) in Python you dont need to use ; to end a statement.

2) use a while loop to continue looping

3) while loop will quit if pressed "quit" (you can replace "quit" with anything else you desire).

4) Also there was a typo.

5) You do not need to write input()so many times if it's looping in while loop.

Hope this helps:

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