我开始说我非常喜欢编码和使用在线课程学习的初学者。不幸的是,我遇到了一个我似乎无法找到解决方法的问题。
我试图从文件中读取一行,然后将其分配给变量。然后我要求用户输入,然后(在一个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)
澄清:
如果用户输入""显示数据"或者"价格pp" (已经分配了变量(cl1& cl2)它假设跳过"没有这样的命令"循环的一部分并转到else部分。但是目前它没有' t,ATM无论用户输入什么,即使它是正确的命令或其他任何东西,它只是将声明视为真实并打印"没有这样的命令"线。我不明白为什么,它会不会与从.readline命令分配变量有什么关系?