我在使用python循环创建测验时遇到问题

时间:2017-06-24 03:36:10

标签: python python-3.x function loops while-loop

我的代码的开头工作正常但是,得分和答案的底部部分没有。我正在做一个关于这个的学校项目,需要快速解决方案。但我必须使用循环!我试图告诉用户答案是正确的部分不起作用。我揭示正确答案的部分也不能正常运作。分数也不合适。

print ('WELCOME TO THE MULTIPLE CHOICE TEST\n')
name = input('WHAT IS YOUR NAME? ')
print ('\nHI THERE ' + name + '! LET''S PLAY A GAME!\n')
print ('I will ask you 10 questions and give you three choices for each question.\n\nYou select which choice is the correct answer. Eg. A, B,C or D\n')

score = 0
score = int(score) 

qn1 = ["What's color of sky?", "a)Red", "b)Blue", "c)White", "d)Black"]


def print_question(qn_num):
    print(qn_num[0])

    print(qn_num[1])
    print(qn_num[2])
    print(qn_num[3])
    print(qn_num[4])

print_question(qn1)

answer = input ()
answer =int(answer)
if answer == 2:
   print ("good work")
   score = score + 1

else:

    print ("better luck next time")

    score = score - 1

1 个答案:

答案 0 :(得分:1)

您可以在无限循环中提出答案,并显示分数

print ('WELCOME TO THE MULTIPLE CHOICE TEST\n')
name = input('WHAT IS YOUR NAME? ')
print ('\nHI THERE ' + name + '! LET''S PLAY A GAME!\n')
print ('I will ask you 10 questions and give you three choices for each question.\n\nYou select which choice is the correct answer. Eg. A, B,C or D\n')

score = 0
score = int(score) 

qn1 = ["What's color of sky?", "a)Red", "b)Blue", "c)White", "d)Black"]


def print_question(qn_num):
  for st in qn_num:
    print(st)

print_question(qn1)

allowed_answers = ["a", "b", "c", "d"]
wrong_answer = True
while(wrong_answer):
  answer = input ("select answer: ")

  if answer == 'b':
   print ("good work")
   score = score + 1
   wrong_answer = False
  elif answer not in allowed_answers:
    print ("Bad input, you have to chose between " + ",".join(allowed_answers) )
  else:
    print ("better luck next time")
    score = score - 1
  print("Your score is: " + str(score))