Python 具有值错误的代码
我需要帮助我的代码,不断给我这个错误。 错误是:Python 具有值错误的代码,python,python-3.x,Python,Python 3.x,我需要帮助我的代码,不断给我这个错误。 错误是: ValueError:要解压缩的值太多(预期为2个) 给出错误的那行是这行: question,answer=generateQuestions(maxNum) 我想不出怎么修理它。下面是我的代码: import random def generateQuestions ( maxNum ) : ops = ['+','-'] var1 = random.randrange ( maxNum )
ValueError:要解压缩的值太多(预期为2个)
给出错误的那行是这行:
question,answer=generateQuestions(maxNum)
我想不出怎么修理它。下面是我的代码:
import random
def generateQuestions ( maxNum ) :
ops = ['+','-']
var1 = random.randrange ( maxNum )
var2 = random.randrange ( maxNum )
maxNum += 1
operation = random.choice (ops)
ques = 'What is' + str(var1) + operation + str(var2) + '?'
if operation == '+' :
var1 + var2
else :
var1 - var2
return ques
difficulty = 0
lives = 0
numofquestion = 0
correctCount = 0
while int (difficulty) <= int (0) or int(difficulty) >= int(4) :
print ('Welcome to the Math Tester')
print ('Please choose your difficulty')
difficulty = int (input ( '"1" for Easy \n"2" for Medium \n"3" for Hard\n'))
if difficulty == 1 :
print ('Easy Selected, You Have 3 Lives')
lives , maxNum = 3,10
elif difficulty == 2 :
print ('Medium Selected, You Have 2 Lives')
lives , maxNum = 2,25
elif difficulty == 3 :
print ('Hard Selected, You Have 1 Life')
lives , maxNum = 1,50
for numofquestion in range (10) :
print ( 'Question', numofquestion + 1, 'of 10,',lives ,'lives remaining.')
question , answer = generateQuestions (maxNum)
print ( question)
userAns = int (input ())
if answer == userAns :
print ('Correct!')
correctCount += 1
else :
print ('Incorrect!')
lives -= 1
print ('Result :')
print ('You scored {} / 10.'.format (correctCount))
print ('{} % - You {}!' .format (( correctCount / 10)*100, 'pass' if correctCount > 4 else 'fail'))
随机导入
def generateQuestions(最大数量):
ops=['+','-']
var1=random.randrange(maxNum)
var2=random.randrange(maxNum)
maxNum+=1
操作=随机选择(ops)
ques='什么是'+str(var1)+操作+str(var2)+'?'
如果操作=='+':
var1+var2
其他:
var1-var2
回信
难度=0
寿命=0
numofquestion=0
更正计数=0
而int(难度)=int(4):
打印(“欢迎使用数学测试仪”)
打印('请选择您的难度')
难度=int(输入('1'表示简单\n'2'表示中等\n'3'表示硬\n'))
如果难度==1:
打印('容易选择,您有3条生命')
生命,最大数量=3,10
elif难度==2:
打印('选择介质,您有两条生命')
生命,最大数量=2,25
elif难度==3:
打印('硬选择,您有1条生命')
生命,最大数量=1,50
对于范围(10)内的问题:
打印('Question',numofquestion+1','of10','lifes','lifes remaining')
问题,答案=generateQuestions(maxNum)
打印(问题)
userAns=int(输入())
如果答案==userAns:
打印('正确!')
正确计数+=1
其他:
打印('不正确!')
寿命-=1
打印('结果:')
打印('您的得分为{}/10.'.format(correctCount))
打印('{}%-You{}!'.format((correctCount/10)*100,如果correctCount>4,则为“通过”,否则为“失败”)
您的generateQuestions
函数不会同时返回问题和答案:它只返回问题。您确实在该函数中计算答案,但您甚至没有将其保存在变量中,更不用说返回它了
您需要存储该计算并将其与ques
一起返回
(我故意没有给你实际的代码,因为这显然是家庭作业。)错误的原因是,你只是从函数中返回问题,尽管你希望它也会返回答案。对您的功能进行以下更改应该有效:
def generateQuestions ( maxNum ) :
ops = ['+','-']
var1 = random.randrange ( maxNum )
var2 = random.randrange ( maxNum )
maxNum += 1
operation = random.choice (ops)
ques = 'What is' + str(var1) + operation + str(var2) + '?'
if operation == '+' :
answer = var1 + var2
else :
answer = var1 - var2
return ques, answer
正如我在回答中提到的,最好不要给出家庭作业问题的实际答案。谢谢你提供的信息。对不起,我的作业给我带来了麻烦。谢谢你给我解释这个错误。你的解释帮助了我。