我刚开始学习Python,我正在尝试为我的作业编写基本的Rock Paper Scissors程序。该游戏旨在持续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
那么在不中断while循环的情况下如何使函数在输入无效输入后再次请求正确的raw_input?
非常感谢您的解释,谢谢
答案 0 :(得分:2)
你有一个局部变量get_player_move
函数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
中以供进一步比较。