1

new to python and need some help with while loop that causing me some issues. I am creating a simple converter.

def converter():
    x = 1
    while x == 1:
        q_1 = input ('please choose the currency you wish to convert: \
                 a) US Dollar\
                 b) Euro\
                 c) Shekel ')

       if q_1.lower() is ('a' or 'b' or 'c'):
           x += 1
       x = 1



   q_2 = False
   while q_2 is False:
       q_2 = input ('please choose the currency you wish to convert to:\
                 a) US Dollar\
                 b) Euro\
                 c) Shekel ')
       if q_2.lower() is not ('a' or 'b' or 'c'):
             q_2 = False
       print ('end for now')

For some reason the first loop is not working. Whatever I input, whether its a,b,c lower or upper or a wrong answer (for example: d,e,f...) it returns the same q_1, Its not moving on to the next block of code even if the input is correct (=a,b,c). Thanks

  • 1
    read up about if statements. do not use `is` unless you need `if a is None` - use `if q_1.lower() in "abc":` This: `if q_1.lower() is ('a' or 'b' or 'c')` is not the way to combine if conditions. – Patrick Artner Sep 05 '18 at 13:30
  • The last line in the first while loop makes the condition always True, so why would you expect that the program ever goes beyond that block? oO – meissner_ Sep 05 '18 at 13:31
  • You add `x += 1` in your `if` statement, then reset it back to 1 on the next line. If you want the `while` loop to break then just miss-out the `x = 1`, or refactor without `x` and use `break`. – cdarke Sep 05 '18 at 13:31
  • 1
    @PatrickArtner is correct. Also, you're adding 1 to `x` when if statement is true but you always make it 1 afterwards. – Lucas Wieloch Sep 05 '18 at 13:32
  • 2
    `q_1.lower() is ('a' or 'b' or 'c')` does not do what you think it does. Don't use `is` when you want to test value equality (`is` tests for *identity*, the two references pointing to the same object). `('a' or 'b' or 'c')` evaluates to the first non-empty string, so you have `if q_1.lower() is 'a'`, and no lowered string produced by `input()` is ever going to be the same object as the `'a'` literal. – Martijn Pieters Sep 05 '18 at 13:32
  • Next, you execute `x +=1` in the `if` statement, and *on the next line* set `x` back to `1`, so it'll never actually be anything other than `1` at the top of the loop. – Martijn Pieters Sep 05 '18 at 13:33
  • You probably want to look at [Asking the user for input until they give a valid response](//stackoverflow.com/q/23294658) too, as that covers the functionality you want to get to work. The correct test would be `q_1.lower() in {'a', 'b', 'c'}` or `q_1.lower() in 'abc'` (both would work, the first is technically the better option but the difference here is negligible). – Martijn Pieters Sep 05 '18 at 13:35

0 Answers0