number = raw_input
print (raw_input('Please enter a number between 1 - 50 :'))
if number <= 10:
print('your number is between 0-10')
elif number <= 20 and >=11: <---- when i put >=11: it gives the error
print('your number is between 11-20')
elif number <
elif number <= 20 and >=11:
^
SyntaxError:语法无效 任何提示或想法?
答案 0 :(得分:0)
试试这段代码:
def get_number(number):
if (number < 10):
print "number<10 ",number
elif (number>=11 and number<=20):
print "11<number>20 ",number
elif (number<50):
print "number<50 ",number
else:
print "type proper number"
number = raw_input("enter number between 1 - 50");
get_number(int(number)) ##raw_input reads string, so convert number in int
答案 1 :(得分:0)
我会接受我刚才写的这段代码:
number = int(input("Please enter a number between 1 - 50:"))
if number <= 10:
print("Your number is between 0-10")
elif number <= 20 and number >= 11:
print("Your number is between 11-20")
首先,要回答您的问题,您需要说“elif number&lt; = 20 and number&gt; = 11: 这是因为Python中的'和'将两个语句分开,如果你愿意的话。因此在'和'之后你需要提醒Python你正在使用哪个变量。
另一方面,我只安装了Python 3,因此'raw_input'不起作用(因为它已被删除)。 我会考虑在将来使用输入,因为它会保持支持。唯一的问题是它返回一个字符串,所以你需要将它转换为一个整数来做你想做的事。 / p>
还有一件事,你可以使用散列在Python中发表评论:
#this is a comment and Python will ignore it
编辑: Python 2.7的'raw_input'相当于Pythons 3的'input',所以除非你升级到Python 3,否则继续使用它。