Python 数学测验 - 输出正确或错误
Python Maths Quiz- Outputting Correct or Incorrect
import random
import time
counter=0
score=0
count=0
function=['+','x','÷','-']
print('Welcome To The Arithmetic Quiz!')
name=input('Please enter you name..')
print('Thanks' , name , 'Lets Get Started!')
while counter <10:
firstnumber=random.randint(0,12)
secondnumber=random.randint(0,6)
function=random.choice(function)
question=print(firstnumber, function, secondnumber, '=')
input('Answer:')
counter= counter+1
if function== '+':
count==firstnumber+secondnumber
if count == int (answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif function== 'x':
count==firstnumber*secondnumber
if count == int (answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif function== '-':
count==firstnumber-secondnumber
if count == int (answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif function== '÷':
count==firstnumber/secondnumber
if count == int (answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
谁能更正结束部分(if
函数)和(elif
函数)
我认为这与变量名有关。
它也没有正确 运行,因为它停在:print('Thanks' , name , 'Lets Get Started!')
,我再次不确定这是为什么。
你的计数器在你的 while
循环中没有增加,因为你有一些严重的缩进问题。结果,您的 while
循环将永远运行,并且您有一个无限循环。确保 缩进 代码 包括计数器 ,您希望在 while
循环中包含该代码以便执行该代码并让你的 while
循环真正停止。
编辑:
我修复了你的缩进和你的柜台。请注意,除非商恰好是整数,否则您的除法仍然无效,如果您想解决这个问题,您必须自己做一些研究。
import random
import time
counter=0
score=0
count=0
function=['+','x','÷','-']
print('Welcome To The Arithmetic Quiz!')
name=input('Please enter you name.')
print('Thanks' , name , 'Lets Get Started!')
while counter <10:
firstnumber=random.randint(0,12)
secondnumber=random.randint(0,6)
operator=random.choice(function)
question=print(firstnumber, operator, secondnumber, '=')
userAnswer = input('Answer:')
if operator== '+':
count=firstnumber+secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif operator== 'x':
count=firstnumber*secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif operator== '-':
count=firstnumber-secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif operator== '÷':
count=firstnumber/secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
counter += 1
print ("Your quiz is over!")
正如 Elizion 所指出的,您的代码缩进不当。另外 count==number + number
应该是 count=number + number
。
然后为函数分配一个永远无法更改的值 - 考虑更改其中一个变量名称。使用 else
,而不是 elif
作为最终可能的条件。最后打印分数怎么样。
我已经更正了您的代码,但您先试试看...
import random
import time
counter=0
score=0
count=0
function=['+','x','÷','-']
print('Welcome To The Arithmetic Quiz!')
name=input('Please enter you name..')
print('Thanks' , name , 'Lets Get Started!')
while counter <10:
firstnumber=random.randint(0,12)
secondnumber=random.randint(0,6)
function=random.choice(function)
question=print(firstnumber, function, secondnumber, '=')
input('Answer:')
counter= counter+1
if function== '+':
count==firstnumber+secondnumber
if count == int (answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif function== 'x':
count==firstnumber*secondnumber
if count == int (answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif function== '-':
count==firstnumber-secondnumber
if count == int (answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif function== '÷':
count==firstnumber/secondnumber
if count == int (answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
谁能更正结束部分(if
函数)和(elif
函数)
我认为这与变量名有关。
它也没有正确 运行,因为它停在:print('Thanks' , name , 'Lets Get Started!')
,我再次不确定这是为什么。
你的计数器在你的 while
循环中没有增加,因为你有一些严重的缩进问题。结果,您的 while
循环将永远运行,并且您有一个无限循环。确保 缩进 代码 包括计数器 ,您希望在 while
循环中包含该代码以便执行该代码并让你的 while
循环真正停止。
编辑: 我修复了你的缩进和你的柜台。请注意,除非商恰好是整数,否则您的除法仍然无效,如果您想解决这个问题,您必须自己做一些研究。
import random
import time
counter=0
score=0
count=0
function=['+','x','÷','-']
print('Welcome To The Arithmetic Quiz!')
name=input('Please enter you name.')
print('Thanks' , name , 'Lets Get Started!')
while counter <10:
firstnumber=random.randint(0,12)
secondnumber=random.randint(0,6)
operator=random.choice(function)
question=print(firstnumber, operator, secondnumber, '=')
userAnswer = input('Answer:')
if operator== '+':
count=firstnumber+secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif operator== 'x':
count=firstnumber*secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif operator== '-':
count=firstnumber-secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif operator== '÷':
count=firstnumber/secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
counter += 1
print ("Your quiz is over!")
正如 Elizion 所指出的,您的代码缩进不当。另外 count==number + number
应该是 count=number + number
。
然后为函数分配一个永远无法更改的值 - 考虑更改其中一个变量名称。使用 else
,而不是 elif
作为最终可能的条件。最后打印分数怎么样。
我已经更正了您的代码,但您先试试看...