猿问

如何在 DEF 语句之外使用 DEF 语句内部的值?

我遇到这个问题有一段时间了,似乎找不到任何有效的方法。我怎样才能得到它,以便当前空间增加滚动总量。


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 = 0


print(roll())


泛舟湖上清波郎朗
浏览 101回答 1
1回答

明月笑刀无情

使用global声明。def roll():     global currentspace     ...do your things...     currentspace += 1
随时随地看视频慕课网APP

相关分类

Python
我要回答