gpt4 book ai didi

Python 新手 - 剪刀石头布

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:40 25 4
gpt4 key购买 nike

我是 Python 的新手,因此决定给自己设定一个挑战,即在不复制他人代码的情况下编写剪刀石头布游戏。但是,我需要一个 Pythonista 成年人的帮助!

我在这里看到了剪刀石头布的许多其他变体,但没有任何内容可以解释为什么我的版本不起作用。我的程序基本上遵循这种格式:在开始时设置空变量,定义 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() 中添加“return playerInput”,而不仅仅是 return。

** 在 computerChoose() 中添加“return computerPick”而不是 return。

** 在 assessResult() 中将其用作 'winsTotal = 0' 之前初始化 winsTotal 变量。

** 您在程序开始时初始化的变量超出了函数的范围。

请检查这个StackOverFlow link 了解 python 中变量的作用域

** 在 assessResult() 中添加 'return winsTotal' 而不是 return。

import random

def showIntroText():
print('Time to play Rock, Paper, Scissors.')
print('Type in your choice below:')

def playerChoose():
playerInput = input()
return playerInput

def computerChoose():
randomNumber = random.randint(1, 3)
if randomNumber == 1:
computerPick = 'Paper'
elif randomNumber == 2:
computerPick = 'Scissors'
else:
computerPick = 'Rock'
return computerPick

def assessResult(winsTotal):
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 winsTotal


total_win = 0
while True:

timesPlayed += 1

showIntroText()

playerAnswer = playerChoose()
computerAnswer = computerChoose()

total_win = assessResult(total_win)

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.' + 'Out of which you won '+ str(total_win))

输出:

   C:\Users\dinesh_pundkar\Desktop>python c.py
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
You win!
Do you want to play again? (y/n)
"y"
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
Draw!
Do you want to play again? (y/n)
"y"
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
Paper beats Rock. You lose!
Do you want to play again? (y/n)
"y"
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
Paper beats Rock. You lose!
Do you want to play again? (y/n)
"n"
Thank you for playing! You played 4 games.Out of which you won 1

关于Python 新手 - 剪刀石头布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39467619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com