在 def 之外赋予 balance 新值

退出循环时如何获得 balance 的新值?现在它只取原始值并打印它。我也试过 balance += (balance + dep)


balance = 500

def main(balance):

    if balance <= 999999:

        intrest = 0.01

    if balance >= 1000000:

        intrest = 0.02

    menu = int(input('Press 1 for balance, 2 for deposit, 3 for withdrawal or 4 for interest return: '))

    if menu == 1:

        print(balance)

    elif menu == 2:

        dep = int(input('How much do you want to deposit? '))

        balance = (balance + dep)

        print('Your new balance is', balance)

        if balance <= 999999:

            intrest = 0.01

        if balance >= 1000000:

            intrest = 0.02

            print('Congratulations, your intrest just went up!')

    elif menu == 3:

        wit = int(input('How much do you want to withdraw? '))

        balance = (balance - wit)

        print('Your new balance is', balance)

        if balance <= 999999:

            intrest = 0.01

        print('Your intrest is now back to standard!')

    elif menu == 4:

        intrest_return = balance + (balance * intrest)

        print(intrest_return)

    while True:

        restart = str(input('Would you like to do more? Press y for yes or n for no: '))

        if restart == 'y':

            main(balance)

        else:

            break

main(balance)

print(balance)


BIG阳
浏览 95回答 2
2回答

三国纷争

您应该return在代码中使用 a ,然后将其分配给您的变量。这将是您的代码:balance = 500def main(balance):&nbsp; &nbsp; if balance <= 999999:&nbsp; &nbsp; &nbsp; &nbsp; intrest = 0.01&nbsp; &nbsp; if balance >= 1000000:&nbsp; &nbsp; &nbsp; &nbsp; intrest = 0.02&nbsp; &nbsp; menu = int(input('Press 1 for balance, 2 for deposit, 3 for withdrawal or 4 for interest return: '))&nbsp; &nbsp; if menu == 1:&nbsp; &nbsp; &nbsp; &nbsp; print(balance)&nbsp; &nbsp; elif menu == 2:&nbsp; &nbsp; &nbsp; &nbsp; dep = int(input('How much do you want to deposit? '))&nbsp; &nbsp; &nbsp; &nbsp; balance = (balance + dep)&nbsp; &nbsp; &nbsp; &nbsp; print('Your new balance is', balance)&nbsp; &nbsp; &nbsp; &nbsp; if balance <= 999999:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intrest = 0.01&nbsp; &nbsp; &nbsp; &nbsp; if balance >= 1000000:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intrest = 0.02&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Congratulations, your intrest just went up!')&nbsp; &nbsp; elif menu == 3:&nbsp; &nbsp; &nbsp; &nbsp; wit = int(input('How much do you want to withdraw? '))&nbsp; &nbsp; &nbsp; &nbsp; balance = (balance - wit)&nbsp; &nbsp; &nbsp; &nbsp; print('Your new balance is', balance)&nbsp; &nbsp; &nbsp; &nbsp; if balance <= 999999:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intrest = 0.01&nbsp; &nbsp; &nbsp; &nbsp; print('Your intrest is now back to standard!')&nbsp; &nbsp; elif menu == 4:&nbsp; &nbsp; &nbsp; &nbsp; intrest_return = balance + (balance * intrest)&nbsp; &nbsp; &nbsp; &nbsp; print(intrest_return)&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; restart = str(input('Would you like to do more? Press y for yes or n for no: '))&nbsp; &nbsp; &nbsp; &nbsp; if restart == 'y':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; main(balance)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; return balancebalance=main(balance)print(balance)

慕田峪7331174

添加global balance函数的第一行。除非您这样做,否则在函数内分配的任何变量都被视为本地变量,这就是您收到错误的原因。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python