Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 它给出了正确的答案,有时给出了错误的答案_Python_Loops_Math - Fatal编程技术网

Python 它给出了正确的答案,有时给出了错误的答案

Python 它给出了正确的答案,有时给出了错误的答案,python,loops,math,Python,Loops,Math,当我这样做时,它会生成一个只循环两次的随机测验,尽管我希望它循环10次。而且,它有时给出正确的答案,有时给出错误的答案。例如: print("Hello, and welcome to the Maths quiz!/n") #Asks user for name - 05/03/2015 name = input("What is your name?") #This will import random to generate random functions import random

当我这样做时,它会生成一个只循环两次的随机测验,尽管我希望它循环10次。而且,它有时给出正确的答案,有时给出错误的答案。例如:

print("Hello, and welcome to the Maths quiz!/n")

#Asks user for name - 05/03/2015
name = input("What is your name?")

#This will import random to generate random functions
import random

#This is a variable for score
#It has been set to 0 at the start of the program
score = 0

#This creates an array containing the mathematical operators
#that this quiz will use
ops = ['+','-','*']

#A loop has been set for 0 - 10
for x in (0,10):
    #This is variable has been set to the operator of the equation that
    #uses the random function and will choose a random operator from the
    #array containing the operators made earlier
    op = random.choice(ops)
    if op == '+':
        left1 = random.randint(1,100)
        right1 = random.randint(1,100)
        print (str(left1) + op + str(right1))
        answer  = eval(str(left1) + op + str(right1))
        guess = input("")
        if guess == answer:
            print("Correct!")
            score += 1
        else:
            print ("Incorrect")
    elif op == '-':
        left2 = random.randint(1,100)
        right2 = random.randint(1,100)
        print (str(left2) + op + str(right2))
        answer1 = eval(str(left2) + op + str(right2))
        guess1 = int(input(""))
        if answer1 == guess1:
            print("Correct!")
            score += 1
        else:
            print("Incorrect")
    elif op == '*':
        left3 = random.randint(1,100)
        right3 = random.randint(1,100)
        print (str(left3) + op + str(right3))
        answer2 = eval(str(left3) + op + str(right3))
        guess2 = input("")
        if answer2 == guess2:
            print("Correct!")
            score += 1
        else:
            print("Incorrect")
    else:
        break

print (score)

我想做的是使用加法、减法和乘法运算符生成随机算术题。另外,它要循环10次,并在最后给出10分。

您并不总是将输入转换为整数

op=='*'
op=='+'
时,尝试与从
input()返回的字符串进行比较:

字符串与数字的比较永远不相等。对于
op='-'
您首先正确地将答案转换为整数:

guess2 = input("")
你的环也断了;您正在循环一个包含两个值的元组:

guess1 = int(input(""))
而不是超过一个范围:

for x in (0, 10):
您最好避免代码中出现如此多的重复;计算表达结果并在一个地方询问答案;这样,犯错误的地方就少了。

for x in range(0, 10):
运行代码(
)两次:一次将
x
设置为0,第二次将
x
设置为10

你真正想要的是:

for x in (0,10):
    ...

然后
x
将是0、1、…、9,代码运行10次。

为什么在三个if块中执行完全相同的操作?当然,一般性地定义op并使用
eval
的整个要点是,您不需要重复代码。你根本不需要if/elif语句。@iced:他们自己在做作业,只是问为什么它不起作用。@iced:真的,就作业(或者在本例中是GCSE考试)问题而言,这并不是那么糟糕。我不知道GCSE考试是什么,但我坚信,任何考试的目的都是测试一个特定的考生,而不是社区。虽然我同意这个问题(以及其中的代码)比大多数家庭作业问题写得好得多。@iced:GCSE是英国高中(14-16岁)国家考试,这是计算GCSE的编码部分,请参阅。这是选择2,其中包括一些更多的工作。
for x in (0,10):
    ...
for x in range(10):
    ...