在python中如何使用两个不同函数的最终值来获得另一个值?

2024-05-15 20:18:25 发布

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

我想开发一个电子计算器:

  1. 将货币转换为单位(KWh)。

  2. 它还需要用户输入来确定他/她在家中使用了多少电器元件,以确定每天的平均用电量。

  3. 在输入和计算这些数据后,它还将显示他/她可以有多少天,而他/她可以再充电(例如:10美元,他可以有100千瓦时,如果平均每天使用10千瓦时,他/她可以有多少天?[日=100kwh/10kwh=10天]

我已经用两个函数解决了1和2的问题,现在我想用这两个函数的最终值来得到一天。我试着划分这些函数的最后一个。有没有可能把这些函数的值除以?你知道吗

1。此函数将货币转换为单位KWh

def money_to_unit():

    recharge=int(input("Enter yor recharge amount : "))

    amount=recharge-(recharge*(5/100)) # after deducting 5% vat

    unit=0

    if amount<=300:   #300 is max value for using 75 Kwh
        unit=amount/4  # 4 is the slab rate for 0-75 Kwh

    elif amount<=981.25:    #981.25 is max value for using 200 Kwh
        unit=75+((amount-300)/5.45) #5.45 is the slab rate for 76-200  Kwh 

    elif amount<=1551.25:    #1551.25 is max value for using 300 Kwh
        unit=75+125+((amount-681.25)/5.7) #5.7 is the slab rate for 201-300 Kwh
    elif amount<=2153.25:
        unit=75+125+100+((amount-1551.25)/6.02)
    elif amount<=4013.25:
        unit=75+125+100+100+((amount-2153.25)/9.30)
    else:
        unit=75+125+100+100+200+((amount-4013.25)/10.7)

    print("Useable amount is :"+ " "+str(round(amount,2))+" "+"Taka")
    print("Useable unit is: "+" "+str(round(unit,2))+" "+"Kwh")

money_to_unit()

2。确定每天的平均用电量(KWh)

def comp():

    light=int(input("Enter the number of light :"))

    watt=int(input("Enter the watt : "))

    hour=int(input("Enter the agerage use of light in hour per day : "))

    consumption=(light*watt*hour)/1000

    print("you total consumption is"+ " " + str(consumption)+ " " + "Kwh per day")

comp()

三。除以money_to_unit()除以comp()。怎么做?你知道吗

(1).500塔卡,可用量475塔卡,可用单位107.11千瓦时

(2).对于每小时20W的5盏灯,每天使用6小时,平均每天使用0.6kWh。你知道吗

(三)。天=107.11 Kwh/0.6 Kwh=178.5天


Tags: the函数forinputisunit单位amount
3条回答

两个函数都应该返回要用于进一步计算的计算值(而不仅仅是打印)。你知道吗

def money_to_unit():
    /* function definition */
    return round(amount,2), round(amount,2)

以及

def comp():
    /* function definition */
    return consumption

然后您可以简单地调用如下函数:

x, y = money_to_unit()
z =  comp()

最后你可以用x,y,z进行任何额外的计算,比如:

result = x/z 
/* or */
result2 = y/z

使用return。为函数指定要执行的任务,而不仅仅是围绕一些脚本。你知道吗

1.第一步定义两个函数:

def recharge_to_amount(recharge, vat):
     return recharge * (1 - vat)

以及

def amount_to_kwh(amount):
    if amount <= 300:
        return amount / 4
    elif amount <= 981.25:
        return 75 + ((amount - 300) / 5.45)
    elif amount <= 1551.25:
        return 75 + 125 + ((amount - 681.25) / 5.7)
    elif amount <= 2153.25:
        return 75 + 125 + 100 + ((amount - 1551.25) / 6.02)
    elif amount <= 4013.25:
        return 75 + 125 + 100 + 100 + ((amount - 2153.25) / 9.30)
    else:
        return 75 + 125 + 100 + 100 + 200 + ((amount - 4013.25) / 10.7)

2.为第二步定义另一个函数:

def consumption(light, watt, hour):
    return light * watt * hour / 1000

3.定义最后一个函数:

def days(watt, cons):
    return watt / cons

然后进行计算:

amount = recharge_to_amount(500)
kwh = amount_to_kwh(amount)
consumption = consumption(5, 20, 6)

days = days(kwh, consumption)
def money_to_unit():

# all your previous code

return round(unit,2)

def comp():

# all your previous code

return consumption

也许:

money_unit = money_to_unit()
consumption = comp()

day = money_unit / consumption
print(round(day,1), "{}".format("day"))

输出:

178.5 day

相关问题 更多 >