Python | Stopping/Restarting a script after user_input

孤者浪人 提交于 2019-12-12 01:19:33

问题


Just before I ask the question I would like to say that I am very new to Python so what ever I ask might be simple to do.

My question is this: How do I stop the script after this question has been answered.

s1 = input("Is your phone freezing/stuttering? ")
if s1 == "Yes" or s1 == "yes":
    print("Try deleting some apps and this might help with your problem")
if s1 == "No" or s1 == "no":
    def foo():
        while True:
            return False

So what I exactly want here is when the user replies with 'Yes' I want the solution to come up and the script to stop or if possible even restart.


回答1:


A simple exit could be reach by this.

I also used the lower function so it will look a bit more pythonic

With just a simple loop:

s1 = "yes"
while s1 == "yes":
  s1 = input("Is your phone freezing/stuttering? ").lower()

Good luck !




回答2:


What you could do is create a loop around it

x = 4
while x == 4:
    s1 = input("Is your phone freezing/stuttering? ")
    if s1 == "Yes" or s1 == "yes":
        print("Try deleting some apps and this might help with your problem")
    if s1 == "No" or s1 == "no":
        def foo():
            while True:
                return False
        break #<-- only if you want the program to end after false#


来源:https://stackoverflow.com/questions/41264055/python-stopping-restarting-a-script-after-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!