将使用 'str()' 的变量转换为数字
turning variables that use 'str()' into numbers
我正在尝试将 math.floor 函数应用于一些使用 str()
函数的变量...执行此操作的正确方法是什么?
这是我的代码:
import math
x = str(10.3)
y = str(22)
z = str(2020)
print "x equals " + x
print "y equals " + y
print "z equals " + z
#playing around with the math module here. The confusion begins...
#how do I turn my str() functions back into integers and apply the floor function of the math module?
xfloor = math.floor(x)
zsqrt = math.sqrt(z)
print "When we print the variable \"xfloor\" it rounds " + x + "down into " + xfloor + "."
print "When we print the variable \"zsqrt\" it finds the sqareroot of " + z + "which is " + zsqrt + "."
raw_input("Press the enter key to continue.")
欢迎任何帮助。
把他们扔回去:
xfloor = math.floor(float(x))
zsqrt = math.sqrt(float(z))
但这不是推荐的做法,因为您不必要地将其转换为 str
。要 print
使用 str.format
print "x equals {}".format(x)
为此,您无需转换为 str
。
我正在尝试将 math.floor 函数应用于一些使用 str()
函数的变量...执行此操作的正确方法是什么?
这是我的代码:
import math
x = str(10.3)
y = str(22)
z = str(2020)
print "x equals " + x
print "y equals " + y
print "z equals " + z
#playing around with the math module here. The confusion begins...
#how do I turn my str() functions back into integers and apply the floor function of the math module?
xfloor = math.floor(x)
zsqrt = math.sqrt(z)
print "When we print the variable \"xfloor\" it rounds " + x + "down into " + xfloor + "."
print "When we print the variable \"zsqrt\" it finds the sqareroot of " + z + "which is " + zsqrt + "."
raw_input("Press the enter key to continue.")
欢迎任何帮助。
把他们扔回去:
xfloor = math.floor(float(x))
zsqrt = math.sqrt(float(z))
但这不是推荐的做法,因为您不必要地将其转换为 str
。要 print
使用 str.format
print "x equals {}".format(x)
为此,您无需转换为 str
。