python代码不会遍历整个循环并停留在raw_input

时间:2017-07-12 21:10:53

标签: python terminal

我目前正在尝试学习python并且最近购买了一本初学者练习书。其中一个挑战是使用以下代码构建摇滚,纸张,剪刀游戏。当我使用这个代码时,我陷入了移动功能,在终端我输入一个移动,它没有移动到'try'命令,只是提示我输入另一个数字。看起来程序停留在while循环中,并且不会比raw_input更进一步。我尝试了一些东西,但我似乎无法理解它。任何帮助将不胜感激。谢谢!

import random
import time

rock = 1
paper = 2
scissors = 3

names = {rock: "Rock", paper:"Paper", scissors:"Scissors"}
rules = {rock:scissors, paper:rock, scissors:paper}

player_score = 0
computer_score = 0

def start():
    print
    print "Let's play Rock, Paper, Scissors"
    while game():
        pass
    scores()

def game():
    player = move()
    computer = random.randint(1, 3)
    result(player, computer)
    return play_again()

def move():
    while True:
        print
        player = input("Rock = 1\nPaper = 2\nScissors = 3\nMake a Move: ")
        try:
            player = int(player)
            if player in (1,2,3):
                return player
        except ValueError:
            pass
        print "Oops! I didn't understand that. Please enter 1, 2, or 3"

def result(player, computer):
    print "1..."
    time.sleep(1)
    print "2..."
    time.sleep(1)
    print"3!"
    time.sleep(0.5)
    print "Computer threw {0}!".format(names[computer])
    global player_score, computer_score
    if player == computer:
        print"Tie Game."
    else:
        if rules[player] == computer:
            print "Your victory has been assured!"
            player_score += 1
        else: 
            print "The computer is victorious"
            computer_score += 1

def play_again():
    answer = raw_input("Would you like to play again? y/n")
    if answer in ("y", "yes", "Y", "Yes"):
        return answer
    else: 
        print "Thank you for playing!"

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

更新:似乎评论中的人可以正常运行代码,但是当我通过终端运行时,我不断获得this。我现在通过我的室友MacBook运行相同的代码,同样的问题仍然存在。

0 个答案:

没有答案