我一直在尝试让我的代码重复执行一个列表,如果该列表为空,则可以正常工作,但是如果输入大于4,则我仍在尝试使python死机
我试图为此找到解决方案,但无法弄清楚应该去哪里
#Create a list for when they select the computer they want
ComputerList = ["(1)Home Basic $900", "(2)Office $1200", "(3)Gamer $1500", "(4)Studio $2200"]
#Ask them what they want and repeat if left blank
while True:
ComputerChoice = input("What computer would you like to buy?(Use the number beside it 1-4): ")
print("")
try:
ComputerChoice_int = int(ComputerChoice)
except ValueError:
print("You can not leave this blank '%s', try again" % (ComputerChoice,))
print("")
else:
break
我希望它会重复出现,但它带有
Traceback (most recent call last):
File "\\hnhs-fs01\StudentUsers$\17031\Documents\11 DTG\1.7\Assement\Assessment computer.py", line 69, in <module>
Computer = ComputerList[ComputerChoice_int -1]
IndexError: list index out of range
答案 0 :(得分:0)
出什么问题了?这段代码可以完美地工作。
#Create a list for when they select the computer they want
ComputerList = ["(1)Home Basic $900", "(2)Office $1200", "(3)Gamer $1500", "(4)Studio $2200", "test (5)"]
#Ask them what they want and repeat if left blank
while True:
print(ComputerList)
ComputerChoice = input("What computer would you like to buy?(Use the number beside it 1-%s): " % len(ComputerList))
try:
ComputerChoice_int = int(ComputerChoice) - 1
if not -1 < ComputerChoice_int < len(ComputerList):
raise ValueError
print("ok")
break
except ValueError:
print("Something went bad :/\n")
答案 1 :(得分:0)
您可以执行以下操作,使用int(input())
使一切变得更好+除了错误以外,您无需做任何事情。
ComputerList = ["(1)Home Basic $900", "(2)Office $1200", "(3)Gamer $1500", "(4)Studio $2200"]
while True:
ComputerChoice = int(input("What computer would you like to buy?(Use the number beside it 1-4): "))
if ComputerChoice not in range(1,5):
print("invalid input")
else:
print("You selected {}".format(ComputerList[ComputerChoice-1]))
# Do whatever here
答案 2 :(得分:0)
它崩溃是因为您没有子句来捕获大于4的输入。如果字段不可转换为整数,则只会引发错误。现在您可以对4 in进行硬编码,但是更可靠的方法是使用列表的长度。如果添加或删除计算机,这将使代码更易于更新。
在这里我会做什么:
ComputerList = ["(1)Home Basic $900", "(2)Office $1200", "(3)Gamer $1500", "(4)Studio $2200"]
max_integer = len(ComputerList)
status = False
while status == False:
try:
Choice = int(input("enter your choice"))
if 0 < Choice <= max_integer:
status = True
except ValueError:
print('input is invalid')
pass
这可以缩短,但是我把它画了一点,以便您了解发生了什么事情
答案 3 :(得分:0)
发生的事情是,您仅捕获ValueError(如字符串或空值)。数字4不是错误,因为4是整数。如果要使用ValueError和IndexError处理,则需要这样做:
#Create a list for when they select the computer they want
ComputerList = ["(1)Home Basic $900", "(2)Office $1200", "(3)Gamer $1500", "(4)Studio $2200"]
#Ask them what they want and repeat if left blank
while True:
ComputerChoice = input("What computer would you like to buy?(Use the number beside it 1-4): ")
print("")
try:
ComputerChoice_int = int(ComputerChoice)
Computer = ComputerList[ComputerChoice_int -1]
except Exception as e:
if e.__class__.__name__ == "ValueError":
print("You can not leave this blank '%s', try again" % (ComputerChoice,))
print("")
elif e.__class__.__name__ == "IndexError":
print("You can not input numbers bigger than {}, try again".format(len(ComputerList)))
print("")
else:
break