raw_input()的一个奇怪的用法,该怎么处理
raw_input()的一个奇怪的用法
1,**** = raw_input('Enter your Number:')
print(****)
#*****************
#*****************
2,**** = raw_input("Enter your Number:")
print int(****)
这个 我都敲进:10.125
执行1代码,报Traceback (most recent call last):
执行2代码,出正确结果:10
请问这2者有什么区别,什么情况使用(''),如何又使用(“”)呢?
------解决方案--------------------
从键盘获取的是str类型。(''),和(“”)没有啥区别。
------解决方案--------------------
>>> int('10.5')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '10.5'
>>> int('10')
10
int([x[, base]])
Convert a string or number to a plain integer. If the argument is a string, it must contain a possibly signed decimal number representable as a Python integer, possibly embedded in whitespace.
这段鸟语我没看懂,是不是说需要这个字符串是python integer类型格式?
------解决方案--------------------
转浮点字符串用float()
------解决方案--------------------
解释器报啥错认真看看,看不懂就整个贴出来,不要自言自语...
------解决方案--------------------
I_NBFA 说的对,你输入的字符串 "10.5" 要转换为数字,因为10.5是float,所以用float(****)
------解决方案--------------------
我测试过很多次,都没问题,我的py版本也是2.7,ubuntu内置的
1,**** = raw_input('Enter your Number:')
print(****)
#*****************
#*****************
2,**** = raw_input("Enter your Number:")
print int(****)
这个 我都敲进:10.125
执行1代码,报Traceback (most recent call last):
执行2代码,出正确结果:10
请问这2者有什么区别,什么情况使用(''),如何又使用(“”)呢?
------解决方案--------------------
从键盘获取的是str类型。(''),和(“”)没有啥区别。
------解决方案--------------------
>>> int('10.5')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '10.5'
>>> int('10')
10
int([x[, base]])
Convert a string or number to a plain integer. If the argument is a string, it must contain a possibly signed decimal number representable as a Python integer, possibly embedded in whitespace.
这段鸟语我没看懂,是不是说需要这个字符串是python integer类型格式?
------解决方案--------------------
转浮点字符串用float()
------解决方案--------------------
解释器报啥错认真看看,看不懂就整个贴出来,不要自言自语...
------解决方案--------------------
I_NBFA 说的对,你输入的字符串 "10.5" 要转换为数字,因为10.5是float,所以用float(****)
------解决方案--------------------
我测试过很多次,都没问题,我的py版本也是2.7,ubuntu内置的