An endless loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Schleimer
    New Member
    • Nov 2011
    • 2

    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.

    Code:
    #This program will help a person keep their budget
    totalBudget = 4000
    
    #the main function
    def main ():
        print 'Welcome to my personal budget calculator'
        print
        choice ()
        
    #this function will start the menu selection
    def choice ():
        print 'Menu Selection'
        print '1 - Add an Expense'
        print '2 - Remove an Expense'
        print '3 - Add a Revenue'
        print '4 - Remove a Revenue'
        print '5 - Exit'
        selection = input ('Enter menu selection ')
        while selection  !=5:
            if selection == 1:
                addExpense (totalBudget)
            if selection == 2:
                removeExpense (totalBudget)
            if selection == 3:
                addRevenue (totalBudget)
            if selection == 4:
                removeRevenue (totalBudget)
            if selection == 5:
                print 'Goodbye'
        
    #this function will add an expense
    def addExpense (totalBudget):
        addAmount = input ('Enter the amount of the expense to be added. ')
        addFrequency = input ('Enter the frequency of the expense. ')
        addTotal = addAmount * addFrequency
        if totalBudget > addTotal:
            totalBudget = totalBudget - addTotal
            print 'The new budget is', totalBudget
            return totalBudget
        elif totalBudget < addTotal:
            print 'The expense exceeds current budget. Try again'
            
        
    #this function will remove an expense
    def removeExpense (totalBudget):
        removeAmount = input ('Enter the amount of the expense to be removed. ')
        removeFrequency = input ('Enter the frequency of the expense. ')
        removeTotal = removeAmount * removeFrequency
        if removeTotal > totalBudget:
            print 'Expenses exceed the budget. Try again.'
        elif removeTotal < totalBudget:
            totalBudget = totalBudget + removeTotal
            print 'The new budget is', totalBudget
            return totalBudget
          
    #this function will add a revenue
    def addRevenue (totalBudget):
        addIncome = input ('Enter the amount of the revenue to be added. ')
        totalBudget = addIncome + totalBudget
        print 'Your new budget is', totalBudget
        return totalBudget
       
    #this function will remove a revenue
    def removeRevenue (totalBudget):
        removeIncome = input ('Enter the amount of the revenue to be removed. ')
        if removeIncome > totalBudget:
            print 'Your income exceeds budget. Try again.'
        elif removeIncome < totalBudget:
            totalBudget = totalBudget - removeIncome
            print 'Your new budget is', totalBudget
            return totalBudget
    
    main()
    Last edited by miller; Nov 22 '11, 05:47 AM.
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    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:
    Code:
    def choice ():
        while True:
            print
            print 'Menu Selection'
            print '1 - Add an Expense'
            print '2 - Remove an Expense'
            print '3 - Add a Revenue'
            print '4 - Remove a Revenue'
            print '5 - Exit'
            selection = input ('Enter menu selection ')
            if selection == 1:
                addExpense (totalBudget)
            if selection == 2:
                removeExpense (totalBudget)
            if selection == 3:
                addRevenue (totalBudget)
            if selection == 4:
                removeRevenue (totalBudget)
            if selection == 5:
                print 'Goodbye'
                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:
    Code:
    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.

    Comment

    Working...