2016-11-30 5 views
0

Python 프로그램에 약간의 문제가 있습니다. 그것은 "stats"와 같은 명령을 입력 할 수 있고 "Weather :", "Day :", "Temperature :"를 반환하거나 "day"명령을 입력하여 하루를 설정하고 "weather"를 설정하는 정말 간단한 프로그램입니다 날씨, 그래서 당신은 "통계"에서 볼 수 있습니다. 모든 명령의 끝에서 "명령 입력"이 다시 나타나야합니다. 첫 번째 명령을 입력하면 명령이 successfuly로 나타나고 "명령 입력"이 다시 나타납니다 (다른 명령을 다시 입력하면 입력 한 내용이 인쇄되고 명령이 실행되지 않고 파이썬 컴파일러가 닫힙니다).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) 파이썬에서 당신이 그나마은 성명을 종료 ;를 사용해야합니다.

2) 당신은 아무것도 당신이 욕망)와 "종료"대체 할 수 ("quit"를 입력 한 경우

3) while 루프가 종료됩니다 루프 계속 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을 참조하십시오.