0

So I'm trying to make a simple troubleshooting app for broken phones and solutions to the problems, about 5 minutes in and I can't get passed line 4 when the user enters the phone model, it just stops instead of continuing the dialogue. I have no clue why. Any help is much appreciated. Also on line 6, I'd love to make it so that when the user writes a line of text about whats wrong with the phone, it picks out words like "broke" "cracked" "wet" etc. but I have no clue how so again, any help is much appreciated!

brand = raw_input("Please state your phone brand. ")
if brand.lower() == ("iphone"):
    iphone = raw_input ("Please state the model. ")
    if iphone.lower() == ("2G"):
        iproblem2g = raw_input ("Please state your problem. ")
        if iproblem2g.lower() == ("broken") or ("broke"):
            ibroke = raw_input ("Is the hardware broken or the software? ")
            if ibroke.lower() == ("hardware") or ("hard"):
                print ("...")
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
Hannibal
  • 3
  • 1
  • 2
  • You need to indent line 2. – pp_ Feb 26 '16 at 12:21
  • @pp_ I think you mean line 3. To my experience text editors don't start at 0. :) – zondo Feb 26 '16 at 12:25
  • Although this question has a relation with another question, it is not a duplicate, but a super-set. This code has many problems that can be addressed. – Xexeo Feb 26 '16 at 12:57

2 Answers2

2

There were a few issues. Try this:

brand = raw_input("Please state your phone brand. ")
if brand.lower() == ("iphone"):
    iphone = raw_input ("Please state the model. ")
    if iphone.lower() == ("2g"):
        iproblem2g = raw_input ("Please state your problem. ")
        if iproblem2g.lower() in ("broken", "broke"):
            ibroke = raw_input ("Is the hardware broken or the software? ")
            if ibroke.lower() in ("hardware", "hard"):
                print ("...")

Indentation is very important in Python. Also, "2G" would have never have matched an input value since the input values are being lowercased. Finally, matching multiple values is simpler in a list or tuple and wouldn't have worked the way it was (it would have always returned True).

Why always True? Take for example if iproblem2g.lower() == ("broken") or ("broke"):. This is checking iproblem2g.lower() == ("broken") OR ("broke"). ("broke") will always be True since it is considered a Truthy value in Python thus the entire conditional will always be True.

Cheers!

CaffeineFueled
  • 561
  • 2
  • 9
0

This line is probably semantically wrong (i.e., it compiles but does not work as you expect):

iproblem2g.lower() == ("broken") or ("broke")

You are actually asking if

(iproblem2g.lower() == ("broken")) or ("broke")

that means

(iproblem2g.lower() == ("broken")) or True

And this will always be True

You can´t check if something is "==" two things in this way. If you want to do this kind of question (is a == b or is a == c) you should use either:

 (iproblem2g.lower() == ("broken")) or  (iproblem2g.lower() ==  ("broke"))

or, more Pythonesc

iproblem2g.lower() in ["broken","broke"]

However, even this question is not a good choice, because it will only match the perfect word "broken" or "broke", and not texts that contain it. It will probably be much better to ask:

   "broken" in iproblem2g.lower() or "broke" in iproblem2g.lower()

In this way you will find the word in any sentence.

You can make it better by creating a simple function that checks if any of a set of words is in a sentence:

def check(words,sentence):
    for word in words:
        if word in sentence:
            return True
    return False

You can even do some pre-processing using .lower() and .trim()

Finally, there is a kind of small trick that you didn´t notice: if "broken" is the string, "broke" always will be (since it is a substring), so you do not need to check for both.

Xexeo
  • 302
  • 1
  • 12
  • He didn't notice because he wasn't looking for it. He was looking at case-insensitive equality. – zondo Feb 26 '16 at 12:43