【问题标题】:Scoring in Python game does not workPython游戏中的评分不起作用
【发布时间】:2016-09-27 13:13:44
【问题描述】:

我是 Python 新手,正在尝试在学校课程之外学习这门语言。虽然分数输出没有显示任何内容,但我正在正确处理这个石头剪刀布游戏。这是代码...

 #!/usr/bin/env python2

# Extra modules used
import random
import time

# Set each move to a specific number
# Once a selection is made by the player,
# it will be equated to that specific variable.
rock = 1
paper = 2
scissors = 3

# Text representation of each move
names = { rock: "Rock", paper: "Paper", scissors: "Scissors" }

# Game Rules
rules = { rock: scissors, paper: rock, scissors: paper }

# Declare variables to be used to track scoring
player_score = 0
computer_score = 0

# Function to print a greeting and start
# a loop to allow the player to continue
#playing as many times as they wish
def start():
    print ("Let's play a game of Rock, Paper, Scissors.")
    while game():
        pass # Allows the loop to stop when player is done
    scores() # Call function when done playing

def game():
    # Call move function to determine player move
    player = move()
    # Get computer move as random int between 1 and 3
    computer = random.randint(1, 3)
    # Send the move through the result function
    result(player, computer)
    return play_again()

# Function to obtain a move from the player
def move():
    while True:
        print
        player = input("Rock = 1\nPaper = 2\nScissors = 3\nMake a move: ")
        # Try to set the player move, or catch the error
        try:
            # Cast the user input as an integer
            player = int(player)
            # If entry is valid, set the variable
            if player in (1,2,3):
                return player
        except ValueError:
            pass
        print ("Oops! I didn't understand that. Please enter 1, 2, or 3.")

# Function to determine the result of the game
# player move and computer move are passed in
def result(player, computer):
    # Countdown to result display
    print ("1...")
    time.sleep(1)
    print ("2...")
    time.sleep(1)
    print("3!")
    time.sleep(0.5)
    # Display the computer's move
    # string.format() gets the text version
    # of the move and inserts it where "0"
    print ("Computer threw {0}!".format(names[computer]))
    #Call the scores set earlier
    global player_score, computer_score
    # Check the results of the game
    if player == computer:
        print ("Tie game.")
    # Check if the losing move to the player's move
    # is equal to the computer's move
    elif rules[player] == computer:
        print ("Your victory has been assured.")
        player_score += 1
    else:
        print ("The computer laughs as you realize you have been defeated.")
        computer_score += 1

# Ask to play again
def play_again():
    answer = input("Would you like to play again? y/n: ")
    if answer in ("y", "Y", "yes", "Yes", "Of course!"):
        return answer
    else:
        print ("Thank you very much for playing. See you next time!")

def scores():
    global player_score, computer_score
    print ("HIGH SCORES")
    print ("Player: "), player_score
    print ("Computer: "), computer_score

# Used to execute in command line or import
# into another Python script. This will prevent
# the code from being executed when being imported.
if __name__ == '__main__':
    start()

【问题讨论】:

  • 这对我来说很好用,无论我退出游戏并查看最终分数,还是在每次迭代时调用scores() 来确定我是否想再次玩。我无法在 2.7 中重现该问题。

标签: python function variables syntax global


【解决方案1】:

您的print 声明有点不对劲。 您应该在 print 语句中包含参数

print("Player: ", player_score)

编辑: 多说一点印刷。你也可以使用 print("Player: {}".format(player_score))

【讨论】:

  • 这是 Python 3 的版本吗?在带有括号的 Python 2.7 上工作正常,我找不到问题,但我认为 3.x print? 需要它们
  • 感谢您的帮助,这有效,我现在看到了不同之处!
  • 我使用的是 Python 3.5,但我使用的代码项目使用的是 Python 2.7
  • print as a statement is python3 (py2 with futures).
【解决方案2】:

您需要括号内的变量:

print ("Player: "), player_score print ("Computer: "), computer_score

变成

print ("Player: ", player_score)
print ("Computer: ", computer_score)

或者,

print ("Player: {}".format(player_score))
print ("Computer: {}".format(computer_score))

格式更好用,因为你可以用它做更多事情(我会让你自己发现)!

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多