[英]Python: Why doesn't it accept an input of 01 or 02 or 03 for month?
print "What is your name?",
name = len(raw_input())
print name
print "How old are you?",
age = int(input())
print "What month were you born in? ",
month = int(input())
if (month>12) or (0>month):
print "That does not exist"
else:
all = (name+age+month)
print "Your name length plus your age plus",
print "the month you were born is equal to:",
print all
If you run the code it works but when prompting to input a month number, if you input 8 it works but if you input 08 it doesn't. 如果你运行代码它可以工作,但是当提示输入一个月号时,如果你输入8它可以工作,但如果你输入08则不然。 Can someone please tell me why. 有人可以告诉我为什么。 I am using Python 2.7 我使用的是Python 2.7
In python2.x, 0
is the prefix for octal numbers... 在python2.x中, 0
是八进制数字的前缀...
>>> 010
8
So, some numbers will be invalid ... eg 08
(since that is out of the range of octal numbers). 因此,某些数字将无效......例如08
(因为它超出了八进制数的范围)。
If you change the code to use int(raw_input('...'))
, it should work since int
always assumes base 10 unless you say otherwise. 如果你改变代码使用int(raw_input('...'))
,它应该工作,因为int
总是假定为10,除非你另有说明。
>>> int('08')
8
You are using input()
for your numbers which according to the docs is 您根据文档使用input()
作为数字
Equivalent to eval(raw_input(prompt)).
"8" is valid python but "08" isn't. “8”是有效的python但“08”不是。
change your calls to raw_input()
and it will work as expected. 将您的调用更改为raw_input()
,它将按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.