python中的while循环、输入和字符串有问题
我正在学习python并练习制作一个简单的基于文本的冒险游戏的技能 在游戏中,我想问玩家他们是否准备好开始了。为此,我创建了一个begin()函数:python中的while循环、输入和字符串有问题,python,string,input,while-loop,user-input,Python,String,Input,While Loop,User Input,我正在学习python并练习制作一个简单的基于文本的冒险游戏的技能 在游戏中,我想问玩家他们是否准备好开始了。为此,我创建了一个begin()函数: def begin(): print(raw_input("Are you ready to begin? > ")) while raw_input() != "yes": if raw_input() == "yes": break print(st
def begin():
print(raw_input("Are you ready to begin? > "))
while raw_input() != "yes":
if raw_input() == "yes":
break
print(start_adventure())
else:
print("Are you ready to begin? > ")
print(begin())
def begin():
while something:
raw_input("Are you ready to begin? > ")
start_adventure()
下面是我的代码中的函数start\u advention()
当我运行程序时,它会启动,我会问我是否准备好开始。然后它无限循环,只有完全关闭Powershell并重新启动Powershell,我才能退出程序。我做错了什么?玩家输入“是”后,如何让循环停止?Python 3解决方案 您不应该多次调用
raw\u input()
。只需实例化x
,然后等待用户输入Y
即可调用start\u冒险
函数。这应该让你开始:
def start_adventure():
print('We have started!')
#do something here
def begin():
x = None
while x!='Y':
x = input('Are you ready to begin (Y/N)?')
if x=='Y':
start_adventure()
begin()
//initialize the condition to no value
condition = None;
#check the condition
while condition != "yes"
#change the condition here based on user input **inside the loop**
condition = raw_input("are you ready to begin? >")
if condition == "yes":
#condition is met do what you need
else:
#condition not met loop again
#nothing needs to go here to print the message again
你希望这能做什么?解决问题的办法是试着理解代码的作用,而不是把东西拼凑在一起。(别担心,我们中至少有80%的人曾经处于这个阶段!) 另外,我强烈建议使用Python3而不是Python2;他们制作了一个新版本的Python,因为Python 2充满了非常奇怪、令人困惑的东西,比如导致安全漏洞的
input
,以及10/4
等于2
你想让它做什么
- 反复询问用户是否准备好开始,直到他们回答“是”
- 调用
开始冒险()
def begin():
print(raw_input("Are you ready to begin? > "))
while raw_input() != "yes":
if raw_input() == "yes":
break
print(start_adventure())
else:
print("Are you ready to begin? > ")
print(begin())
def begin():
while something:
raw_input("Are you ready to begin? > ")
start_adventure()
这里有很多差距,但这只是一个开始。目前,我们正在获取用户的输入并将其丢弃,因为我们没有将其存储在任何地方。让我们来解决这个问题
def begin():
while something:
answer = raw_input("Are you ready to begin? > ")
start_adventure()
这已经开始成形。我们只想在应答时继续循环“是”
def begin():
while answer != "yes":
answer = raw_input("Are you ready to begin? > ")
start_adventure()
万岁!让我们看看这是否有效
Traceback (most recent call last):
File "example", line 2, in <module>
while answer != "yes":
NameError: name 'answer' is not defined
这会有用的 如果两次使用raw_input(),它将查找两个不同的输入。您需要调用raw_input()一次,然后将其分配给一个变量。为什么要连续调用
raw_input
三次?所有额外的print
调用是怎么回事?我添加了:x=raw\u input(),而x!='yes':如果x='yes':中断打印(start_adventure())否则:打印(原始输入(“准备好开始了吗?”),它仍然无限循环。我不确定为什么当x=='yes'@melpomene我试图以字符串的形式从用户那里获取输入时,它没有中断。是或否。所以我需要循环获取该信息,并使用它停止循环或继续循环。一旦循环中断,我希望它继续到下一个函数。你不需要break语句,把它去掉。条件更改将使代码“中断”出while循环。while循环的全部要点是不需要break语句。这应该由while循环条件控制,而不是显式的中断调用。对于像op这样的人来说,这使用了python3而不是python2(只是提到op),他们第一次尝试解决这个问题并理清逻辑,我认为这是一个很好的逐步演练@rahlf23我决定开始回答“简单”的问题,回答的答案实际上会帮助提问者,而不是教他们堆栈溢出是一个神奇的小盒子,可以给他们想要的所有代码。非常感谢!!!这真的很有帮助,它将帮助我有一个在未来的其他问题去尝试!!!非常感谢@JacquelynneHeiman如果这回答了您的问题,您可以单击此答案左上角投票按钮下的灰色复选标记。如果它变成绿色的勾号,这意味着它被标记为已接受的答案。如果你那样做,我会得到15分的声誉。