2011-03-18 176 views
0

背景: 对于我的计算机科学课,我们被要求创建一个程序来帮助小学的孩子学习基础数学。
他们会选择他们想要学习的操作(加法,减法,乘法或除法),或者选择随机的,随机选择这些操作之一。
一旦选择了一个操作,用户将被问到一个问题,然后输入答案,如果正确的程序会问另一个问题,最多4个问题总数,然后程序将返回菜单。
如果答案不正确,它会要求用户再次输入答案,最多三次,如果答案仍然不正确,将显示正确的答案,则会提出另一个问题(如果4个问题定额是如果没有其他问题,则返回到菜单。Python程序调试:无限循环

问题: 我已经写出所有内容,并且当我在IDLE中运行程序时,一切看起来都在工作,但是在出于某种原因选择某个操作之后,程序停留在无限循环中,并且不会返回到已经询问了4个问题之后的菜单。
我第一次使用for循环来满足4个问题的配额,并且没有工作,所以然后我尝试了一个while循环,它读取while x<4: etc etc,在while循环之前将x定义为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()` 

预先感谢您的任何援助,在这里任何人都可以提供!

+0

原谅我,我的问题代码的格式,我是新来的网站,不知道如何正确地显示我的代码在我的问题 – Jay 2011-03-18 04:57:41

+0

后,您在编辑器中输入一个代码块,高亮显示它并点击'{}'按钮。所做的就是缩进整个块4个空格。 – 2011-03-18 05:03:35

+0

感谢Jim,下次我使用这个网站时,我一定会记住这一点! – Jay 2011-03-18 05:13:15

回答

5

您需要将raw_input置于while循环内。

更改主这样:

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()。你可以让函数返回。虽然这与你的bug无关。

+0

这个伎俩,非常感谢你dappawit! – Jay 2011-03-18 05:12:43

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()