如何在python中选择小数点?

2024-06-29 00:58:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Python为微软飞行模拟器制作一个简单的飞行计划助手,我正在尝试将一个数字转换成时间 例如,如果某事物的小数位数大于0.6(小时),它将加1减去0.6

问题2: 如何在完成一个问题后重新启动代码?你知道吗

向所有困惑的人道歉:D

print 'Hello and Welcome to Flight planner Helper V1'
print '-'
question_1 = raw_input('time or fuel? ')
if question_1 == 'time':
    a = input('nautical miles ')
    b = input('average ground speed ')
    c = a / float(b)
    print c            # <--- PROBLEM 1
    print 'Hours'


elif question_1 == 'fuel':
    c = input('travel time ')
    d = input('fuel usage (in litres)')
    e = c * d
    f = input('number of engines ')
    g = e * f
    print '%s litres' % (g)    

else:
    print 'your not a pilot, your a emu'

CLOSE = input('Press ENTER to close')

Tags: toinputyourtime助手时间数字模拟器
3条回答

您需要向代码中添加某种循环,这样它就可以继续/“重新启动”。你知道吗

尝试添加“while”循环。你知道吗

while True

或者你需要的任何时间。你知道吗

如果c是小时数,您可以执行以下操作:

hours = int(c)
minutes = int((c - hours) * 60)
seconds = int((c - hours - minutes / 60.0) * 3600))
print "%d:%d:%d" % (hours, minutes, seconds)

要重复代码,只需将其包装成一个循环

while (True):
    your code here

或者最好将代码放入函数中,然后从循环中调用函数。你知道吗

代码中有一些小错误,但很容易解决:

import math

while True:

    print 'Hello and Welcome to Flight planner Helper V1'
    print '-'
    question_1 = raw_input('time or fuel? ')
    if question_1 == 'time':
        a = input('nautical miles ')
        b = input('average ground speed ')
        c = float(a) / float(b)
        print math.ceil(c),            # < - PROBLEM 1
        print 'Hours'


    elif question_1 == 'fuel':
        c = raw_input('travel time ')
        d = raw_input('fuel usage (in litres)')
        e = float(c) * float(d)
        f = raw_input('number of engines ')
        g = float(e) * float(f)
        print '%s litres' % (g)    

    else:
        print 'your not a pilot, your a emu'

    again = raw_input('New calculation? y/n ')
    if again != "y": break

你说0.6是什么意思?你想聚一聚吗?数学.ceil()这样做。你知道吗

相关问题 更多 >