我一直在尝试让我的代码对列表进行重复,如果将其留空则可以工作,但如果输入在中断中大于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
https://stackoverflow.com/questions/56469068
复制相似问题