0

背景:在我的计算机科学课上,我们被要求创建一个程序来帮助小学生学习基础数学。
他们会选择他们想学习的操作(加法、减法、乘法或除法),或者选择随机的,它会随机选择这些操作之一。
一旦选择了一个操作,用户会被问一个问题,然后输入答案,如果正确,程序会再问一个问题,总共最多4个问题,然后程序会返回菜单。
如果答案不正确,它要求用户重新输入答案,最多3次,如果答案仍然不正确,将显示正确答案,然后再问一个问题(如果没有达到4个问题配额) 或如果没有其他问题返回菜单。

问题:我把所有东西都写出来了,当我在 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()`

预先感谢您在这里提供的任何帮助!

4

2 回答 2

5

您需要将其放在raw_inputwhile 循环中。

将主要更改为此:

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 循环之前调用了一次。然而,它再也没有被调用过。相反,循环将继续,但它将继续使用selection它在第一次(也是唯一一次)调用中检索到的相同值raw_input

此外,您不需要quit()main函数结束时。你可以让函数返回。尽管这与您的错误无关。

于 2011-03-18T05:01:48.137 回答
0

这将产生基于随机数和操作的问题。

从字符串导入下
从运算符导入添加,子,mul
从随机导入randint,选择

ops = { '+': 添加, '-': sub, '*': mul}
MAXTRIES = 2

def doprob():
    op = 选择('+-*')
    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])
    哎呀 = 0
    而真:
        尝试:
            如果 int(raw_input(pr)) == ans:
                打印'正确'
                休息
            如果 oops == MAXTRIES:
                print 'answer\n%s%d'%(pr, ans)
            别的:
                print '不正确...再试一次'
                哎呀 = 哎呀 + 1
        除了(键盘中断、EOFError、ValueError):
            print '无效的输入...再试一次'

定义主():
    而真:
        多普罗布()
        尝试:
            opt = lower(raw_input('Again?'))
        除了(键盘中断,EOFError):
            打印 ; 休息
        如果 opt 和 opt[0] == 'n':
            休息

如果 __name__ == '__main__':
    主要的()

于 2011-03-18T05:14:20.190 回答