【问题标题】:how to ask the user if they want the code to start again如何询问用户是否希望代码重新开始
【发布时间】:2023-03-10 14:17:01
【问题描述】:

我刚开始学习 python Nat5 课程,所以如果答案非常明显,我很抱歉,但我想要做的是询问用户是否希望重新启动代码并再次询问是否用户输入错误答案

print("would you like to ask again?")
go = input("enter yes/y or no/n")

if(go == 'y') or (go == 'yes'):
#replay here
elif(go == 'n') or (go == 'no'):
    exit()
else:
print("It's a yes or no question")
#ask to replay again

【问题讨论】:

  • 了解循环。
  • 你想在这种情况下使用while loop

标签: python


【解决方案1】:

我不知道这是否是最佳做法,但它对我有用,而且我没有遇到任何问题。 将代码放入函数中

def code():
    #insert the code here

然后调用函数

if (go=='y'):
   code()

还值得注意的是,您还需要在代码末尾调用该函数,以便它真正运行第一次。

【讨论】:

  • 完美解决了我的问题,感谢一群朋友
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
相关资源
最近更新 更多