dimanche 27 juin 2021

A section of code loops itself before proceeding

I just figured out a way to remember the users last menu selection by utilizing a Python list, but after implementing and running it, after the code has cycled through once, I choose an option from the menu and it loops itself once then proceeds as normal.

To begin with, I thought it might have to do with the python list needing to bear cleared, but I tried thislist.clear() and no luck. Then, I thought it had to do with the number input variable needing to be used in the main section of the code, however it did not work. Unfortunately, I have had no further inspiration of what it could be, so your help would be greatly appreciated, please!

Here is my code:

import random 
global thislist
thislist = []

def menu(): 
    print(""" 
Which account would you like to access? 
            
            1. Balance 
            ----------    
            2. Savings 
            ----------""")    
    number = int(input("\nEnter option: ")) 
    return number

def balance(): 
    while True: 
        try:
            while True: # not sure why but this works 
                print("""
What would you like to do?
                        
            1. View
            ---------
            2. Update
            ---------""")
                selected_option_01 = int(input("\nEnter option: "))
                if selected_option_01 == 1:
                    view_bal = open("current_balance.txt", "r")
                    print("\nBalance: " + view_bal.read())
                    view_bal.close()
                    thislist.append("balance")
                    break
                elif selected_option_01 == 2: 
                    new_bal_entry = input("\nEnter amount: ") 
                    bal_file = open("current_balance.txt", "r") 
                    existing_val = bal_file.read()
                    bal_file = open("current_balance.txt", "w")
                    if existing_val != "":
                        val = float(new_bal_entry) + float(existing_val)
                        bal_file.write(str(val) + "\n")
                    else:
                        bal_file.write(new_bal_entry)
                    bal_file = open("current_balance.txt", "r")
                    print("New balance: " + bal_file.read())
                    bal_file.close()
                    break
                elif selected_option_01 >3: 
                    print("\n######      Not an option. Try again.      ######")
                    continue 
        except ValueError: 
            print("\n######      Not an option. Try again.      ######")
            continue
        finally: 
            add_trans()
        break

def savings():
    while True: 
        try: 
            print("""
What would you like to do?
          
            1. View
            ---------
            2. Update
            ---------""")
            select_option_02 = int(input("\nEnter option: "))
            if select_option_02 == 1:
                view_savings = open("current_savings.txt", "r")
                print("\nBalance: " + view_savings.read())
                view_savings.close()
                thislist.append("savings")
                break
            elif select_option_02 == 2: 
                new_savings_entry = input("\n" + " Enter figure: ") 
                savings_file = open("current_savings.txt", "r")
                existing_savings = savings_file.read()        
                savings_file = open("current_savings.txt", "w")         
                if existing_savings != "":
                    savings_val = float(new_savings_entry) + float(existing_savings)
                    savings_file.write(str(savings_val) + "\n")
                else: 
                    savings_file.write(new_savings_entry)        
                savings_file = open("current_savings.txt", "r")
                print("\nNew savings: " + savings_file.read())
                savings_file.close()
                thislist.append("savings")
                break    
            elif select_option_02 >3: 
                print("\n######      Not an option. Try again.      ######")
                break
        except ValueError: 
            print("######      Not an option. Try again.      ######")
            continue 
        finally: 
            add_trans()
        break             
    
def add_trans(): 
    while True:
        try: 
                print("Would you like to do anything else?")
                print("""   
            1. Menu
            ----------------------
            2. Another transaction
            ----------------------
            3. Quit
            ----------------------""")
                more_trans = int(input("\n" + "Enter option: "))
                if more_trans == 1:   
                    menu()
                elif more_trans == 2:
                    if "balance" in thislist:
                        balance()                        
                    elif "savings" in thislist:
                        savings()
                elif more_trans == 3:
                    print("\nTransaction ID:", random.randint(0,9999))
                    break
                else:
                    print("\n######      Not an option. Try again.      ######")
                    continue
                break
        except ValueError:
            print("######      Not an option. Try again.      ######")
            continue 

while True: # main code that decides where user needs to go (this is where it is looping)
    try:
        option_chosen = menu()
        if option_chosen == 1:
            balance()
        elif option_chosen == 2: 
            savings()
        else: 
            print("\n######      Not an option. Try again.      ######")
            continue
    except ValueError:
        print("\n######      Not an option. Try again.      ######")
        continue
    break

Aucun commentaire:

Enregistrer un commentaire