我怎样才能让 python 重复某一行代码,直到用户说他们会保留这个名字?

How can I make python repeat a certain line of code until a user says they will keep the name?

我是初学者,我正在尝试创建一个允许用户为他们的漫游者选择名称的程序。该程序应该通过询问用户他们想给他们的漫游者取什么名字来工作,如果他们确定的话,然后最终确定。如果没有,那么他们会继续重做,直到他们决定一个名字。代码写在Python。这是我到目前为止所做的:

    nameofbot = input("What would you like to name your rover?  ")
surity = input("Are you sure?")
surity = "Yes":
  print(nameofbot)

else:
  print("Let's try this again.")
  def multiply(

print("It is...")
time.sleep(3)
print(nameofbot)   

注意:我试图让它重复从“nameofbot”到第二行末尾的所有代码。感谢您的帮助!

flag = True
while(flag):
    bot_name = input("What would you like to name your rover? \n")
    surity = input("Are you sure? \n")
    if surity == "Yes":
      print(bot_name)
      flag = False
     

这应该对你有帮助。该标志将为 True,直到用户不确定该名称,因此 while 循环将 运行,如果他确定该标志设置为 False 并且我们退出循环。

您可以 运行 循环,直到用户给出您想要的输入,例如:

surity = ""
while surity != "Yes":
    nameofbot = input("What would you like to name your rover?  ")
    surity = input("Are you sure?")

这将一直持续 运行ning 直到 surity = "Yes" 退出循环。