-1

OK so I'm not sure what im doing wrong here but my code is automatically choosing the first one in the "if" statement for exmaple if it is

if 1 == 2
print("Works")
elif 1 == 1
print("There we go")

it will automatically choose the first one even if the incorrect value is typed in. Please see my code below:

def troubleshoot ():
print("Now we will try and help you with your iPhone.")
print("")
time.sleep(1)
hardsoft = input(str("Is the issue a problem with hardware of software? Write n if you are not sure:  ") ) #ISSUE WITH IT SELECTING THE FIRST ONE NO MATER WHAT# 
if hardsoft == "Software" or "software" or "S" or "s" or "soft" or "Soft":
    software ()
elif hardsoft == "Hardware" or "hardware" or "Hard" or "hard" or "h" or "H":
    hardware ()
elif hardsoft == "Not sure" or "not" or "Not" or "NOT" or "not sure" or "n" or "N":
    notsure ()
else:
    print("Sorry, that command was not recognised")
    print("Please try again")
    troubleshoot ()
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Leo Codron
  • 17
  • 2
  • 3
    In your first code block, you're missing colons and indentation. Is that a miss copy-paste or does your code look exactly like that? – Celeo Nov 21 '16 at 21:08
  • 1
    `if hardsoft == 'Software' or hardsoft == 'software' or hardsoft == 'S' ....` Or `if 'S' == hardsoft[:1].upper():` – 001 Nov 21 '16 at 21:10

2 Answers2

1

In the first code block you are missing indentation and colons. It should be:

if 1 == 2:
    print("Works")
elif 1 == 1:
    print("There we go")

This way you also get the expected result.

For the second part: if hardsoft == "Software" or "software" or "S" or "s" or "soft" or "Soft": is not a valid condition - or at least it is not doing what you think it should do. Every string there gets converted to a boolean value, and afair any string that is not empty will be interpreted as true. Therefore a condition like if "Software" is always true. Correct condition would be:

if hardsoft == "Software" or hardsoft == "software" or hardsoft == "S" or hardsoft == "s" or hardsoft == "soft" or hardsoft == "Soft":
    ... and so on
Striezel
  • 3,693
  • 7
  • 23
  • 37
0

Your if statement contains many logical parts. Each part converted to boolean - so any non empty string converted to True. As I can suggest you wanted something like this:

def troubleshoot ():
print("Now we will try and help you with your iPhone.")
print("")
time.sleep(1)
hardsoft = input(str("Is the issue a problem with hardware of software? Write n if you are not sure:  ") ) #ISSUE WITH IT SELECTING THE FIRST ONE NO MATER WHAT# 
if hardsoft == "Software" or hardsoft == "software" or hardsoft == "S" or hardsoft == "s" or hardsoft == "soft" or hardsoft == "Soft":
    software ()
elif hardsoft == "Hardware" or hardsoft == "hardware" or hardsoft == "Hard" or hardsoft == "hard" or hardsoft == "h" or hardsoft == "H":
    hardware ()
elif hardsoft == "Not sure" or hardsoft == "not" or hardsoft == "Not" or hardsoft == "NOT" or hardsoft == "not sure" or hardsoft == "n" or hardsoft == "N":
    notsure ()
else:
    print("Sorry, that command was not recognised")
    print("Please try again")
    troubleshoot ()

Also you must be aware from such long ifs, and optimize this code.

Oleg Imanilov
  • 2,591
  • 1
  • 13
  • 26
  • Thank you so much for this, this is exactly what i was looking for. How would I optimize it? – Leo Codron Nov 21 '16 at 21:16
  • You can predefine all your cases in arrays: sw = [ "Software", "software", "S","s","soft","Soft"], then just call sw.index[hardsoft] >= 0. – Oleg Imanilov Nov 21 '16 at 21:21