在 python 中拆分整数
Splitting an integer in python
我是 python 的新手,正在尝试根据 "Think Python"
中的练习构建一个 10k 运行计算器
我想做的是将输入时间 ie:43.12 分解为 2 个单独的数字...
然后执行 (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()
如果不立即调用将用户输入转换为 float
会更容易。字符串提供 split
功能,浮点数不提供。
>>> time = input("what was your time? ")
what was your time? 42.12
>>> time= time.split('.')
>>> time
['42', '12']
>>> time= int(time[0])*60+int(time[1])
>>> time
2532
当您在输入中请求时,您已经将数字转换为浮点数;只接受它作为一个字符串,然后你可以很容易地将它分成不同的部分:
user_input = input('what was your time?')
bits = user_input.split('.') # now bits[0] is the minute part,
# and bits[1] (if it exists) is
# the seconds part
minutes = int(bits[0])
seconds = 0
if len(bits) == 2:
seconds = int(bits[1])
total_seconds = minute*60+seconds
我会让用户输入格式为 [hh:]mm:ss
的字符串,然后使用类似:
instr = raw_input('Enter your time: [hh:]mm:ss')
fields = instr.split(':')
time = 0.0
for field in fields:
yourtime *= 60
yourtime += int(field)
print("Time in seconds", yourtime)
但如果你真的需要时间,那么你可以使用 time.strptime()。
import re
time = raw_input("what was your time? ")
x=re.match(r"^(\d+)(?:\.(\d+))$",str(time))
if x:
time= int(x.group(1))*60+int(x.group(2))
print time
else:
print "Time format not correct."
试试这个 way.You 也可以通过这种方式添加一些错误检查。
我是 python 的新手,正在尝试根据 "Think Python"
中的练习构建一个 10k 运行计算器我想做的是将输入时间 ie:43.12 分解为 2 个单独的数字... 然后执行 (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()
如果不立即调用将用户输入转换为 float
会更容易。字符串提供 split
功能,浮点数不提供。
>>> time = input("what was your time? ")
what was your time? 42.12
>>> time= time.split('.')
>>> time
['42', '12']
>>> time= int(time[0])*60+int(time[1])
>>> time
2532
当您在输入中请求时,您已经将数字转换为浮点数;只接受它作为一个字符串,然后你可以很容易地将它分成不同的部分:
user_input = input('what was your time?')
bits = user_input.split('.') # now bits[0] is the minute part,
# and bits[1] (if it exists) is
# the seconds part
minutes = int(bits[0])
seconds = 0
if len(bits) == 2:
seconds = int(bits[1])
total_seconds = minute*60+seconds
我会让用户输入格式为 [hh:]mm:ss
的字符串,然后使用类似:
instr = raw_input('Enter your time: [hh:]mm:ss')
fields = instr.split(':')
time = 0.0
for field in fields:
yourtime *= 60
yourtime += int(field)
print("Time in seconds", yourtime)
但如果你真的需要时间,那么你可以使用 time.strptime()。
import re
time = raw_input("what was your time? ")
x=re.match(r"^(\d+)(?:\.(\d+))$",str(time))
if x:
time= int(x.group(1))*60+int(x.group(2))
print time
else:
print "Time format not correct."
试试这个 way.You 也可以通过这种方式添加一些错误检查。