2016-05-29 19 views
0

我是新來的Python,試圖在我的學校課程之外選擇語言。這岩石紙剪刀遊戲我正在正常工作,雖然分數輸出不顯示任何東西。這裏是代碼...在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() 
+0

對我而言,這是否正常工作,無論我是否退出遊戲並查看最終分數,還是我在每次迭代中是否要再次播放時調用'scores()'。我不能在2.7中重現這個問題。 – roganjosh

回答

1

您的print聲明是有點關閉。 你應該包括print語句中的參數

print("Player: ", player_score)

編輯: 要說話多一點的打印。你也可以使用 print("Player: {}".format(player_score))

+0

這是Python 3嗎?使用括號的Python 2.7工作正常,我找不到問題,但我認爲他們需要3.x'print'? – roganjosh

+0

謝謝你的幫助,這個工作,我現在看到了不同! – slickset

+0

我正在使用Python 3.5,但我正在使用的代碼項目利用Python 2.7 – slickset

1

您需要括號所以裏面的變量:

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

格式是更好地使用作爲t這裏有更多你可以用它做的事情(我會讓你自己找出來)!