-2

I have this piece of code, and everything works fine, and now I am trying to build the IF statement correctly.

I only have one ASCEND function right now, so yea, the IF statement references it for ascend and descend, so ignore that piece. When I typed in "r" as my 'answer', it still ran the rainSortAscend.

Can you help point me in the right direction for the IF statement?

answer = ""
print('Do you want the rainfall to be ascending or descending?')
answer = input('Answer with "A" or "D"' + answer)


def rainSortAscend(monthRainfall):
    for number in range(len(monthRainfall)-1,0,-1):
        for i in range(number):
            if monthRainfall[i]>monthRainfall[i+1]:
                temp = monthRainfall[i]
                monthRainfall[i] = monthRainfall[i+1]
                monthRainfall[i+1] = temp


if answer == 'a' or 'A':
    rainSortAscend(monthRainfall)
    print(monthRainfall)
elif answer == 'd' or 'D':
    ranSortAscend(monthRainfall)
else:
    print('Start over and type in the specified "A" or "D" option')
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
hitbid
  • 53
  • 7
  • 1
    Try `if answer == 'a' or answer == 'A':` – Mathias Jun 02 '18 at 17:23
  • `if answer == 'a' or 'A'` checks whether answer equals to 'a', or literal 'A', which is always true. you need to use `if answer == 'a' or answer == 'A'` or `if answer in ['a', 'A']` – Priyesh Kumar Jun 02 '18 at 17:23

1 Answers1

1

Since you are using if answer=='a' or 'A', the 'A' will always be true which causes rainSortAscend function to run everytime. if you want to validate single variable for multiple values. You can write your code as shown below

if answer in('a','A'):
    rainSortAscend(monthRainfall)
    print(monthRainfall)
elif answer in('d','D'):
    ranSortAscend(monthRainfall)
else:
    print('Start over and type in the specified "A" or "D" option')
Nitishkumar Singh
  • 1,781
  • 1
  • 15
  • 33