私はコーディングの初心者だと言って始めます。私は学ぶためにオンラインコースで働いています。残念ながら私は回避できない問題に挑戦しました。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)
だから、明確化:
私は希望の何かがある:
ユーザ入力「ショーデータ」や「価格のPPは、」それは飛ばしてください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 and answer != cl2:
ユーザ入力CL1またはCL2かの作品が、私はそれがユーザーの種類ならばコマンドを仕事をしたいの代わり:
IVEは、以下の
while answer != "cl1" or answer != "cl2":
while answer != cl1 or answer != cl2:
while answer != "cl1" and answer != "cl2":
while answer != cl1 and answer != cl2:
のすべてのバリエーションを試してみました変数名。
私の問題を説明してくれることを願っています。
簡単な質問: 'line2/line3'の意図する結果は何ですか?これらの2つの値は文字列なので分割できません。 – Neelik
これは別のコマンドになります。それはちょうど1つ以上のコマンドの例としてATMです。 – Gromit