Python中的剪刀石头布函数帮助
我刚开始学习Python,正在为我的作业写一个简单的石头剪刀布程序。这个游戏计划进行10轮,并记录玩家和电脑之间的得分。不过,我遇到了两个具体的问题。
import random
def welcome_prompt():
print ("ROCKER PAPER SCISSORS in PYTHON Assignment")
print ("Rules: Rocks beats Scissors, Scissors beats Paper, Paper beats Rock")
def get_player_move():
print ('Round ' + str(round))
print ("Please play one of the following")
get_player_move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")
if get_player_move == ("R"):
print ("You used Rock!")
return 1
elif get_player_move == ("P"):
print ("You used Paper!")
return 2
elif get_player_move == ("S"):
print ("You used Scissors!")
return 3
else:
print "Invalid input, please use capitalized initial (R,P,S)"
return get_player_move()
def get_computer_move():
get_computer_move = random.randint(1,3)
if get_computer_move == 1:
print ("Computer used Rock!")
return 1
elif get_computer_move == 2:
print ("Computer used Paper!")
return 2
elif get_computer_move == 3:
print ("Computer used Scissors!")
return 3
def compare_moves(get_player_move, get_computer_move):
# Rock = 1
# Paper = 2
# Scissors = 3
if (get_player_move == 1 and get_computer_move == 1) or (get_player_move == 2 and get_computer_move == 2) or (get_player_move == 3 and get_computer_move == 3):
print ("It's a tie!")
return 0
elif (get_player_move == 1 and get_computer_move == 3) or (get_player_move == 2 and get_computer_move == 1) or (get_player_move == 3 and get_computer_move == 2):
print ("You win the round!")
return 1
elif (get_player_move == 1 and get_computer_move == 2) or (get_player_move == 2 and get_computer_move == 3) or (get_player_move == 3 and get_computer_move == 1):
print ("You lose the round!")
return -1
elif (get_player_move == 4):
print ("You didn't put in correct input, computer gets a free win")
return -1
# Game Program
player_score = 0
comp_score = 0
round = 0
welcome_prompt()
('Round ' + str(round))
while round< 10:
round = round + 1
get_player_move()
get_computer_move()
compare_moves(get_player_move, get_computer_move)
if compare_moves == 1:
player_score = player_score + 1
print 'Player Score'+ str(player_score)
print 'Computer Score'+ str(player_score)
elif compare_moves == -1:
comp_score = comp_score + 1
print 'Player Score'+ str(player_score)
print 'Computer Score'+ str(player_score)
print "Game Over"
首先,我无法让compare_move函数正确获取get_player_move和get_computer_move返回的值。虽然游戏可以运行而没有错误,但它完全跳过了比较和得分的部分。我对基础知识还有点不太熟悉,所以不太确定缺少了什么。
其次,在get_player_move函数中,当我输入一个无效的内容(比如:blah)来测试raw_input时,它会报错。
Traceback (most recent call last):
File "C:\Python27\Rock Paper Scissors.py", line 85, in <module>
get_player_move()
File "C:\Python27\Rock Paper Scissors.py", line 32, in get_player_move
return get_player_move()
TypeError: 'str' object is not callable
那么,如何让一个函数在输入无效内容后,能够再次请求正确的raw_input,而不打断while循环呢?
非常感谢你的解释!
1 个回答
你在函数 get_player_move()
里面有一个局部变量 get_player_move
;所以你不能再用这个函数的名字(全局的)了。
你需要给这个局部变量换个名字。
所以,不要这样:
get_player_move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")
要这样:
move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")
可能是这样的。
获取用户输入时,最好不要依赖递归。因为用户可能一直按 'C',这样你的程序就会因为 RuntimeError: maximum recursion depth exceeded
而崩溃。用循环会更简单:
while True:
move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")
if move == "R":
print ("You used Rock!")
return 1
# etc.
else:
print "Invalid input, please use capitalized initial (R,P,S)"
当你在函数里做出正确选择时,会返回并自动退出循环。如果到最后打印出 Invalid input
,那么 while True
循环会重新从头开始,用户会再次被要求输入选择。
接下来:虽然你的函数返回了一个选择(一个整数),但你从来没有保存这个返回值。你必须在调用函数的地方保存它:
player_move = get_player_move()
computer_move = get_computer_move()
result = compare_moves(player_move, computer_move)
if result == 1:
注意,保存返回值的不是函数名,而是一个单独的变量。例如,player_move
被赋值为 get_player_move()
返回的内容。
然后你可以把这些返回的值传给 compare_moves()
;它也会返回一个结果,这里存储在 result
里以便后续比较。