我正在学习python,我正在尝试编写一个计算器,它可以比较黄油的价格,计算百分比差异,以及100克黄油的最低价格。你知道吗
def procenta(x,y):
vysledek = (y-x)/x * 100 # (b-a) : a * 100
return round(vysledek, 2)
tesco = float(input("Zadejte cenu masla v Tescu: "))
tesco_g = int(input("Zadejte gramaz v Tescu: "))
lidl = float(input("Zadejte cenu masla v Lidlu: "))
lidl_g = int(input("Zadejte gramaz v Lidlu: "))
kaufland = float(input("Zadejte cenu masla v Kauflandu: "))
kaufland_g = int(input("Zadejte gramaz v Kauflandu: "))
cena_tesco = int(tesco)/(tesco_g[:2]/10)
cena_lidl = int(lidl)/(lidl_g[:2]/10)
cena_kaufland = int(kaufland)/(kaufland_g[:2]/10)
回溯:
Traceback (most recent call last):
File "python", line 6, in <module>
ValueError: invalid literal for int() with base 10: ''
别担心其他的文字是什么意思,这是我的母语,最终可以翻译出来。但是,如果我将其更改为:
cena_tesco = float(tesco/(tesco_g[:2]/10))
我得到TypeError float object is not subscriptable
。你知道吗
如果我能让它工作,它会被打印出来:
按100克计算黄油的价格。 我应该怎么处理乐购g[:2],所以它从乐购g取前两个数字,然后用它们来计算价格?你知道吗
通过使用第5行将
tesco_g
强制转换为整数。TypeError告诉您varname[:2]
语法不起作用,因为您的类型没有索引。重新定义口译员正在做的事情: 1它读tesco_g[:2]
2它检查:tesco_g
类型是否有索引到第二个元素的方法? 三。它没有找到该方法,并抛出一个错误。你知道吗cena_tesco
行应该做什么?你知道吗相关问题 更多 >
编程相关推荐