【发布时间】:2014-12-25 04:29:01
【问题描述】:
我花了两天时间试图弄清楚(可能是由于我的编码能力有限)Usgin Python 3.4.1
我正在尝试为输入设计一系列选项,并让选项“4”成为通行证,但前提是文件存在于特定位置。我可以随时按任何数字并重复。但是,如果我选择 4 并且文件不存在,它会返回不正确,但无论下一个答案如何,它都会继续我的程序。
print ("Please Choose From the Following Options")
print ("1. Option A")
print ("2. Option B")
print ("3. Option C")
print ("4. Option D")
print ("5. Option R")
monkeyGuess = input("Selection: ")
monkey = "4"
while monkey != monkeyGuess:
print ()
print ("Incorrect")
monkeyGuess = input("Selection: ")
while monkey == monkeyGuess:
try:
with open('c:\test.txt') as file:
break
pass
except IOError as e:
time.sleep(1)
print ()
print ("Incorrect")
monkeyGuess = input("Selection: ")
我厌倦了将两者结合起来,但收效甚微:
while monkey != monkeyGuess:
time.sleep(1)
print ()
print ("Incorrect Inputs Found")
monkeyGuess = input("Selection: ")
monkey == monkeyGuess or os.path.isfile('test.txt')
print ()
print ("Incorrect Inputs Found")
monkeyGuess = input("Selection: ")
【问题讨论】:
标签: python file loops while-loop