好吧,我是一般编码的新手,在一次练习活动中,我遇到了使用while循环的问题,我试图模拟一个8号球,除非你问它一个问题,否则它不起作用,到目前为止,我所要做的就是使代码重新询问问题,直到它满足非空输入的参数,但每次输入非空时,它都在打印出8球答案之前结束
question = input("Isaiah's MagicAndTotallyNotSentient 8-Ball: what is your question? ")
while len(question) == 0:
if len(question) == 0:
print("...")
question = input("Isaiah's MagicAndTotallyNotSentient 8-Ball: What is your question?")
continue
elif len(question) < 0:
break
我昨天一整天都在做这个,今天我终于大体上结束了循环,但是现在我不知道如何让它继续执行while循环之后的代码,而不中断当前的循环。我已经就位了。我试着用else语句来中断循环,用elif语句来中断循环,但是现在我不太确定该怎么做
3条答案
按热度按时间bvk5enib1#
你可以使用walrus运算符(
:=
)来赋值和计算变量,同时也可以减少重复代码,所以while循环会一直持续到变量的值不为None或不为空为止:gmol16392#
我认为如果你只是检查输入是否为空,而不是马上进入while循环,会更实用。
k97glaaz3#
只有当输入为空时,才需要进入循环,而
if len(question) == 0:
和while len(question) == 0:
似乎是多余的。我想我会这样做: