0

Ill start off by saying im very much a beginner in coding and im working with an online course to learn. Unfortunately ive come up against an issue which i cant seem to find a work around.

im trying to read a line from a file and then assign it to a variable. im then asking for input from the user, and then (within a while loop) if that users input matches the variable to open up another file and follow the action.

here is my code currently:

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_line1 = commands.readline()
# command_line2 = price PP
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)

So all of this code is fine up untill "What would you like to do " command, it asks for input but it user inputs cl1 or cl2 (or anything in fact) it just runs the while loop and asks the question again.

What i would like it to do is if user input either command as written in the command file (show data or price pp) to skip the first loop and start the second nested loop.

I hope that all made sense, any suggestion?

Thanks in advance.

Marc

edit: this has nothing to do with comparing variables, i think edit: added while answer != "cl1" or answer != "cl2": code still outputs: What would you like to do? show data Im sorry, there is no such command What would you like to do? cl1 Im sorry, there is no such command What would you like to do?

Gromit
  • 77
  • 8
  • Im still getting the same issue. whether the input to the answer is cl1, cl2 or show data, price pp (which it should respond to) it treats the loop as true and just asks the question again. – Gromit Feb 23 '17 at 23:58
  • output is still "What would you like to do? show data Im sorry, there is no such command" – Gromit Feb 24 '17 at 00:02
  • Okay, sorry. Do you want to print `"Im sorry, there is no such command"` only if `answer` is not equal to `"cl1"` or `"cl2"`? Then you should use `answer != "cl1" and answer != "cl2"` or `answer not in {"cl1", "cl2"}`. – vaultah Feb 24 '17 at 11:11
  • Yes that is correct and how it is currently setup, if the user input's "show data" or "price pp" (which has been assigned the variable (cl1 & cl2) it's suppose to skip the "no such command" part of the loop and go to the else part. But currently it doesn't, ATM whatever the user inputs even if it is the right command or anything else it just treats the statement as true and prints "no such command" line. And I don't understand why, would it have anything to do with assigning variables from .readlinr commands maybe ? – Gromit Feb 24 '17 at 18:46

0 Answers0