2017-02-24 3 views
0

코딩에 초보자라는 말로 시작하고 배우려고 온라인 과정을 진행 중입니다. 불행히도 내가 해결할 수없는 문제에 대해 생각해 냈습니다.while 루프에서 사용자 입력 후 변수가 손실 됨 Python 3x

command.txt 파일에서 한 줄을 읽은 다음 변수에 할당하려고합니다. (cl1 & cl2) 그런 다음 사용자 입력이 다른 파일을 열고 작업을 수행하기 위해 변수와 일치하는 경우 사용자로부터 입력을 요청한 다음 (while 루프 내에서) 요청합니다. 그리고 만약 그렇지 않다면 다시 질문을하십시오.

user1 = open ("holiday.txt", "w") 
user1.write ("Holiday Location: " + holiday_loc + "\n") 
user1.write ("Total Price: £" + str(total_price) + "\n") 
user1.write ("Total People: " + str(total_people) + "\n") 
user1.close() 
print ("Here are a list of commands") 
commands = open ("commands.txt", "r") 
# command_line 1 = show data 
# command_line2 = price PP 
command_line1 = commands.readline() 
command_line2 = commands.readline() 
print (command_line1 + command_line2) 
commands.close() 
cl1 = command_line1 
cl2 = command_line2 
answer = input ("What would you like to do? ") 
while answer != "cl1" or answer != "cl2": 
    print("Im sorry, there is no such command") 
    answer = input("What would you like to do? ") 
else: 
    if answer == cl1: 
     show_data = open ("holiday.txt", "r") 
     line1 = show_data.readline() 
     line2 = show_data.readline() 
     line3 = show_data.readline() 
     print (line1 + "\n" + line2 + "\n" + line3) 
    elif answer == cl2: 
     print (line2/line3) 

그래서 명확한 설명 :

내가 뭘하고 싶은 것은 :

사용자 입력은 "데이터"또는 "가격 쪽"다음은 건너 뛰어야 여기

현재 내 코드입니다 to :

else: 
     if answer == cl1: 
      show_data = open ("holiday.txt", "r") 
      line1 = show_data.readline() 
      line2 = show_data.readline() 
      line3 = show_data.readline() 
      print (line1 + "\n" + line2 + "\n" + line3) 
     elif answer == cl2: 
      print (line2/line3) 

현재 사용자가 "show data"또는 "price pp"를 입력하면 루프 :

What would you like to do? show data Im sorry, there is no such command What would you like to do?

이 변수 할당이 어쩌면 길을 따라 길을 잃지 것을 나에게 보인다? 또는 그것의 끈과 섞는다. 확실하지 않습니다.

필자는 다음의 모든

while answer != "cl1" or answer != "cl2": while answer != cl1 or answer != cl2: while answer != "cl1" and answer != "cl2": while answer != cl1 and answer != cl2:

의 변화 시도 : 사용자 입력 CL1 나 CL2는하지만 난 그것이 작동하려면 while answer != cl1 and answer != cl2: 작품을 사용자 유형의 경우 명령 대신의 변수 이름.

내 문제를 설명하기를 바랍니다.

+0

'line2/line3'의 의도 된 결과는 무엇입니까? 이 두 값은 문자열이며 따라서 나눌 수 없습니다. – Neelik

+0

이것은 또 다른 명령이 될 것입니다. ATM은 단지 하나 이상의 명령의 예입니다. – Gromit

답변

0

commands.txt 파일에 "cl1"및 "cl2"문자열이 포함되어 있지 않으면 while answer != cl1 and answer != cl2: (cl1 및 cl2가없는 경우) 행이 작동해야합니다. commands.txt 파일에 무엇이 있습니까?

+0

두 개의 문자열 "show data"및 "price pp"는 사용자에게 입력을 요청하기 전에 cl1 및 cl2를 인쇄 할 경우 지정합니다 수정 한 다음 사용자 질문에서 선택하지 마십시오. – Gromit

관련 문제