背景: 對於我的計算機科學課,我們被要求創建一個程序來幫助小學的孩子學習基礎數學。
他們會選擇他們想要學習的操作(加法,減法,乘法或除法),或者選擇隨機的,隨機選擇這些操作之一。
一旦選擇了一個操作,用戶將被問到一個問題,然後輸入答案,如果正確的程序會問另一個問題,最多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()`
預先感謝您的任何援助,在這裏任何人都可以提供!
原諒我,我的問題代碼的格式,我是新來的網站,不知道如何正確地顯示我的代碼在我的問題 – Jay 2011-03-18 04:57:41
後,您在編輯器中輸入一個代碼塊,高亮顯示它並點擊'{}'按鈕。所做的就是縮進整個塊4個空格。 – 2011-03-18 05:03:35
感謝Jim,下次我使用這個網站時,我一定會記住這一點! – Jay 2011-03-18 05:13:15