- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚开始学习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/
如何让玩家玩随机数量的比赛,然后以全部分数结束游戏? 我正在尝试做一个循环,让玩家玩多场比赛,并在最后询问是否还想玩。 public class RockPaperScissors { pub
我的石头剪刀布游戏遇到问题。当我玩游戏时,它会将胜利或失败记录为胜利和失败。我试图让它记录一场胜利和一场失败,并且不会为胜利和失败添加分数。有什么建议吗? import java.io.*; publ
#RoShamBo import random count=0 while count -2: compnum=random.randint(0,2) usernum=int(inpu
这是我的石头剪刀布游戏。 http://jsfiddle.net/Renay/hL3j5hm6/6/ 如何添加动画,在给出结果之前,图像上下弹跳时有 3,2,1 倒计时。我尝试过添加各种功
我的类(class)本周将举行机器人竞赛,我们应该让我们的机器人战胜其他人的机器人。比赛项目为石头、布、剪刀、炸药、水气球。炸药胜过一切,除了水气球,而水气球只胜过炸药。老师写了竞技场,还有其他一些机
我刚刚开始使用 python,需要一些帮助!我当时在做剪刀石头布游戏,我想在人或计算机赢得 3 场胜利后添加一个重启选项。 我已经四处寻找一些答案,但从我看到的所有其他代码来看,似乎超出了我的范围,或
我正在使用 Python 创建一个非常简单的石头剪刀布游戏,但无法解决这个问题。每次我在命令提示符中输入答案时,它都会输出消息“无效输入,请重试!”,这就是我告诉它在存在无效输入时执行的操作。但是,我
我需要实现一个石头剪刀布锦标赛模拟器,它将玩完所有回合并返回锦标赛。 这是我的锦标赛数组: tournament = [ [ [ ["Armando",
我遇到了一个问题,选择根本没有更新。我已经列出了每次选择后应显示最后结果和当前分数的位置。分数运行良好,但选择根本没有更新。谢谢。 Dynamic Web Squirtle,
我的图片和消息正在更改为显示获胜、失败、平局,但我的 javascript 增量似乎不起作用,因为我的分数没有改变。请帮忙:) Rock, Paper, Scissors Rock
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我希望代码获取用户在输入字段中输入的值并将其传递给变量 userChoice。我不知道为什么这段代码不起作用,唯一的学习方法就是问你们。 HTML: Choose your destiny !
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我的类(class)被分配了一个石头剪刀布游戏。以下是作业说明: 学习目标: 练习枚举的使用 创建一个包含构造函数、字段和方法的枚举 设计并实现您自己的 GUI 创建一个可运行的 jar 描述:编写一
我正在用Python创建一个石头剪刀布游戏。我的方法如下。然而,我不想打印玩家 1 和玩家 2,而是想让它们成为真实的名字。因此,可以说 John Wins 或 Joe Wins。如何实现每次打印玩家
我正在尝试完成类里面的石头、剪刀、布作业。 我收到“UnboundLocalError:赋值前引用的局部变量“绑定(bind)”” 错误。 有人可以告诉我为什么会收到此错误吗? import rand
我在 C++ 上学习石头剪刀布游戏时有一个游戏实例类。我想创建整数常量,它表示游戏中允许的符号数。对于经典的 rsp 游戏,它是 3(石头、剪刀和布),但是有一些有趣的 rcs 游戏扩展带有额外的符号
好吧,有件事一直困扰着我,但我一直找不到答案。这是 Codeacademy 的石头剪刀布游戏: var userChoice = prompt("Do you choose rock, paper o
我正在可汗学院编写一个剪刀石头布游戏,这样我就可以看到视觉效果,但是 var Compare = function(choice1, choice2) 无法正常工作。 html 它工作得很好。插入了我
我制作了一个基本的“石头、剪刀、布”游戏。我对这个项目有一些疑问/问题。 在我的浏览器上,不显示谁获胜的消息。如“计算机获胜”。我得到的结果如下: Computer: Paper You: rock
我是一名优秀的程序员,十分优秀!