如何在python中计算正确的答案?

时间:2015-02-13 03:05:30

标签: python count

我非常喜欢编程(例如,在intro类中)和python。我的任务是提示用户回答数学问题,计算正确/错误的数字,计算/显示他们的数字等级,并显示他们的字母等级。

一切似乎都很好......除了我无法弄清楚如何计算正确/错误答案的数量。有什么帮助吗?

def main():
    name = input("What is your name? ")
    correct = 0
    incorrect = 0
    ans = int(input("What is 4 + 5? "))
    val = add(4, 5)
    if(ans == val):
        correct + 1
    else:
        incorrect + 1
    ans2 = int(input("What is 20 * 6? "))
    val2 = mult(20, 6)
    if(ans2 == val2):
        correct + 1
    else:
        incorrect + 1
    ans3 = int(input("What is 14 - 10? "))
    val3 = sub(14, 10)
    if(ans3 == val3):
        correct + 1
    else:
        incorrect + 1
    ans4 = int(input("What is 30 / 5? "))
    val4 = div(30, 5)
    if(ans4 == val4):
        correct + 1
    else:
        incorrect + 1
    ans5 = int(input("What is 29 + 2? "))
    val5 = add(29, 2)
    if(ans5 == val5):
        correct + 1
    else:
        incorrect + 1
    ans6 = int(input("What is 50 - 10? "))
    val6= sub(50, 10)
    if(ans6 == val6):
        correct + 1
    else:
        incorrect + 1
    ans7 = int(input("What is 5 * 11? "))
    val7 = mult(5, 11)
    if(ans7 == val7):
        correct + 1
    else:
        incorrect + 1
    ans8 = int(input("What is 9 / 3? "))
    val8 = div(9, 3)
    if(ans8 == val8):
        correct + 1
    else:
        incorrect + 1
    ans9 = int(input("What is 90 - 5? "))
    val9 = sub(90, 5)
    if(ans9 == val9):
        correct + 1
    else:
        incorrect + 1
    ans10 = int(input("What is 412 + 5? "))
    val10 = add(412, 5)
    if(ans10 == val10):
        correct + 1
    else:
        incorrect + 1
    print()
    print("Thanks, " + str(name) + "!")
    print()
    print("Correct " + str(correct))
    print()
    print("Incorrect " + str(incorrect))
    print()
    calcGrade(correct)

def add(value, value2):
    return value + value2

def sub(value, value2):
    return value - value2

def mult(value, value2):
    return value * value2

def div(value, value2):
    return value / value2

def calcGrade(correct):
    grade = (correct * 100)/ 10
    print("Numeric Grade " + str(grade))
    if(grade > 90):
        letterGrade = "A"
    if(grade > 80):
        letterGrade = "B"
    if(grade < 70):
        letterGrade = "C"
    if(grade < 69):
        letterGrade = "F"
    print()
    print("Letter Grade " + str(letterGrade))

main()

4 个答案:

答案 0 :(得分:0)

当您撰写correct + 1时,您正在评估correct加上一个等于的内容,但您没有更新correct中存储的值。

您要放的是correct = correct + 1。或者,更简洁,correct += 1

同样适用于incorrect

答案 1 :(得分:0)

每种编程语言都有一些特定的功能,您可以将它们用于不同的目的。在Python中,有一个名为eval()的函数,它对你的情况非常有用......

以下是一个使用示例:

In [2]: eval('2*3'), eval('12+13'), eval('30/5')
Out[2]: (6, 25, 6)

请注意,您多次执行相同的操作集:

  • 打印提示
  • 输入数字
  • 评估另一个数字
  • 看到两个数字相等
  • 递增各自的计数器

在Python(或许多其他语言)中,这些将在循环中完成。为此,如果要循环的内容,则需要创建一个列表。列表如下所示:

In [4]: qns
Out[4]:
['4+5',
 '20*6',
 '14-10',
 '30/5',
 '29+2',
 '50-10',
 '5*11',
 '9/3',
 '90-5',
 '412+5']

此时,您可以使用名为 list comprehension 的内容来获取所有结果......

In [5]: [input('what is ' + q + '?')  for q in qns ]
what is 4+5?9
what is 20*6?12
what is 14-10?34
what is 30/5?6
what is 29+2?31
what is 50-10?40
what is 5*11?50
what is 9/3?2
what is 90-5?85
what is 412+5?417
Out[5]: [9, 12, 34, 6, 31, 40, 50, 2, 85, 417]

现在您需要将它们与实际值进行比较。您实际上可以将列表理解中的比较放入单个操作中。

In [6]: results = [input('what is ' + q + '?') == eval(q)  for q in qns ]
what is 4+5?9
what is 20*6?12
what is 14-10?34
what is 30/5?6
what is 29+2?31
what is 50-10?40
what is 5*11?50
what is 9/3?2
what is 90-5?85
what is 412+5?417

In [7]: results
Out[7]: [True, False, False, True, True, True, False, False, True, True]

在Python中,对于某些案例,结果是True == 1False == 0你问哪些案例?嗯,这就是带有“em> duck typing ”的经验。所以在几个月内,凭借足够的经验,你会找到&#34;哪些案例&#34;几乎无足轻重。无论如何,由于这种现象,你可以将正确答案计算为:

In [8]: sum(results)
Out[8]: 6

错误的答案?

In [9]: len(qns) - sum(results)
Out[9]: 4

干杯,快乐的节目!

答案 2 :(得分:0)

你的代码有些错误。请尝试这个。正确+ = 1或正确=正确+ 1和错误 - = 1和错误=错误 - 1

答案 3 :(得分:0)

correct = correct + 1
incorrect = incorrect + 1

通过这样做,正确/不正确答案的当前值会递增并存储在同一个变量中,基本上,这是一个计数器,用于正确和错误答案的数量