Python新手 - 摇滚,纸,剪刀

时间:2016-09-13 10:20:16

标签: python

我对Python非常陌生,并决定自己设定一个Rock,Paper,Scissors游戏编程的挑战,而不会复制别人的代码。但是,我需要Pythonista成年人的帮助!

我在Rock,Paper,Scissors上看到了很多其他的变化,但是没有什么可以解释为什么我的版本不起作用。我的程序基本上遵循以下格式:在开始时设置空变量,定义打印介绍文本的4个函数,接收玩家输入,随机选择计算机的选择,然后评估玩家是赢还是输。

然后,当玩家选择他们不想再玩时,这一切都会停留在一个中断循环中。 (这一点工作正常)

然而,每当我运行代码时,它总是给出一个平局,并且似乎不存储计算机选择函数调用的任何数据。有人知道我做错了吗?

非常感谢!

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

4 个答案:

答案 0 :(得分:2)

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

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

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

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

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

请检查此StackOverFlow link 了解python中变量的范围

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

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

答案 1 :(得分:2)

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

答案 2 :(得分:2)

在函数中添加输入和返回

def computerChoosedef assessResult返回无

例如,您可以通过此代码玩这个游戏:

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 :(得分:1)

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

修改这些声明的玩家选择() - > return playerInput 和computerChoose() - >返回computerPick

同样你必须使用全局变量。插入此行

{"a", "b", "c"}
在assessmentResult()中。