【发布时间】:2021-04-25 00:32:49
【问题描述】:
每当我运行以下代码时,都会出现值错误。如何将以下代码从字符串更改为整数?谢谢
v1 = "5"
v2 = "3"
#covert to int
v1 = int(v1)
v2 = int(v2)
#save the sum of v1 and v2 into to var total
total = v1 + v2
print("The sum of :", v1 , "and", v2, "is", total)
或选项 2
v1 = input("Please enter a number: ")
v1 = int(v1)
v2 = input("Please enter a number: ")
v2 = int(v2)
#save the sum of v1 and v2 into to var total
total = v1 + v2
print("The sum of :", v1 , "and", v2, "is", total)
【问题讨论】:
-
input('5')不会像您认为的那样做。它不使用字符串'5',它只是使用5作为提示符 -
我想可能是因为我将 str 输入转换为 int 它可以改变它。根据我正在学习的书。但非常感谢
-
v1 = input(5) v2 = input(3) total = int(v1) + int(v2) print ("the sum of", v1, "and", v2, "is? ",总计)
-
我仍然有 vaule 错误
标签: python-3.x variables input integer