我对python还很陌生,正在尝试根据“思考python”中的练习构建一个运行10公里的计算器
我要做的是把输入时间分解成两个独立的数字,即:43.12。。。 然后执行(43x60)-给出秒数,然后加上剩余的秒数+12。。 给出准确的数字。。你知道吗
下面是运行它,如果我硬编码4312作为一个整数-但我想接受它的动态。。。有人能帮我指出正确的方向吗
#python 10k calculator
import time
distance = 6.211180124223602
time = float(input("what was your time?"))
tenK = 10
mile = 1.61
minute = 60
sph = 0
def convertToMiles():
global distance
distance = tenK / mile
convertToMiles()
print("Distance equal to :",distance)
def splitInput():
test = [int(char) for char in str(4312)]
print(test)
splitInput()
当您在输入中请求数字时,您已经在将其转换为浮点数了;只需将其作为字符串接受,然后就可以轻松地将其分成不同的部分:
我会让用户以
[hh:]mm:ss
格式输入一个字符串,然后使用如下命令:但如果你真的需要时间的话时间.strtime(). 你知道吗
如果不立即调用convert将用户输入转换为
float
,则更容易。字符串提供了split
函数,而浮点函数则没有相关问题 更多 >
编程相关推荐