2015-12-21 68 views

回答

0

userEuro是一个字符串。您应该使用float(raw_input())将其转换为浮点数。

FYI,字符串可以相乘,而是仅由一个整数。这导致在一个字符串连接:

print 'a' * 2 
>> 'aa 
0

更改第二行:

USD = float(userEuro) * 1.3667 

的raw_input接受用户输入作为String。 在执行上述数学运算之前,您需要将其转换为浮点数。

1

哪个用户在输入raw_input()返回值的string

所以userEuro变量的类型为字符串,您可以通过type()方法检查

>>> userEuro = raw_input("What amount in Euro do you wish to convert?") 
What amount in Euro do you wish to convert?1.2 
>>> type(userEuro) 
<type 'str'> 
>>> userEuro 
'1.2' 

型铸造从字符串转换浮。

>>> float(userEuro) 
1.2 
>>> 

异常类型的铸造过程中的处理,因为用户可以输入错误的值作为输入。

>>> userEuro = raw_input("What amount in Euro do you wish to convert?") 
What amount in Euro do you wish to convert?ab 
>>> userEuro 
'ab' 
>>> float(userEuro) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: could not convert string to float: ab 

>>> try: 
... float(userEuro) 
... except ValueError: 
... print "Wrong value for type casting: ", userEuro 
... 
Wrong value for type casting: ab 
>>> 

什么是TypeError: can't multiply sequence by non-int of type 'float'例外:

当我们通过浮点值乘以字符串,则该异常来。

>>> "1.2" * 1.3667 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: can't multiply sequence by non-int of type 'float' 

字符串是多个由整数值

>>> "test" * 2 
'testtest' 

但是字符串多个由整数或浮点值

>>> "test" * '2' 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: can't multiply sequence by non-int of type 'str' 
>>> 

的Python 3.X

raw_input()方法是从Python 3.x的除去,

raw_input()用于Python 2.x的

input()用于Python 3.x的

相关问题