Made a program that selects the minimum price from the presented. But with the output of 92 and 95, under the line print ('По запросу мы нашли лучшую цену!', gaz_92(), ' руб') extra number is displayed, help please get rid of it. And if it's not difficult tell me how you can remove the numerous if in the code. Code:

 price_gazprom = {'92': 41.10, '95': 44.20, '95+': 45.60, '100+': 51.40, 'ДТ': 47.75, 'Пропан': 21.20} price_rosneft = {'92': 43.00, '95': 45.00, 'ДТ': 48.10} price_novyi_potok={'92': 39.95, '95': 43.40, 'ДТ': 46.95} def gaz_92 (): p_end = 0 p_end = min(price_gazprom['92'], price_rosneft['92'], price_novyi_potok['92']) return p_end def gaz_95 (): p_end = 0 p_end = min(price_gazprom['95'], price_rosneft['95'], price_novyi_potok['95']) return p_end def DT (): p_end = 0 p_end = min(price_gazprom['ДТ'], price_rosneft['ДТ'], price_novyi_potok['ДТ']) return p_end print ('Приветствуем вас в программе подборки лучшей цены на топливо! Все представленные цены действительны на территории г.Тюмень и 300 км от него!') vvod = input('Введите желаемое топливо, а мы сравним все заправки и выберем лучшую цену! Ввод(92,95,95+,100+,ДТ,Пропан): ', ) if vvod == '92': print ('По запросу мы нашли лучшую цену!', gaz_92(), ' руб') if vvod =='95': print ('По запросу мы нашли лучшую цену!', gaz_95(), ' руб') if vvod == 'ДТ': print ('По запросу мы нашли лучшую цену!', DT(), ' руб') if vvod == '95+' or '100+' or 'Пропан': print (price_gazprom[vvod]) 

enter image description here

  • if vvod == '95+' or vvod == '100+' or vvod == 'Пропан': - m0nte-cr1st0
  • Of the three functions gaz_92 , gaz_95 , DT , which do the same thing, you need to do one. If done correctly, no if -a will be needed. - Enikeyschik

2 answers 2

The or operator can only be used with boolean values. Other types will be cast to bool , in particular, any non-empty string is True . That is why the last branch of the condition works regardless of the input.

It will be correct like this:

 if vvod == '95+' or vvod == '100+' or vvod == 'Пропан': print (price_gazprom[vvod]) 

As for the numerous if , here is a detailed answer .


Note that non- if constructions can under certain circumstances slow down the code a lot. Read more here .

  • Thank you, your answer helped me! - Maksim Musienko pm
 price_gazprom = {'92': 41.10, '95': 44.20, '95+': 45.60, '100+': 51.40, 'ДТ': 47.75, 'Пропан': 21.20} price_rosneft = {'92': 43.00, '95': 45.00, 'ДТ': 48.10} price_novyi_potok={'92': 39.95, '95': 43.40, 'ДТ': 46.95} def get_min_price(inp): return min(price_gazprom[str(inp)], price_rosneft[str(inp)], price_novyi_potok[str(inp)]) print ('Приветствуем вас в программе подборки лучшей цены на топливо! Все представленные цены действительны на территории г.Тюмень и 300 км от него!') inp = input('Введите желаемое топливо, а мы сравним все заправки и выберем лучшую цену! Ввод(92,95,95+,100+,ДТ,Пропан): ', ) if inp == '95+' or inp == '100+' or inp == 'Пропан': print (price_gazprom[inp]) else: print('По запросу мы нашли лучшую цену!', get_min_price(inp), 'руб')