python-3.x 条件语句不适用于模块值

k4emjkb1  于 2023-03-31  发布在  Python
关注(0)|答案(1)|浏览(127)

我正在制作一个简单的银行家迷你游戏。只需存款和取款。我正在使用3个模块和1个主文件(bank.py)。
money.py

bank_balance = 500.00*100
wallet_balance = 500.00*100

银行存款.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 ammount 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 ammount 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.")

问题出在最后一个选项上。例如,如果你存款了钱,钱包里剩下0美元。如果你按了deposit(2),你会陷入一个死循环。如果钱包里没有钱(并且money.wallet_balance〈0),它应该不允许用户按第二个选项。有人能告诉我我做错了什么吗?
另外,如果有人能向我解释解决方案或如何修复它,我将非常感激。
PS:同样的情况发生在第三个选项,如果没有钱在银行,你想撤回。

a8jjtwal

a8jjtwal1#

这里你有〉或〈put = like〉=或〈=。
bank_withdraw.py -第8和14行
bank_存款.py -第8和14行
bank.py - 第24和33行

相关问题