Python | Skipping a user_input

会有一股神秘感。 提交于 2019-12-13 23:37:45

问题


I have a problem with my python script as when this question comes up:

("Is there problem with the software or hardware? ")

When I type in "Software" my first software question comes up

("Is your phone freezing/stuttering? ")

So if I answer yes a solution comes up b``ut if I type in No then my hardware question comes up but I dont want that to happen.

Here is my script:

phone2 = input("Is there problem with the software or hardware? ") #Question
if phone2 == "Software" or phone2 == "software" :
def foo():
 while True:
  return False
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
if phone2 == "Hardware" or phone2 == "hardware" :
    def foo():
     while True:
      return False
h1 = input("Does your phone switch on? ")
if h1 == "No" or h1 == "no" :
    print("There might be a issue with your battery. I recommend replacing          it with the help of a specialist")
if h1 == "Yes" or h1 == "yes" :
    def foo():
     while True:
        return False

回答1:


The problem was that you did not use proper indentations after your if statements ,hence the rest of your code was executed anyway:

phone2 = input("Is there problem with the software or hardware? ") #Question
if phone2 == "Software" or phone2 == "software" :
    def foo():
        while True:
             return False
    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
if phone2 == "Hardware" or phone2 == "hardware":
    def foo():
        while True:
            return False
    h1 = input("Does your phone switch on? ")
    if h1 == "No" or h1 == "no":
        print("There might be a issue with your battery. I recommend replacing it with the help of a specialist")
    if h1 == "Yes" or h1 == "yes":
        def foo():
            while True:
                return False

PS: (I don't think you need that "foo" function in there.)




回答2:


(1) Think about what if clauses do.

(2) Make sure you understand what tabs/spaces do in python.

Here is how to do it:

phone2 = input("Is there problem with the software or hardware? ") 
if phone2 == "Software" or phone2 == "software":
    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 phone2 == "Hardware" or phone2 == "hardware" :
   h1 = input("Does your phone switch on? ")
   if h1 == "No" or h1 == "no" :
      print("There might be a issue with your battery.")


来源:https://stackoverflow.com/questions/41144688/python-skipping-a-user-input

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