python代码没有';t遍历整个循环并停留在原始输入
我目前正在尝试学习python,最近购买了一本关于初学者练习等的书。其中一个挑战是使用以下代码构建一个石头、布、剪刀游戏。但是,当我使用此代码时,我会被move函数卡住,在终端中输入move,它不会移动到“try”命令,只会提示我输入另一个数字。看起来程序好像卡在while循环中,不会比原始输入移动得更远。我试过几件事,但我似乎没办法控制住它。任何帮助都将不胜感激。谢谢python代码没有';t遍历整个循环并停留在原始输入,python,terminal,Python,Terminal,我目前正在尝试学习python,最近购买了一本关于初学者练习等的书。其中一个挑战是使用以下代码构建一个石头、布、剪刀游戏。但是,当我使用此代码时,我会被move函数卡住,在终端中输入move,它不会移动到“try”命令,只会提示我输入另一个数字。看起来程序好像卡在while循环中,不会比原始输入移动得更远。我试过几件事,但我似乎没办法控制住它。任何帮助都将不胜感激。谢谢 import random import time rock = 1 paper = 2 scissors = 3 nam
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
更新:似乎评论中的人可以很好地运行代码,但当我通过终端运行它时,我会不断得到。我现在已经通过我的室友MacBook运行了相同的代码,同样的问题仍然存在 您在函数
game()
result中输入了一个拼写错误。修复了game()函数中的拼写错误,并将原始输入更改为输入,以确保数据类型为int。但是循环仍然不会完全迭代,现在当我输入字符串而不是错误消息时,终端将结束程序。你知道我哪里出错了吗?你能把问题中的代码更新为你正在使用的当前代码吗?有问题的版本在game()函数中仍然具有result()上的类型。解决了这个问题,当前的代码对我来说运行良好。更新了代码。尝试在终端和空闲状态下运行,但仍不工作?我尝试了原始输入和输入。您的move
功能对我来说很好(使用input
或raw\u input
,尽管后者更擅长处理不适当的输入)。输入文本后是否按enter键?