无尽的循环

我正在为我开始的程序课写一个程序,作为期末项目。我已经完成了所有的工作,除了当我运行一个模块时,它会导致同一个模块的无限循环。也许我需要一种方法来清除每次运行时的选项()。

选择 | 换行 | 行号
  1.  
  2. #This program will help a person keep their budget
  3. totalBudget = 4000
  4.  
  5. #the main function
  6. def main ():
  7.     print 'Welcome to my personal budget calculator'
  8.     print
  9.     choice ()
  10.  
  11. #this function will start the menu selection
  12. def choice ():
  13.     print 'Menu Selection'
  14.     print '1 - Add an Expense'
  15.     print '2 - Remove an Expense'
  16.     print '3 - Add a Revenue'
  17.     print '4 - Remove a Revenue'
  18.     print '5 - Exit'
  19.     selection = input ('Enter menu selection ')
  20.     while selection  !=5:
  21.         if selection == 1:
  22.             addExpense (totalBudget)
  23.         if selection == 2:
  24.             removeExpense (totalBudget)
  25.         if selection == 3:
  26.             addRevenue (totalBudget)
  27.         if selection == 4:
  28.             removeRevenue (totalBudget)
  29.         if selection == 5:
  30.             print 'Goodbye'
  31.  
  32. #this function will add an expense
  33. def addExpense (totalBudget):
  34.     addAmount = input ('Enter the amount of the expense to be added. ')
  35.     addFrequency = input ('Enter the frequency of the expense. ')
  36.     addTotal = addAmount * addFrequency
  37.     if totalBudget > addTotal:
  38.         totalBudget = totalBudget - addTotal
  39.         print 'The new budget is', totalBudget
  40.         return totalBudget
  41.     elif totalBudget < addTotal:
  42.         print 'The expense exceeds current budget. Try again'
  43.  
  44.  
  45. #this function will remove an expense
  46. def removeExpense (totalBudget):
  47.     removeAmount = input ('Enter the amount of the expense to be removed. ')
  48.     removeFrequency = input ('Enter the frequency of the expense. ')
  49.     removeTotal = removeAmount * removeFrequency
  50.     if removeTotal > totalBudget:
  51.         print 'Expenses exceed the budget. Try again.'
  52.     elif removeTotal < totalBudget:
  53.         totalBudget = totalBudget + removeTotal
  54.         print 'The new budget is', totalBudget
  55.         return totalBudget
  56.  
  57. #this function will add a revenue
  58. def addRevenue (totalBudget):
  59.     addIncome = input ('Enter the amount of the revenue to be added. ')
  60.     totalBudget = addIncome + totalBudget
  61.     print 'Your new budget is', totalBudget
  62.     return totalBudget
  63.  
  64. #this function will remove a revenue
  65. def removeRevenue (totalBudget):
  66.     removeIncome = input ('Enter the amount of the revenue to be removed. ')
  67.     if removeIncome > totalBudget:
  68.         print 'Your income exceeds budget. Try again.'
  69.     elif removeIncome < totalBudget:
  70.         totalBudget = totalBudget - removeIncome
  71.         print 'Your new budget is', totalBudget
  72.         return totalBudget
  73.  
  74. main()
# 回答1


嗨。感谢您的帖子
问题是,选择变量是在选择开始时设置的,然后再也不允许更改。这是因为您随后进入了一个循环,循环不停地循环,但选择永远不会改变!
按如下方式调整选择会让你理清头绪:

选择 | 换行 | 行号
  1. def choice ():
  2.     while True:
  3.         print
  4.         print 'Menu Selection'
  5.         print '1 - Add an Expense'
  6.         print '2 - Remove an Expense'
  7.         print '3 - Add a Revenue'
  8.         print '4 - Remove a Revenue'
  9.         print '5 - Exit'
  10.         selection = input ('Enter menu selection ')
  11.         if selection == 1:
  12.             addExpense (totalBudget)
  13.         if selection == 2:
  14.             removeExpense (totalBudget)
  15.         if selection == 3:
  16.             addRevenue (totalBudget)
  17.         if selection == 4:
  18.             removeRevenue (totalBudget)
  19.         if selection == 5:
  20.             print 'Goodbye'
  21.             break

祝好运!
顺便说一句,我做了另外两个不必要的更改:
1.我让循环显示"While True:",然后包含一条Break语句。你可以很容易地坚持你的做法("选择的时候!-5")。
2.我在菜单顶部添加了一条打印,以清除一行内容。
我想说的另一件事是,使用以下命令运行代码:

选择 | 换行 | 行号
  1. if __name__=="__main__": main()

更好,因为这样您就可以在不运行代码的情况下导入代码(即在其他地方获得函数,而不运行主循环),但是如果您运行这个特定的脚本,它就会正常运行。

标签: python

添加新评论