第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 def 之外賦予 balance 新值

在 def 之外賦予 balance 新值

BIG陽 2022-05-24 15:29:59
退出循環(huán)時如何獲得 balance 的新值?現(xiàn)在它只取原始值并打印它。我也試過 balance += (balance + dep)balance = 500def 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:            breakmain(balance)print(balance)
查看完整描述

2 回答

?
三國紛爭

TA貢獻1804條經(jīng)驗 獲得超7個贊

您應(yīng)該return在代碼中使用 a ,然后將其分配給您的變量。這將是您的代碼:


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

    return balance

balance=main(balance)

print(balance)


查看完整回答
反對 回復(fù) 2022-05-24
?
慕田峪7331174

TA貢獻1828條經(jīng)驗 獲得超13個贊

添加global balance函數(shù)的第一行。

除非您這樣做,否則在函數(shù)內(nèi)分配的任何變量都被視為本地變量,這就是您收到錯誤的原因。


查看完整回答
反對 回復(fù) 2022-05-24
  • 2 回答
  • 0 關(guān)注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號