【问题标题】:Python : How to make a quiz and check if the answer is correct? [closed]Python:如何进行测验并检查答案是否正确? [关闭]
【发布时间】:2017-02-16 18:33:35
【问题描述】:

我正在学习计算机科学课程,最近参加了一项考试,其中有一个我不知道答案的问题,我想知道该怎么做。

问题/问题是向用户提出 7 个问题。然后检查有多少是正确的,然后给出一个百分比。我熟悉输入、运算符和变量,但我不明白如何检查用户输入的答案是否正确以及如何计算正确的答案和不正确的答案

【问题讨论】:

  • 我们不会为你做作业。尝试自己做,并就代码中无法解决的特定问题寻求帮助。
  • if raw_input("what is 5+7=?") == "12": print "Correct-o-rama!" ...

标签: python input


【解决方案1】:

这样的东西(伪代码)怎么样?

qa = [
    ('Q1', 'A1'),
    ('Q2', 'A2'),
     ]
num_correct = 0

for q,a in qa:
    user_answer = raw_input(q) 
    if user_answer == a:
        num_correct += 1

print 'Total questions:', len(qa)
print 'Total correct:', num_correct  

你应该能够找出其余的。

【讨论】:

  • 为什么不将问题和答案映射到字典而不是使用元组?
  • 当然。除非你想保持秩序。
  • 好吧,如果您通过键得到每个答案,那将不是问题。
  • 我在考虑保留问题的顺序。什么都行。
  • 好吧,对于一个很重要的元组,但对于字典,这并不重要。如果按关键字搜索,则无需排序。
【解决方案2】:

我已经完成了,下面是我所做的代码:

q1=int(input("What Is 5+1 "))
if q1==6:
    print ("Correct")
    corr1=int(1)
else:
    print ("Wrong, The Answer Is: 6")
    corr1=int(0)

q2=int(input("What Is 6+9 "))
if q2==15:
    print ("Correct")
    corr2=int(1)
else:
    print ("Wrong, The Answer Is: 115")
    corr2=int(0)
        
q3=int(input("What Is 54+4 "))
if q3==58:
    print ("Correct")
    corr3=int(1)
else:
    print ("Wrong, The Answer Is: 58")
    corr3=int(0)

q4=int(input("What Is 43+9 "))
if q4==52:
    print ("Correct")
    corr4=int(1)
else:
    print ("Wrong, The Answer Is: 52")
    corr4=int(0)

q5=int(input("What Is 67+9 "))
if q5==76:
    print ("Correct")
    corr5=int(1)
else:
    print ("Wrong, The Answer Is: 76")
    corr5=int(0)

q6=int(input("What Is 64+14 "))
if q6==78:
    print ("Correct")
    corr6=int(1)
else:
    print ("Wrong, The Answer Is: 78")
    corr6=int(0)

q7=int(input("What Is 44+3 "))
if q7==47:
    print ("Correct")
    corr7=int(1)
else:
    print ("Wrong, The Answer Is: 47")
    corr7=int(0)

correct=corr1+corr2+corr3+corr4+corr5+corr6+corr7
print("You Scored %s/7" %(correct))

per=int(correct/7*100)

if per>=50:
    print("Congratulations You Passed With A %s Percent" % (per))
else:
    print("Oh No You Did Not Pass The Test And Scored A %s Percent, Try Harder Next Time!" %(per)) 

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2020-01-16
    • 1970-01-01
    • 2013-10-10
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    相关资源
    最近更新 更多