亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何在 DEF 語句之外使用 DEF 語句內部的值?

如何在 DEF 語句之外使用 DEF 語句內部的值?

泛舟湖上清波郎朗 2023-08-08 15:59:18
我遇到這個問題有一段時間了,似乎找不到任何有效的方法。我怎樣才能得到它,以便當前空間增加滾動總量。def roll():    import random    doubles = 0    x = random.randint(1, 6)    y = random.randint(1, 6)    print(x, y)    if x == y:        print('Doubles')        doubles = 1        print(f'You have rolled {doubles} doubles!')        x1 = random.randint(1, 6)        y1 = random.randint(1, 6)        print(x1, y1)        if x1 == y1:            print('Doubles')            doubles = 2            print(f'You have rolled {doubles} doubles!')            x2 = random.randint(1, 6)            y2 = random.randint(1, 6)            print(x2, y2)            if x2 == y2:                doubles = 3                print(f'You rolled {doubles} doubles!')                print('Go to Jail!')            else:                total2 = x + y + x1 + y1 + x2 + y2                print(f'You rolled {total2} in total.')        else:            total1 = x + y + x1 + y1            print(f'You rolled {total1} in total.')    else:        total = x + y        print(f'You rolled {total} in total.')        currentspace = 0print(roll())
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

使用global聲明。

def roll():
    global currentspace
    ...do your things...
    currentspace += 1


查看完整回答
反對 回復 2023-08-08
  • 1 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號