将两个字符串连接在一起然后转换为整数。蟒蛇

时间:2017-12-05 17:59:32

标签: python python-3.x python-2.7

我必须编写一个程序,以便用户可以按照他们输入的百分比增加100美元。我需要做100 * 1.n,但我一直收到错误,因为我将字符串转换为整数,请帮忙;

percent = input("Enter the % you want to increase by: ")
n = "1." + percent
k = int(n)
print(k)

我不断收到错误消息:

Traceback (most recent call last):
 File "C:\Users\T Pad\Downloads\dlee.py", line 3, in <module>               
 k = int(n)
 ValueError: invalid literal for int() with base 10: '1.50'

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您无法将其转换为int,您需要float

percent = '2837' #input("Enter the % you want to increase by: ")
n = "1." + percent
k = float(n)
print(k)

答案 1 :(得分:0)

您试图将包含浮点数的字符串转换为int,如果您想要浮点数,则会引发ValueError

percent = input("Enter the % you want to increase by: ")
n = "1." + percent
k = float(n)
print(k)