代码有很长的路要走,但不能作为函数



我有一个骰子程序,我用两种不同的方式编程。有一种方法很长,那就是我重复了代码,而且它工作得很好。当我尝试将重复的代码放入函数中时,当双精度滚动时,程序将不再工作。这是有效的代码;


while True:
dice1 = int(random.randint(1, 6))
dice2 = int(random.randint(1, 6))
ask = input('Do you want to roll the dice (Y/N)? ')
if ask == 'Y'.lower():
print('Throws dice...')
time.sleep(1)
print(dice1 + dice2)
# if player rolls a double, they get to roll again
if dice1 == dice2:
print('You rolled doubles!', dice1, '&', dice2, 'Roll again')
time.sleep(0.5)
dice1 = int(random.randint(1, 6))
dice2 = int(random.randint(1, 6))
print('Throws dice...')
time.sleep(1)
print(dice1 + dice2)
# if the player rolls doubles a second time they get a third roll
if dice1 == dice2:
print('You rolled doubles for a second time!',
dice1, '&', dice2, 'Roll again')
time.sleep(0.5)
dice1 = int(random.randint(1, 6))
dice2 = int(random.randint(1, 6))
print('Throws dice...')
time.sleep(1)
print(dice1 + dice2)

正如你所看到的,选手在双打比赛中获得两个额外的掷骰子。在终端中,这看起来像这样:(还不能在我的个人资料上发布图像)

*你想掷骰子吗?y

掷骰子。。。

12

你打出了双打!6&6再次滚动

掷骰子。。。

6

你第二次获得双打!3&3再次滚动

掷骰子。。。

7*

在带有函数的程序中,它看起来是这样的;

def doubles_roll():
time.sleep(0.5)
dice1 = int(random.randint(1, 6))
dice2 = int(random.randint(1, 6))
print('Throws dice...')
time.sleep(1)
print(dice1 + dice2)
# while loop to maintain game as long as player indicates 'Y'

while True:
dice1 = int(random.randint(1, 6))
dice2 = int(random.randint(1, 6))
ask = input('Do you want to roll the dice (Y/N)? ')
if ask == 'Y'.lower():
print('Throws dice...')
time.sleep(1)
print(dice1 + dice2)
# if player rolls a double, they get to roll again
if dice1 == dice2:
print('You rolled doubles!', dice1, '&', dice2, 'Roll again')
doubles_roll()
# if the player rolls doubles a second time they get a third roll
if dice1 == dice2:
print('You rolled doubles for a second time!',
dice1, '&', dice2, 'Roll again')
doubles_roll()

这个代码与上面的代码完全相同,但有一个在掷完双骰子后掷骰子的功能。

然而,终端输出非常不同:

*你想掷骰子吗?y

掷骰子。。。4

你打出了双打!2&2再次滚动

掷骰子。

7

你第二次获得双打!2&2再次滚动

掷骰子。。。

9

你想掷骰子吗(是/否)?*

正如你在粗体中看到的,骰子的值在使用函数时永远不会改变。

与Macko建议的完全一样),函数doubles_roll()中的变量dice1dice2不能在生成它们的函数之外访问,要使用它们,必须将它们返回给函数调用方。

为此,您必须在函数doubles_roll()的末尾放一个返回,指定生成的两个变量。

以下是该功能的外观:

def doubles_roll():
time.sleep(0.5)
dice1 = int(random.randint(1, 6))
dice2 = int(random.randint(1, 6))
print('Throws dice...')
time.sleep(1)
print(dice1 + dice2)
return dice1, dice2
while True:
dice1 = int(random.randint(1, 6))
dice2 = int(random.randint(1, 6))
ask = input('Do you want to roll the dice (Y/N)? ')
if ask == 'Y'.lower():
print('Throws dice...')
time.sleep(1)
print(dice1 + dice2)
# If a player rolls a double, they get to roll again
if dice1 == dice2:
print('You rolled doubles!', dice1, '&', dice2, 'Roll again')
dice1, dice2 = doubles_roll()
# If a player rolls doubles a second time they get a third roll
if dice1 == dice2:
print('You rolled doubles for a second time!',
dice1, '&', dice2, 'Roll again')
dice1, dice2 = doubles_roll()

这是因为变量dice1dice2是函数的局部变量,所以无论何时比较dice1 == dice2,实际上都是在比较第3行和第4行的原始滚动结果。您需要做的是从函数返回滚转值:

def doubles_roll():
time.sleep(0.5)
dice1 = int(random.randint(1, 6))
dice2 = int(random.randint(1, 6))
print('Throws dice...')
time.sleep(1)
return dice1, dice2

然后将这些值分配给主函数中的原始变量:

dice1, dice2 = doubles_roll()

相关内容

最新更新