gpt4 book ai didi

python - Python 石头剪刀布函数帮助

转载 作者:行者123 更新时间:2023-12-01 05:20:51 25 4
gpt4 key购买 nike

我刚刚开始学习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 循环?

非常感谢您的解释,谢谢

最佳答案

你有一个局部变量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 中以便进一步比较。

关于python - Python 石头剪刀布函数帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22430926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com