【发布时间】:2016-09-20 23:01:19
【问题描述】:
我对 Python 非常陌生,因此决定为自己设定一个挑战,即在不复制他人代码的情况下编写 Rock、Paper、Scissors 游戏。但是,我需要 Pythonista 大人的帮助!
我在这里看到了许多其他关于 Rock、Paper、Scissors 的变体,但没有什么可以解释为什么我的版本不起作用。我的程序基本上遵循这种格式:在开始时设置空变量,定义打印介绍文本的 4 个函数,接收玩家输入,随机选择计算机的选择,然后评估玩家的胜利或失败。
这一切都被困在一个while循环中,一旦玩家选择他们不想再玩,就会中断。 (这个位工作正常)
但是,每当我运行代码时,它总是给出一个平局,并且似乎没有为计算机的选择函数调用存储任何数据。有人知道我做错了什么吗?
非常感谢!
import random
playerAnswer = ''
computerAnswer = ''
winsTotal = 0
timesPlayed = 0
def showIntroText():
print('Time to play Rock, Paper, Scissors.')
print('Type in your choice below:')
def playerChoose():
playerInput = input()
return
def computerChoose():
randomNumber = random.randint(1, 3)
if randomNumber == 1:
computerPick = 'Paper'
elif randomNumber == 2:
computerPick = 'Scissors'
else:
computerPick = 'Rock'
return
def assessResult():
if playerAnswer == computerAnswer:
print('Draw!')
elif playerAnswer == 'Rock' and computerAnswer == 'Paper':
print('Paper beats Rock. You lose!')
elif playerAnswer == 'Paper' and computerAnswer == 'Scissors':
print('Scissors cuts Paper. You lose!')
elif playerAnswer == 'Scissors' and computerAnswer == 'Rock':
print('Rock blunts Scissors. You lose!')
else:
print('You win!')
winsTotal += 1
return
while True:
timesPlayed += 1
showIntroText()
playerAnswer = playerChoose()
computerAnswer = computerChoose()
assessResult()
print('Do you want to play again? (y/n)')
playAgain = input()
if playAgain == 'n':
break
print('Thank you for playing! You played ' + str(timesPlayed) + ' games.')
【问题讨论】:
-
您的
playerChoose和computerChoose函数不会返回任何内容。 -
i.o.w.
return,但是呢? -
在 playerChoose() 中添加 'return playerInput' 而不是仅返回。
-
在computerChoose()中添加'return computerPick'而不是return。
-
@redrackham - 请检查更新的代码。我已更新以计算玩家赢得的游戏数
标签: python