import sys
def main():
print("\t\tGame Menu\n")
print("\t\tA - enter your name\n\t\tB - play the game\n\t\tC - quit")
s = input("please enter your choice").upper()
valid_option = ['A','B','C']
while True:
if s in valid_option['A']:#here
n=input("please enter your name")
print("I welcome thee valient night sir", n)
print("\t\tGame Menu\n")
print("\t\tA - enter your name\n\t\tB - play the game\n\t\tC -
quit")
elif s in valid_option['B']:#here
print("the game is starting,")
print("\t\tGame Menu\n")
print("\t\tA - enter your name\n\t\tB - play the game\n\t\tC -
quit")
elif s in valid_option['C']:#and here
print("you have chosen to depart from our company, farewell
traveler")
sys.exit()
main()
当我尝试运行它时,它说TypeError:list indices必须是整数或切片,而不是str
请帮忙
答案 0 :(得分:0)
我对你用这条线试图达到的目标一点也不清楚:
if s in valid_option['A']:#here
也许这个会更适合你:
if s == 'A':
答案 1 :(得分:0)
valid_option
是list
。要在列表中查找值,必须使用索引表示法。
valid_option[0]
等于'A'
,valid_option[1]
等于'B'
,valid_option[2]
等于'C'
。
如果您想使用str
类型查找值,则valid_option
变量必须是词典(即{}
而不是[]
}),而不是列表。