0

converter including multiple input(). :

def distance_converter():
    print ('Please choose the value you wish to convert by entering the 
    relevant letter:')
    intro=input('A) miles to km or km to miles  B)cm to inch or inch to cm:')
    if intro.upper() == ('A'):
        A_ans1=input('would you like to convert miles or km?   ')
        if A_ans1.lower() == ('miles') or ('m'):
            M_ans1 = input('please enter amount of miles: ')
            y = float(M_ans1)*1.6 or int(M_ans1)*1.6
            return 'the amount of km are: ' + str(round(y,2))
        if A_ans1.lower() == ('km') or ('k'):
            Km_ans1 = input('please enter amount of km: ')
            x = float(Km_ans1)*0.62137 or int(Km_ans1)*0.62137
            return 'the amount of miles are: ' + str(round(x,2))
    if intro.upper() == ('B'):
        B_ans1=input('would you like to convert cm or inch? ')
        if B_ans1.lower == ('cm') or ('c'):
            Cm_ans1 = input('please enter amount of cm: ')
            t = float(Cm_ans1)/2.54 or int(Cm_ans1)/2.54
            return 'the amount of inches are: ' + str(round(t,2))
    if B_ans1.lower == ('inch') or ('i'):
            Inch_ans1 = input('please enter amount of inch: ')
            z = float(Inch_ans1)*2.54 or int(Inch_ans1)*2.54
            return 'the amount of inches are: ' + str(round(z,2))

so the code works fine with the first input of A and B. Lets say I choose option A, it Doesn't matter what I input later, it always takes me to the miles converter. Same issue with cm and inches. Its like its not taking in to account the second "if" that will ask for a km input, always leads to the string of 'please enter amount of miles'. same issue with the B option and cm/inch. Thanks

T.Nel
  • 1,540
  • 2
  • 18
  • 34
  • 1
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Patrick Haugh Jul 20 '18 at 11:37
  • `if A_ans1.lower() == ('miles') or ('m'):` >>>>> `if A_ans1.lower() in ('miles','m'):` _or_ `if A_ans1.lower()[0] == 'm':` if you can live with the fact that entering 'music' will lead to miles as well - same for your other comparisons. – Patrick Artner Jul 20 '18 at 11:41

1 Answers1

0

if A_ans1.lower() == ('miles') or ('m'):

needs to be changed to

if A_ans1.lower() == ('miles') or A_ans1.lower() == ('m'):

The first code will check if A_ans1.lower() == ('miles') is True, then if ('m') is True. ('m') is just a non-empty string, so it is always True.

It is exactly the same for the B option. But in this case you also forgot the () after the lower function call. A_ans1.lower will just return the lower function but won't call it.

EDIT :

A more pythonic way of doing

if A_ans1.lower() == ('miles') or A_ans1.lower() == ('m'):

is to do :

if A_ans1.lower() in ('miles', 'm'):

T.Nel
  • 1,540
  • 2
  • 18
  • 34
  • This is all posted already inside the dupe - which was flagged as such by PAtrick Haugh about 6 minutes before your answer / in my comment which predates your answer by a minute. – Patrick Artner Jul 20 '18 at 12:27
  • @PatrickArtner Their is also the problem on the `lower` call which I think could have been a pain to debug for the author if he's not used to common python mistake since `'A'.lower == 'a'` returns `False` and do not triggers warning if you don't use a linter. If you think my answer isn't helping, flag it, but until its removal, I'm considering the answer has been helping more than the dupe link and found it's place here. – T.Nel Jul 20 '18 at 13:02