0
def quantity():
    global amount 
    amount = input('How many tickets are you looking for? ')
    if amount == range(1,7):
        print('You have selected {} tickets '.format(amount))
    elif amount == (0):
        print('You have selected 0 tickets')
    else:
        print('Please choose an amount between 0-6')

I'm trying to create ticket cost calculator to jog my memory but I was stumped on the IF statement for the quantity section because it only prints the ELSE even when IF and ELIF are true.

  • 1
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – tripleee Nov 15 '18 at 04:59
  • 1
    @tripleee: The dupe you're suggesting doesn't cover the comparison to a `range` value, but you're about halfway there. – Makoto Nov 15 '18 at 05:00
  • @Makoto but that wasn't the main concern of OP, the question was why do both IF and ELIF fail, and the reason is covered in the duplicate. i.e. without conversion ELIF alway fails, with conversion it is true for 0. – Emil Vatai Nov 15 '18 at 05:10

7 Answers7

2

You want

if amount in range(1,7):

rather than what you currently have,

if amount == range(1,7):

It's a bit more complicated in reality (because it returns a generator rather than a list), but you can conceptualize range(1,7) as a function that returns a list of numbers in that range. e.g.

range(1,7) ~~ [1, 2, 3, 4, 5, 6]

If your amount is an integer, you want to see if it's in that range, not if it is that range - after all, an integer cannot be a list at the same time.


[edit]: As one of the other answers pointed out, you might also want to cast the result of your input() to an int - as input() generally returns a string.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • 1
    `amount` isn't an `int`, though. You're close. – Makoto Nov 15 '18 at 05:00
  • `def quantity(): global amount amount = input('How many tickets are you looking for? ') if amount in range(1,7): print('You have selected {} tickets '.format(amount)) elif amount == (0): print('You have selected 0 tickets') else: print('Please choose an amount between 0-6') amount = int(amount)` So I added the if ... in and turned amount into an integer but still receive only the else part. – WalkingLick Nov 15 '18 at 05:09
  • You need to put `amount = int(amount)` *before* you do the `if` condition, not after. You could even wrap the `input()` statement in an `int()` call – Green Cloak Guy Nov 15 '18 at 05:15
0

Try this code use in for ==

def quantity():
    global amount 
    amount = input('How many tickets are you looking for? ')
    if amount in range(1,7):
        print('You have selected {} tickets '.format(amount))
    elif amount == (0):
        print('You have selected 0 tickets')
    else:
        print('Please choose an amount between 0-6')
vishalk
  • 182
  • 2
  • 10
0

You have to convert the input() from a string into an integer, otherwise you will always run the else statement. You can do that with int(), but will likely have to do some error handling in case the user enters something that cannot be converted to an integer.

0

I have made some changes to your code. I believe this is what you are looking for.

def quantity():
    global amount
    amount = int(input('How many tickets are you looking for? '))
    if amount in range(1,7):
        print('You have selected {} tickets '.format(amount))
    elif amount ==0 :
        print('You have selected 0 tickets')
    else:
        print('Please choose an amount between 0-6')
Nishil
  • 227
  • 1
  • 9
0

Instead of using if amount == range(1,7): use if amount in range(1,7):

This is the modified code that you should use:

def quantity():
    global amount 
    amount = input('How many tickets are you looking for? ')
    if amount in range(1,7):
        print('You have selected {} tickets '.format(amount))
    elif amount == (0):
        print('You have selected 0 tickets')
    else:
        print('Please choose an amount between 0-6')
Siddharth Satpathy
  • 2,737
  • 4
  • 29
  • 52
0

1.The range function in python3 returns an iteration type.

2.The input function in python3 returns a string and should be converted to an integer. Try this:

amount = int(input('How many tickets are you looking for? '))
if amount in range(1,7):
    print('You have selected {} tickets '.format(amount))
elif amount == (0):
    print('You have selected 0 tickets')
else:
    print('Please choose an amount between 0-6')
myhaspldeep
  • 226
  • 2
  • 7
-1

input returns a string, so I guess you should be doing amount = int(input('...')).

Emil Vatai
  • 2,298
  • 1
  • 18
  • 16
  • Am I missing something? Ahh... the `in` not `==` is also important... but I don't think that was the main concern of OP. – Emil Vatai Nov 15 '18 at 05:04