如何制作汽车用品,告诉您当前的速度,汽油的百分比等。在Python中 [英] How do I make a car thing that tells you the current speed, percent of gas etc. In Python

查看:99
本文介绍了如何制作汽车用品,告诉您当前的速度,汽油的百分比等。在Python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作汽车用品,告诉你当前的速度,最大值,速度,剩余汽油的百分比等。它还需要在GPS上有2-10个目的地。我被困在GPS上了。顺便说一句,GPS的东西也就是目的地。



我尝试过:



这是我到目前为止:



< pre lang =Python> class Car(object): 
def __init __(self,max_speed,acceleration):
self.max_speed = max_speed
self.acceleration = acceleration
self.current_speed = 0


def加速(自我,时间):
self.current_speed = self.current_speed + self.acceleration * time
if self.current_speed> self.max_speed:
print(警告!!!当前速度比最大速度快。)
self.current_speed = self.max_speed
返回self.current_speed


def speeddown(自我,时间):
self.current_speed = self.current_speed / time
返回self.current_speed


def距离(自我,目的地):
距离= 0
距离=距离+目的地
返回self.distance


def check_gas(自我,时间,current_speed):
距离=时间* current_speed
gas = 500
如果距离== +1:
gas - 1
如果气体== 50:
打印(警告,燃气不足。)
返回self.check_gas


lamborghini_asterion =汽车(400,40)
lamborghini_asterion.speedup(10 )
lamborghini_asterion.speeddown(2)

old_barn = 1
#vancouver_airpor t = 53
#usa_border = 124
#sir_willam_osler_elementary_school = 12
#science_world = 34
#living_shangri_la = 25
print(请只输入小写!)
destination = input(选择:1km之外的旧谷仓|温哥华机场53km |美国边境124km |威廉奥斯勒小学爵士小学12km |科学世界34km |生活香格里拉25公里远:)
如果目的地==(旧谷仓):
print(你到了+(old_barn / lamborghini_asterion.current_speed)+分钟。你有 +(lamborghini_asterion.check_gas)+你到达后的500分钟气体。

解决方案

我现在完成了:

  import  time 
class Car( ):
def __init __(self,max_speed,acceleration,total_gas):
self.max_speed = max_speed
self.acceleration = acceleration
self.total_gas = total_gas
self.current_speed = 0
self.distance = 0
self.old_gas = total_gas


def 加速(自我,时间):
self.current_speed = self.current_speed + self.accel eration * time
if self.current_speed> = self.max_speed:
print 警告!!!当前速度更快或与最大速度相同。速度。
self.current_speed = self.max_speed
return self.current_speed


def speeddown(self,time):
self.current_speed = self.current_speed / time
return self.current_speed


def check_gas(self,distance):
current_gas = self.old_gas - 距离
print 你有 + str(current_gas)+ gas out of + str(self到达之后.total_gas+
self.old_gas = current_gas
如果 current_gas< = 50 和 current_gas> 0
print 警告气体低!!
如果 current_gas< = 0
print 没有更多气体。

I am trying to make a car thing that tells you the current speed, max,speed, % of gas left etc. on spyder. It also needs to have 2-10 destinations on a GPS thing. I'm stuck on the GPS thing. By the way the GPS thing is aka destination.

What I have tried:

This is what I have so far:

<pre lang="Python">class Car(object):
    def __init__(self, max_speed, acceleration):
        self.max_speed = max_speed
        self.acceleration = acceleration
        self.current_speed = 0
        

    def speedup(self, time):
        self.current_speed = self.current_speed + self.acceleration * time
        if self.current_speed > self.max_speed:
            print ("Warning!!! The current speed is faster than the max. speed.")
            self.current_speed = self.max_speed
        return self.current_speed
    
    
    def speeddown(self, time):
        self.current_speed = self.current_speed / time
        return self.current_speed

    
    def distance(self, destination):
        distance = 0
        distance = distance + destination
        return self.distance
        

    def check_gas(self, time, current_speed):
        distance = time * current_speed
        gas = 500
        if distance == +1:
            gas - 1
        if gas == 50:
            print("Warning, low on gas.")
        return self.check_gas


lamborghini_asterion = Car(400, 40)
lamborghini_asterion.speedup(10)
lamborghini_asterion.speeddown(2)
    
old_barn = 1
#vancouver_airport = 53
#usa_border = 124
#sir_willam_osler_elementary_school = 12
#science_world = 34
#living_shangri_la = 25
print ("Please only type in lowercase!")
destination = input ("Choose from: Old barn 1km away | Vancouver Airport 53km away | USA Border 124km away | Sir William Osler Elementary School 12km away | Science World 34km away | Living Shangri-La 25km away: ")
if destination == ("old barn"):
     print ("You arrived in " + (old_barn / lamborghini_asterion.current_speed) + "minutes. You have " + (lamborghini_asterion.check_gas) + "gas out of 500 after you arrived")

解决方案

I'm done now:

import time
class Car():
    def __init__(self, max_speed, acceleration, total_gas):
        self.max_speed = max_speed 
        self.acceleration = acceleration
        self.total_gas = total_gas
        self.current_speed = 0
        self.distance = 0
        self.old_gas = total_gas
        

    def speedup(self, time):
        self.current_speed = self.current_speed + self.acceleration * time
        if self.current_speed >= self.max_speed:
            print ("Warning!!! The current speed is faster or the same as the max. speed.")
            self.current_speed = self.max_speed
        return self.current_speed
    
    
    def speeddown(self, time):
        self.current_speed = self.current_speed / time
        return self.current_speed


    def check_gas(self, distance):
         current_gas = self.old_gas - distance
         print ("You have " + str (current_gas) + " gas out of " + str (self.total_gas) + " after you arrived")
         self.old_gas = current_gas
         if current_gas <= 50 and current_gas > 0:
             print("Warning low on gas!!")
         if current_gas <= 0:
             print ("No more gas. 😢 ")
         return current_gas

class electronic_car(Car):
    def __init__(self, battery_life, max_speed, acceleration):
#        self.battery_life = battery_life
#        self.max_speed = max_speed
#        self.acceleration = acceleration
#        self.current_speed = 0       
#        self.old_battery = battery_life
        
        super().__init__(max_speed, acceleration,0)
        self.old_battery = battery_life
        self.battery_life = battery_life
    
    def check_gas(self):
        print("Gas is not available in electronic_car.")

  
    
def GPS ():
    lamborghini_asterion = Car(650, 40, 500)
    lamborghini_asterion.speedup(13)
    print ("Type exit to exit")
    print ("Please only type in lowercase!")
    print ("Type the numbers not the words!")
    while True:  
        old_barn = 1
        vancouver_airport = 41
        usa_border = 124
        sir_willam_osler_elementary_school = 12
        science_world = 32
        gas_station = 10
        destination = input (""" Choose from: 
        1: Old barn 1km away  
        2: Vancouver Airport 41km away 
        3: USA Border 124km away 
        4: Sir William Osler Elementary School 12km away 
        5: Science World 34km away 
        6: Gas station 10km away: """)
        if destination == ("exit"):
            break
        if destination == ("1"):
            oldbarn = old_barn /  lamborghini_asterion.current_speed
            print ("You are traveling at the speed of " +  str (lamborghini_asterion.current_speed) + (" km per hr"))
            time.sleep(1) 
            lamborghini_asterion.check_gas(1)
            print ("You arrived in " + str (oldbarn) + " hours.")
        if destination == ("2"):
            vancouverairport = vancouver_airport /  lamborghini_asterion.current_speed
            print ("You are traveling at the speed of " +  str (lamborghini_asterion.current_speed) + (" km per hr"))
            time.sleep(7) 
            lamborghini_asterion.check_gas(41)
            print ("You arrived in " + str (vancouverairport) + " hours.")
        if destination == ("3"):
            usaborder = usa_border /  lamborghini_asterion.current_speed
            print ("You are traveling at the speed of " +  str (lamborghini_asterion.current_speed) + (" km per hr"))
            time.sleep(12) 
            lamborghini_asterion.check_gas(124)
            print ("You arrived in " + str (usaborder) + " hours.")
        if destination == ("4"):
            sirwillamoslerelementaryschool = sir_willam_osler_elementary_school /  lamborghini_asterion.current_speed
            print ("You are traveling at the speed of " +  str (lamborghini_asterion.current_speed) + (" km per hr"))
            time.sleep(4) 
            lamborghini_asterion.check_gas(12)
            print ("You arrived in " + str (sirwillamoslerelementaryschool) + " hours.")
        if destination == ("5"):
            scienceworld = science_world /  lamborghini_asterion.current_speed
            print ("You are traveling at the speed of " +  str (lamborghini_asterion.current_speed) + (" km per hr"))
            time.sleep(6) 
            lamborghini_asterion.check_gas(32)
            print ("You arrived in " + str (scienceworld) + " hours.")
        if destination == ("6"):
            gasstation = gas_station /  lamborghini_asterion.current_speed
            print ("You are traveling at the speed of " +  str (lamborghini_asterion.current_speed) + (" km per hr"))
            time.sleep(5) 
            lamborghini_asterion.check_gas(10)
            print ("You arrived in " + str (gasstation) + " hours.")
            lamborghini_asterion.old_gas=lamborghini_asterion.total_gas
            lamborghini_asterion.current_gas = lamborghini_asterion.total_gas
            print ("You refilled you gas tank to 500")


这篇关于如何制作汽车用品,告诉您当前的速度,汽油的百分比等。在Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆