简体   繁体   English

Python:为什么它不接受01或02或03的输入?

[英]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.

相关问题 如何在python中将日期格式化为此'2015-02-03T05:04:01 + 0000' - How to formatting date to this '2015-02-03T05:04:01+0000' in python 如何使用Python的re模块将'1:2:3'标准化为'01:02:03'? - How to normalize '1:2:3' to '01:02:03' by using re module of Python? 将 integer 1,2,3 更改为 01,02,03 of a month 列以使用 pd.to_datetime 等 - Change the integer 1,2,3 with 01,02,03 of a month column to use pd.to_datetime etc Python:将字符串转换为压缩十六进制('01020304' - >'\\ x01 \\ x02 \\ x03 \\ x04') - Python: convert string to packed hex ( '01020304' -> '\x01\x02\x03\x04' ) Python 3.8.2 | 为什么我的输入不接受我的答案,即使它是有效的? (功能) - Python 3.8.2 | Why does my input doesn't accept my answer even if it's a valid one? (function) 为什么 TF Boosted Trees 不接受数字数据作为输入? - Why doesn't TF Boosted Trees accept numerical data as input? 为什么我的脚本不接受有效日期作为用户输入? - Why doesn't my script accept a valid date as user input? Python正则表达式:为什么python不接受我的模式? - Python Regex: why doesn't python accept my pattern? 值错误:时间数据'2019-03-19T07:01:02Z'与格式'%Y-%m-%dT%H:%M:%S.%fZ'不匹配 - Value Error :Time data '2019-03-19T07:01:02Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ' 为什么我的Python代码不接受%z作为DateTime指令? - Why doesn't my Python code accept %z as a DateTime directive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM