[Python] Conditionals don't work with module values

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am making a simple banker minigame. Just deposit and withdraw money. I am working with 3 modules and 1 main file (bank.py).

    money.py

    bank_balance = 500.00*100
    wallet_balance = 500.00*100


    bank_deposit.py

    import money

    def bank_deposit():

    while True:
    try:
    deposit = float(input("Deposit: "))
    if deposit <= money.wallet_balance and deposit > 0:
    print(f"Deposited ${deposit}!")
    break
    elif deposit == 0:
    print("You can't deposit $0!")
    continue
    elif deposit < 0:
    print("How are you supposed to deposit a negative amount of money?\nC'mon, try again!")
    continue
    else:
    print(f"You haven't got enough money in your pockets! You only have {round(money.wallet_balance/100, 2)}.")
    continue
    except ValueError:
    print("Input must be a valid number!")


    money.bank_balance += deposit*100
    money.wallet_balance -= deposit*100

    print(f"Bank account balance: {round(money.bank_balance/100, 2)}")
    print(f"You have now ${round(money.wallet_balance/100, 2)} in your pockets.")


    bank_withdraw.py

    import money

    def bank_withdraw():

    while True:
    try:
    withdraw = float(input("Withdraw: "))
    if withdraw <= money.bank_balance and withdraw > 0:
    print(f"Withdrawed ${withdraw}!")
    break
    elif withdraw == 0:
    print("You can't withdraw $0!")
    continue
    elif withdraw < 0:
    print("How are you supposed to withdraw a negative amount of money?\nC'mon, try again!")
    continue
    else:
    print(f"You haven't got enough money in your bank account! You only have {round(money.bank_balance/100, 2)}")
    continue
    except ValueError:
    print("¡Input must be a valid number!")


    money.wallet_balance += withdraw*100
    money.bank_balance -= withdraw*100

    print(f"Bank account balance: {round(money.bank_balance/100, 2)}")
    print(f"You have now ${round(money.wallet_balance/100, 2)} in your pockets.")


    bank.py

    import money, bank_deposit, bank_withdraw

    print("Welcome to The Bank. Please, sit down. How may I serve you?\n")

    print("1. I want to check my balance.")
    print("2. I want to make a deposit.")
    print("3. I want to make a withdraw.")
    print("4. I've changed my mind (leave).")

    leave_bank = False

    while True:
    try:
    while True:
    banker_ans = int(input("\nChoose an option (1/2/3/4): "))

    if banker_ans == 1:
    print(f"Your bank account holds actually ${round(money.bank_balance/100, 2)}.") # Las cantidades están *100.
    print(f"And if you can see, there is ${round(money.wallet_balance/100, 2)} in your wallet.")
    print("\nIs there anything else I may help you with?")
    continue

    if banker_ans == 2 and money.wallet_balance < 0:
    print("\nI'm sorry. But you don't have any money to deposit")
    continue

    if banker_ans == 2:
    bank_deposit.bank_deposit()
    print("\nIs there anything else I may help you with?")
    continue

    if banker_ans == 3 and money.bank_balance < 0:
    print("\nI'm sorry. But your bank account is empty.")
    continue

    if banker_ans == 3:
    bank_withdraw.bank_withdraw()
    print("\nIs Anything else I may help you with?")
    continue

    if banker_ans == 4:
    print("Thank you for your visit. Please, come back soon!")
    leave_bank = True
    break

    else:
    print("Input a number from 1 to 4. Then click intro button.")
    continue

    if leave_bank:
    break


    except ValueError:
    print("First input a number, so THEN press the enter button.")



    The issue is with the last one. If you for example deposit your total and leave $0 in your wallet. If you press deposit (2), you will get into an endless loop. It's supposed to don't allow the user to press the 2nd option if there is no money in the wallet (and money.wallet_balance < 0). Anyone can explain to me what am I doing wrong?

    Also if anyone can explain to me the solution or how to fix it, I would be really grateful.

    PS: Same happens for the 3rd option if there is no money in the bank and you want to withdraw.

    Continue reading...

Compartilhe esta Página