我上课时已经遇到了这个实验课的问题。我想确保我的输入是指定的枚举,而不是任何其他输入。但由于某种原因,我似乎无法让它正常工作,即使我输入错误的答案它只是在循环中运行,直到我输入正确的答案。我没有经验知道我没有看到什么。
questions = [
{'question': '\nOverall what is your response?',
'answers': ['Calmly look around?',
'Panic!!',
'Roll over and cry?'],
'correct': '1'},
{'question': '\nYou notice a door. Do you try it?',
'answers': ['Yes',
'No'],
'correct': '1'},
{'question': '\nYou smell something strange in the corner. What do you do?',
'answers': ['Investigate?',
'Do you poke it?',
'Leave it alone?',
'Vomit'],
'correct':'1'},
{'question': '\nA light flickerss above you. What do you do?',
'answers': ['Break it!!',
'Tighten it.',
'Leave it alone.',
'Roll over'],
'correct': '2'},
{'question': '\nThe bed you woke up in seems strange..',
'answers': ['Roll it over.',
'Roll over and cry.',
'Go to bed.'],
'correct': '1'},
{'question': '\nIn one corner a is playing Tiny Tim. What do you do?',
'answers': ['Turn it off',
'Mess with the dials',
'Sing along',
'Watch the show, continuously'],
'correct': '1'},
{'question': '\nThe door knob creeks',
'answers': ['Jump through the roof',
'Run to it',
'Wait calmly',
'Yell!'],
'correct': '3'},
{'question': '\nYou notice a cellar door in the corner',
'answers': ['Try to open it',
'Cautiously approach it',
'Forcfully open',
'Yell at it'],
'correct': '1'},
{'question': '\nA loud speaker is on the ceiling, starts playing Tiny Tim',
'answers': ['Sing along',
'Panic even more',
'Embrace the Tiny Tim',
'Try and destroy the loud speaker'],
'correct': '4'},
{'question': '\nYou see a toilet of needeles. What do you do?',
'answers': ['Reach your arm into it.',
'Question the needles in the toilet.',
'Check the tank.',
'Roll over.'],
'correct': '3'}
]
score = 0
for question in questions:
print(question['question'])
for i, choice in enumerate(question['answers']):
print(str(i + 1) + '. ' + choice)
answer = ''
while answer not in range(1, len(question['answers'])):
answer = input('Choose a numerical answer: ')
if answer == question['correct']:
score = score
break
elif answer in question['answers']:
break
if answer == question['correct']:
score = score + 1
else:
print('That\'s one way to try it...')
答案 0 :(得分:0)
在我看来,您正在将字符串答案与范围生成的整数列表进行比较。
尝试将整数列表转换为字符串列表。
while answer not in [str(x) for x in range(1, len(question['answers']))]:
答案 1 :(得分:0)
这应该有所帮助。
score = 0
for question in questions:
print(question['question'])
for i, choice in enumerate(question['answers'], 1): #enumerate start from 1
print(str(i) + '. ' + choice)
c = map(int, range(1, len(question['answers'])+1)) #Convert your choice list to int
answer = input('Choose a numerical answer: ')
while answer not in c: #Check if input in available choice list.
answer = input('Choose a numerical answer: ')
if answer == question['correct']:
score = score + 1
else:
print('That\'s one way to try it...')
答案 2 :(得分:0)
我发现将范围转换为字符串然后再加上长度就完美了。这是我在这里收到的两个答案所提供的。
while answer not in [str(x)
for x in(range(1, len(question['answers'])+1))]:
answer = input('Choose a numerical answer: ')
if answer == question['correct']:
score = score
break
if answer == question['correct']:
score = score + 1