刚开始在我的计算基础课上学习Python。我们必须建立一个为数学创造练习集的程序。用户输入两个数字,一个运算符,然后是一个解,程序会告诉他们是否正确。在那之后,程序应该循环,这样用户就可以继续练习了。这个程序应该循环5套练习。我有这个循环,但是当它被执行时,它会重复用户最初输入的输入,而不是重置,因此用户不能再进行任何输入。我错过了什么
我尝试了一段时间:对我的全局变量进行循环,但这只会导致主函数在没有完成程序的情况下循环。作业非常清楚地指出需要使用while循环,因为我的教授提供了一些帮助我们的提示
num1 = int(input("Enter First Input: "))
num2 = int(input("Enter Second Input: "))
op = str(input("Enter Operator: "))
UserSolution = int(input("Enter Solution: "))
res1 = num1+num2
res2 = num1-num2
res3 = num1*num2
res4 = num1/num2
timesCorrect = 0
timesIncorrect = 0
def main ():
counter = 0
while counter < 4:
print(num1)
print(num2)
print(op)
print(UserSolution)
counter = counter + 1
函数确实像我希望的那样循环,但是它没有像我希望的那样重置变量
可能会超出你的任务范围,但这里有一个未经验证的建议:
您需要将输入语句移动到循环中,例如:
另外,如果希望它循环五次,则需要将计数器比较改为
< 5
或<= 4
,而不是< 4
相关问题 更多 >
编程相关推荐