2016-05-29 2 views
0

Я новичок в Python, пытаясь забрать язык за пределами моих школьных курсов. Эта игра Rock Paper Scissors. Я правильно работаю над функциями, хотя результат не показывает ничего. Вот код ...Оценка в игре 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

Это отлично работает для меня, выходит ли я из игры и просматриваю итоговые оценки, или я называю 'score()' на каждой итерации, хочу ли я снова играть. Я не могу воспроизвести проблему в 2.7. – roganjosh

ответ

1

Ваши заявления print немного неактивны. Вы должны быть в том числе аргументы внутри print заявления

print("Player: ", player_score)

Edit: Чтобы говорить немного больше печати. Вы можете также использовать 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 здесь намного больше вы можете сделать с ним (я дам вам это узнать самостоятельно)!

Смежные вопросы