473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

An endless loop

I am writing a program for my beginning program class as a final project. I got it all done, except when I run through a module, it causes an endless loop through the same module. Maybe I need a way to clear the choice () every time it runs.

Expand|Select|Wrap|Line Numbers
  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()
Nov 22 '11 #1
1 2464
Glenton
391 Expert 256MB
Hi. Thanks for posting

The issue is that the selection variable is set at the beginning of choice, and is then never allowed to change again. This is because you then enter into a loop, and the loop goes around and around, but selection never changes!

Adjusting choice as follows would sort you out:
Expand|Select|Wrap|Line Numbers
  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
Good luck!

By the way, I made two other changes that weren't really necessary:
1. I made the loop say "while True:" and then included a break statement. You could easily have stayed with the way you did it ("while selection!-5").
2. I added a print to clear a line at the top of the menu.

The other thing I wanted to say, was that running your code with:
Expand|Select|Wrap|Line Numbers
  1. if __name__=="__main__": main()
is better, because then you can import your code without running it (ie get your functions somewhere else, without running the main loop), but if you run this particular script, then it runs as normal.
Nov 22 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: codesmithsf | last post by:
I'm using PHP 4.2.2 and Apache 2.0.4.0. A script running on a virtual host had an endless loop in it that brought Apache to its knees. I have max_execution_time=90 and memory_limit=32M to...
3
by: John F. | last post by:
Hi all, I'm searching fulltime for days now, and I don't find the solution, so I'm thinking this is more a bug :( If i use following code in form&subforms:
2
by: Tom van Stiphout | last post by:
Hi All, This one has me stumped. I hope someone else has seen this behavior and knows what's going on. I'm using Access 2003 ADP on Windows XP, database in 2002/2003 format, with SQL Server 2000....
24
by: Tweaxor | last post by:
This has been puzzling me all this week. This is actually a homework assignment from last semesmter. But the teacher wouldn't tell us why certain things didn't work, but it didn't just work. My...
13
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include...
0
by: Bill Borg | last post by:
Hello all, Not sure I can describe this adequately, but I am creating an endless loop when an authenticated user signs out. When the user signs out, I want to remove authentication, abandon...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
1
by: cory6903 | last post by:
i am having a problem with an endless loop **int x; **a: **cout << "enter a #" << endl; **cin >> x **switch(x) **{ ** case 1:
6
by: uche | last post by:
This function that I have implemented gives me an infinite loop. I am trying to produce a hexdum program, however, this function is not functioning correctly.....Please help. void...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.