#### If statement only printing else

``````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')
``````

7 个回复

``````if amount in range(1,7):
``````

``````if amount == range(1,7):
``````

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

：正如其他答案之一指出的那样，您可能还想将`input()`的结果强制转换为int-因为`input()`通常返回一个字符串。

``````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')
``````

``````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')
``````

``````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')
``````

1. python3中的range函数返回一个迭代类型。

2. python3中的输入函数返回一个字符串，应将其转换为整数。 尝试这个：

``````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')
``````

`input`返回一个字符串，所以我想你应该做`amount = int(input('...'))`

1 没有打印语句的Python打印

2 打印语句后的输入语句先打印

2018-12-28 08:13:00 1 18   python
4 php，打印语句在哪里打印

7 打印语句会多次打印而不是一次打印？

2016-03-10 17:17:33 1 332   c/ printf
8 else 语句不打印线？

9 如何保存打印语句的输出

10 aws lambda 不记录打印语句