【问题标题】:Python Newbie - Rock, Paper, ScissorsPython新手 - 石头,纸,剪刀
【发布时间】: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.')

【问题讨论】:

  • 您的 playerChoosecomputerChoose 函数不会返回任何内容。
  • i.o.w. return,但是呢?
  • 在 playerChoose() 中添加 'return playerInput' 而不是仅返回。
  • 在computerChoose()中添加'return computerPick'而不是return。
  • @redrackham - 请检查更新的代码。我已更新以计算玩家赢得的游戏数

标签: python


【解决方案1】:

在大多数情况下,您都错过了返回值。

** 在 playerChoose() 中添加 'return playerInput ' 而不是只返回。

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

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

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

请查看StackOverFlow link 以了解python 中变量的范围

** 在 assesResult() 中添加 '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

【讨论】:

  • 谢谢,这非常有用,因为它随后出现了一个关于初始化我正在努力解决的 winTotal 变量的错误。
  • 虽然,它似乎只在我一开始初始化“timesPlayed = 0”变量时才起作用。否则运行时会报错。
【解决方案2】:

在你的函数中添加输入和返回

def computerChoosedef assessResultreturn 无

例如通过这个代码你可以玩这个游戏:

import random

playerAnswer = ''
computerAnswer = ''
winsTotal = 0
timesPlayed = 0


def playerChoose():
    playerInput = input("insert:")
    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(playerAnswer, computerAnswer):
    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!')
    return


while True:
    timesPlayed += 1

    playerAnswer = playerChoose()
    computerAnswer = computerChoose()

    assessResult(playerAnswer,computerAnswer)

    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.')

【讨论】:

    【解决方案3】:

    这总是平局,因为你没有从你的函数中返回答案,playerAnswer 和 computerAnswer 都返回 None

    【讨论】:

    • 谢谢。我是新手,所以能向我展示这样的东西是件好事。
    【解决方案4】:

    正如一些人所说, playerChoose() 和 computerChoose() 返回 None

    修改这些语句 playerChoose() -> return playerInput 和 computerChoose() -> 返回 computerPick

    你必须使用全局变量。插入这一行

    global winsTotal
    

    在评估结果()中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    相关资源
    最近更新 更多