0

The program takes all the inputs properly, however when the user enters all the chords that need to be transposed, the program only performs a positive shift on them(it doesn't execute the negative shift part of the if statement)

I have realised that if I swap the positive and negative shift parts of the program around. The program only performs negative shifts and I cant figure out how to solve this.(The elif sign == '2' onward is being skipped) problem persists if or part of the statement is removed from both branches of the if statement



usr_list = ['A','BB','B','C','C#','D','EB','E','F','F#','G','AB']

while len(u_list)>0:
    chord = (u_list[0])
    if sign == '1' or '+':
        sign = '+'
        chord_value = int(usr_list.index(chord))
        chord_transpose = chord_value + t_key
        if chord_transpose >= 12:
            chord_transpose = chord_transpose - 12
            print('Greater than 12',usr_list[chord_transpose])
        elif chord_transpose < 12:
            del u_list[0]
            t_list.append(usr_list[chord_transpose])
        else:
            print('Invalid')
    elif sign == '2' or '-':
        sign = '-'
        chord_value = int(usr_list.index(chord))
        chord_transpose = chord_value - t_key
        if chord_transpose >= 12:
            chord_transpose = chord_transpose + 12
            print('Greater than 12',usr_list[chord_transpose])
        elif chord_transpose < 12:
            del u_list[0]
            t_list.append(usr_list[chord_transpose])
        else:
            print('Invalid')


    else:
        print('Invalid')

print('transposition of those chords by',sign+str(t_key),'is',t_list)

when the user enters a transposition value between 1 & 12 and chooses chords from the list , transposition of their chosen symbol should be performed either positive or negative but for some reason even when user chooses a negative transposition, the program performs a positive shift

  • `if sign == '1' or '+':` does not do what you think. It is *always* true. – chepner Apr 03 '19 at 17:15
  • Yes, always. If `sign == '1'` is False, it evaluates `'+'`, which is always True. – chepner Apr 03 '19 at 17:17
  • what do you mean it is always true?, I need to check the value of sign to execute 2 different parts of the code - either positive shift or negative. How else is this possible? – devjyot kainda Apr 03 '19 at 17:18
  • The expression `sign == '1' or '+'` is parsed the same as `(sign == '1') or ('+')`. If `sign == '1'`, it's true. If not, it's true if `'+'` is true, which as a non-empty string it is. – chepner Apr 03 '19 at 17:19
  • @0x5453 I have the same problem even if the 'or '+' ' is removed – devjyot kainda Apr 03 '19 at 17:19
  • Because you have the *same* problem in the `elif` clause. – chepner Apr 03 '19 at 17:20
  • I meant if I remove it from them both, I still have the same problem – devjyot kainda Apr 03 '19 at 17:29
  • What's `u_list`, and is it intended to be separate from `usr_list`? Destructively iterating over it is odd to begin with; what's wrong with `for chord in u_list: ...`? – chepner Apr 03 '19 at 17:49

2 Answers2

1

If you want to evaluate multiple if's you need to use if for each one and not elif. As you have your code written right now, as soon as it satisfies one of the if's it doesn't consider the others because elif is only checked if the previous if statement is not true.

For example:

str = 'abc'
if 'a' in str:
    print 'a'
elif 'b' in str:
    print 'b'
elif 'c' in str:
    print 'c'

This would only print a. If you wanted it to print a-c you would have to use:

str = 'abc'
if 'a' in str:
    print 'a'
if 'b' in str:
    print 'b'
if 'c' in str:
    print 'c'

This way it will check all conditions instead of just checking up to the first true condition.

0

Found the problem :) if sign == '1' or '+': and elif sign == '2' or '-': Should be if sign == '1' or sign == '+': and elif sign == '2' or sign == '-':

Whenever sign was 2, the if statement would be read by python as follows

(sign == 1) or ('+') Which resolved to (false) or (true)

Meaning that if statement ran every time.

Small syntactical mistake we all make, hope this was helpful!

OsmosisJonesLoL
  • 244
  • 1
  • 4