1
resposta

Exercício - Vendas de frutas

import os

#sales_lastmonth = []
'''
This list will be used in next revision to store monthly sales 
'''

def menu_heading(heading):
    '''
    Define the menu Heading for each menu in the program.
    Input: Taken from defined text when the function is applied
    Output: Print the defined heading on the current menu
    '''
    print(heading)

def wrong_value():
    '''
    Warns the user input is invalid and give user another try
    Input: Called in 'def itens()' when user enter a string/bolean value
    Output: Make user aware of mistake ans return  to previous menu
    '''
    os.system('clear')
    input('Enter a whole number. Press Enter to try again\n')
    itens()


def itens():
    '''
    Menu used to collect info from user (amount of itens) and then calculate which entry has the highest value.
    Input: User
    Output: Which of the intens in the list had the highest sales
        In next revision will be added the functionality to append to a list to save the sales records
    '''
    menu_heading('Inventory Update\n')

    try:
        apples= int(input('Enter the amount of apples sold: \n'))
        bananas = int(input('Enter the amount of bananas sold: \n'))
            #total = {'Bananas':bananas, 'Apples':apples}
            #sales_lastmonth.append(total)
    except ValueError:
        wrong_value()
        
    
    if apples > bananas:
        print('Apples sold more than bananas')
    elif apples < bananas:
        print('Bananas sold more than Apples')
    else:
        print('Both itens sold the same amount')
    return

itens()
1 resposta

Oi, Rafael! Como vai? Agradeço por compartilhar seu código com a comunidade Alura.

Com o que você descreveu no código, ficou bem legal ver como você organizou tudo em funções, usou try/except para tratar ValueError e já deixou comentários pensando na próxima revisão com a lista de vendas mensais. Essa estrutura deixa o programa mais fácil de manter e evoluir, por exemplo quando você for guardar os dados em uma lista ou arquivo.

Uma dica interessante para o futuro é usar um dicionário junto com a função max() para descobrir qual fruta vendeu mais sem precisar de tantos if/elif. Veja este exemplo:


sales = {
    'Apples': 120,
    'Bananas': 150
}

most_sold = max(sales, key=sales.get)
print('Fruit with more sales:', most_sold)

Esse código cria um dicionário com as vendas, usa max() para achar a chave com o maior valor e exibe qual fruta vendeu mais. Assim você pode adaptar isso ao seu programa e reaproveitar a lógica de forma simples.

Alura Conte com o apoio da comunidade Alura na sua jornada. Abraços e bons estudos!