Python程序调试:无限循环问题
背景:
在我的计算机科学课上,我们被要求创建一个程序,帮助小学生学习基础数学。
孩子们可以选择想要学习的运算(加法、减法、乘法或除法),或者选择随机,这样程序会随机选择一种运算。
一旦选择了运算,程序会问用户一个问题,然后让他们输入答案。如果答案正确,程序会继续问下一个问题,最多问四个问题,然后返回菜单。
如果答案不正确,程序会让用户再输入答案,最多三次。如果答案还是不对,程序会显示正确答案,然后再问一个问题(如果还没问够四个问题),如果问题问完了,就返回菜单。
问题:
我已经把所有内容写好了,当我在IDLE中运行程序时,一切看起来都正常,但在选择运算后,程序却卡在一个无限循环中,无法在问完四个问题后返回菜单。
我最开始用的是for循环来满足四个问题的要求,但没有成功,所以我试了while循环,代码是 while x<4: 等等
,在while循环之前定义了x为0,然后在函数的最后加上 x=x+1
。
从阅读代码来看,似乎每个函数都应该能正常工作,但运行后我还是卡在了无限循环中。
这是代码:
def show_instructions():
"""
Displays a greeting to the user and provides instructions on how to use the
program. [PURPOSE]
"""
print " "
print "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"
print " Math Mania"
print " "
print "Welcome to Math Mania! This program is designed to help you learn basic"
print "math skills in addition, subtraction, multiplication, and division."
print "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"
print " "
print "To learn a skill, type the first letter of that skill."
print " "
print "a for addition"
print "s for subtraction"
print "m for multiplication"
print "d for division"
print "r for random"
print "q to quit"
print " "
def add():
"""
generates display two random numbers and sums them, then prompts the user
to input the correct sum, if the input is incorrect, it prompts the user
to try again.
[PURPOSE]
"""
x=0
while x<4:
num1 = random.randint(1,20)
num2 = random.randint(1,20)
print num1, "+", num2, '= ?'
answer = input ('Enter your answer: ')
count1=0
while answer != num1+num2 and count1<3:
count1=count1 +1
print 'Incorrect, please try again.'
print
print num1, '+', num2, '= ?'
answer = input ('Enter your answer: ')
if count1==3:
print "Sorry, that's incorrect."
print "The correct answer is ",num1+num2
else:
print "That's correct!"
print
x=x+1
def sub():
"""
generates and displays two random numbers and subtracts the smaller of the
two from the larger one. It then prompts the user to input the correct
answer, if the input is incorrect, it prompts the user to try again.
[PURPOSE]
"""
x=0
while x<4:
num1 = random.randint(1,20)
num2 = random.randint(1,20)
if num1>num2:
print num1, "-", num2, '= ?'
answer = input('Enter your answer: ')
count1=0
while answer != num1 - num2 and count1<3:
count1=count1+1
print 'Incorrect, please try again.'
print
print num1, "-", num2, '= ?'
answer = input ('Enter your answer: ')
if count1==3:
print "Sorry, that's incorrect."
print "The correct answer is ",num1-num2
else:
print "That's correct!"
print
x=x+1
else:
print num2, "-", num1, '= ?'
answer = input ('Enter your answer')
count1=0
while answer!= num2-num1 and count1<3:
count1=count1+1
print 'Incorrect, please try again.'
print
print num2, "-", num1, '= ?'
answer = input ('Enter your answer: ')
if count1==3:
print "Sorry, that's incorrect."
print "The correct answer is ",num2-num1
else:
print 'Thats correct!'
print
x=x+1
def mult():
"""
generates and displays two random numbers and finds the product of the two.
It then prompts the user to input the correct product of the two numbers, if
the input is incorrect, it prompts the user to try again.
[PURPOSE]
"""
x=0
while x<4:
num1 = random.randint(1,20)
num2 = random.randint(1,20)
print num1, "x", num2, '= ?'
answer = input ('Enter your answer: ')
count1=0
while answer != num1*num2 and count1<3:
count1=count1+1
print 'Incorrect, please try again.'
print
print num1, 'x', num2, '= ?'
answer = input ('Enter your answer: ')
if count1==3:
print "Sorry, that's incorrect."
print "The correct answer is ", num1*num2
else:
print "That's correct!"
print
x=x+1
def div():
"""
generates and displays the quotient of two numbers, and then prompts the
user to input the correct answer, if the input is incorrect, it then prompts
the user to try again.
[PURPOSE]
"""
x=0
while x<4:
num1 = random.randint(1,20)
num2 = random.randint(1,20)
while (num1%num2!=0):
num2 = random.randint(1,20)
num1 = random.randint(1,20)
print num1, "/", num2, '= ?'
answer = input ('Enter your answer: ')
count1=0
while answer != num1/num2 and count1<3:
count1=count1 +1
print 'Incorrect, please try again.'
print num1, '/', num2, '= ?'
answer = input ('enter your answer:')
if count1==3:
print "Sorry, that's incorrect."
print "The correct answer is ",num1/num2
else:
print "That's correct!"
print
x=x+1
def rand():
"""
picks a arithmetic function at random for the user to to try
[PURPOSE]
"""
num=random.randint(1,4)
if num==1:
add()
if num==2:
sub()
if num==3:
mult()
if num==4:
div()
def main():
"""
main function that brings it all together
[PURPOSE]
"""
show_instructions()
selection = raw_input ('Please select the skill you want to learn: ')
while selection != "q":
if selection == "a":
add()
elif selection == "s":
sub()
elif selection == "m":
mult()
elif selection == "d":
div()
elif selection == "r":
rand()
print "The program will now quit."
quit()
main()`
非常感谢大家提供的任何帮助!
2 个回答
0
这段代码会根据随机数字和运算生成一些问题。
from string import lower from operator import add, sub, mul from random import randint, choice ops = { '+': add, '-': sub, '*': mul} MAXTRIES = 2 def doprob(): op = choice('+-*') nums = [randint(1,10), randint(1,10)] nums.sort();nums.reverse() ans = apply(ops[op], nums) pr = '%d %s %d = ' % (nums[0], op, nums[1]) oops = 0 while True: try: if int(raw_input(pr)) == ans: print 'correct' break if oops == MAXTRIES: print 'answer\n%s%d'%(pr, ans) else: print 'incorrect... try again' oops = oops + 1 except (KeyboardInterrupt, EOFError, ValueError): print 'invalid input... try again' def main(): while True: doprob() try: opt = lower(raw_input('Again? ' )) except (KeyboardInterrupt, EOFError): print ; break if opt and opt[0] == 'n': break if __name__ == '__main__': main()
5
你需要把 raw_input
放在 while 循环里面。
把 main 改成这样:
def main():
"""
main function that brings it all together
[PURPOSE]
"""
show_instructions()
selection = None
while selection != "q":
selection = raw_input ('Please select the skill you want to learn: ')
if selection == "a":
add()
elif selection == "s":
sub()
elif selection == "m":
mult()
elif selection == "d":
div()
elif selection == "r":
rand()
print "The program will now quit."
这里的问题是 raw_input
只在 while 循环之前被调用了一次。但是,它之后再也没有被调用过。这样一来,循环会继续进行,但它会一直使用第一次(也是唯一一次)调用 raw_input
时得到的 selection
值。
另外,你在 main
函数的最后不需要 quit()
。你只需要让函数返回就可以了。虽然这和你的错误没有关系。