Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中拆分整数_Python - Fatal编程技术网

在python中拆分整数

在python中拆分整数,python,Python,我是python新手,正在尝试根据“思考python”中的练习构建一个10k运行的计算器 我想做的是把输入时间分解成两个独立的数字,即:43.12。。。 然后执行(43x60)-给出秒数,然后将剩余的秒数+12相加。。 给出工作的准确数字 下面是运行它,如果我硬编码4312作为一个整数-但我想动态接受它。。。有人能帮我指出正确的方向吗 #python 10k calculator import time distance = 6.211180124223602 time = float(in

我是python新手,正在尝试根据“思考python”中的练习构建一个10k运行的计算器

我想做的是把输入时间分解成两个独立的数字,即: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()

如果不立即调用convert将用户输入转换为
浮点值
,就更容易了。字符串提供了一个
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

如果不立即调用convert将用户输入转换为
浮点值
,就更容易了。字符串提供了一个
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

如果不立即调用convert将用户输入转换为
浮点值
,就更容易了。字符串提供了一个
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

如果不立即调用convert将用户输入转换为
浮点值
,就更容易了。字符串提供了一个
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

当您在输入中请求该数字时,您已经将其转换为浮点数;只需将其作为字符串接受,然后您就可以轻松地将其分成各个部分:

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

当您在输入中请求该数字时,您已经将其转换为浮点数;只需将其作为字符串接受,然后您就可以轻松地将其分成各个部分:

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

当您在输入中请求该数字时,您已经将其转换为浮点数;只需将其作为字符串接受,然后您就可以轻松地将其分成各个部分:

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.strtime()。

我会让用户输入一个字符串,格式为
[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.strtime()。

我会让用户输入一个字符串,格式为
[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.strtime()。

我会让用户输入一个字符串,格式为
[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.strtime()

试试这种方法。你也可以用这种方法添加一些错误检查

试试这种方法。你也可以用这种方法添加一些错误检查

试试这种方法。你也可以用这种方法添加一些错误检查


试试这种方法。你也可以用这种方法添加一些错误检查。

不太确定你在问什么。您已经在使用
input
获取时间值。不确定您在这里要问什么。您已经在使用
input
获取时间值。不确定您在这里要问什么。您已经在使用
input
获取时间值。不确定您在这里要问什么。您已经在使用
input
获取时间值。如果输入没有秒组件,则此操作将失败。如果输入没有秒组件,则此操作将失败。如果输入没有秒组件,则此操作将失败。如果输入没有秒组件,则此操作将失败。