繁体   English   中英

程序产生意外的输出-为什么?

[英]Program gives unexpected output — why?

我正在编写一个测试算术技能的程序。 应该产生随机问题,并进行随机运算。 这是我的代码:

import random

score = 0
counter = 0
ops = ['+', '-', '*', '/']

def question():
    num1 = random.randint(0,10)
    num2 = random.randint(1,10) #Starts from 1 to avoid zerodivision error
    operation = random.choice(ops)
    question = float(input(f"What is {num1} {operation} {num2}?: "))
    global answer
    answer = eval(str(num1) + operation + str(num2))
    global counter
    counter = counter + 1


def points():
    while counter < 10:
        question()
        if question == answer:
            print("\nCorrect!\n")
            global score
            score = score + 1
        else:
            print(f"\nWrong! The answer is {answer}\n")


points()

print(f"\nYou got {score} out of {counter}")

但是它给出了以下输出:

What is 9 + 3?: 12
Wrong! The answer is 12

如果输入与答案匹配,则应该说正确,对分数进行计数,最后将分数打印出十分之十。

请帮我解决这个问题。

问题出在

if question == answer:

在该行, question是对函数的引用question()你先前定义。 由于answer是某个持有问题答案的int ,因此==始终为False 您实际上从未发现用户键入了什么。

要解决此问题,请执行以下操作:

def question():
    num1 = random.randint(0,10)
    num2 = random.randint(1,10) #Starts from 1 to avoid zerodivision error
    operation = random.choice(ops)
    user_answer = float(input(f"What is {num1} {operation} {num2}?: "))
    global answer
    answer = eval(str(num1) + operation + str(num2))
    global counter
    counter = counter + 1

    return user_answer

def points():
    while counter < 10:
        user_answer = question()
        if user_answer == answer:
            print("\nCorrect!\n")
            global score
            score = score + 1
        else:
            print(f"\nWrong! The answer is {answer}\n")

我更改了名称以使其更清楚地显示正在发生的事情。


作为附录,我强烈建议不要使用全局变量。 他们几乎从来没有必要,通常会使问题困惑。 例如,我认为

def question():
    num1 = random.randint(0,10)
    num2 = random.randint(1,10) #Starts from 1 to avoid zerodivision error
    operation = random.choice(ops)

    user_answer = float(input(f"What is {num1} {operation} {num2}?: "))
    real_answer = eval(str(num1) + operation + str(num2))

    return user_answer, real_answer

def points():
    score = 0
    for questions_asked in range(1, 11):
        user_answer, real_answer = question()
        if user_answer == real_answer:
            print("\nCorrect!\n")
            score += 1
        else:
            print(f"\nWrong! The answer is {answer}\n")

这样可以更容易地理解程序流程。

您的问题已经由乔什·卡佩尔(Josh Karpel)回答,但我只是想指出另外一个与分裂有关的问题。

除法将始终导致float而不是int ,因此,如果用户说4 / 22 ...脚本将说出答案是错误的,因为它正在对"2""2.0"进行严格的比较。 这就是为什么在Josh的版本中,他将用户答案转换为float 这样,就可以对答案进行数值比较。

此外,该脚本当前能够询问诸如What is 4 / 7 ? 一个人很难回答。 一种解决方法是将num2随机化,直到被num1整除。

def question():
    num1 = random.randint(0, 10)
    num2 = random.randint(1, 10)
    operation = random.choice(ops)
    if operation == '/':
        while num1 % num2 != 0:
            num2 = random.randint(1, 10)
    # do eval

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM