Created
January 14, 2012 18:41
-
-
Save moby1br/1612462 to your computer and use it in GitHub Desktop.
Exercicio 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
################################################################################ | |
# | |
# CONSTANTES | |
# | |
################################################################################ | |
TEXTO_CATEGORIA = {'1': 'Eletronicos', '2': 'Moveis', '3': 'Alimentos', '4': 'Roupas', '5': 'Outros'} | |
TEXTO_UNIDADE = {'1': 'Unidade', '2': 'Caixa/Pcte', '3': 'Duzia'} | |
################################################################################ | |
# | |
# ENTRADA DE DADOS | |
# | |
################################################################################ | |
print "SISTEMA DE ENTRADA DE PRODUTOS" | |
print "Por favor insira os dados do produto" | |
codigo = raw_input("Codigo:\n") | |
codigo = codigo.rjust(10,'0') | |
# Garantir que o código do produto seja armazenado com 10 caracteres 0000000000 | |
# Ex: Entrada '123' transforme em '0000000123' | |
# Dica: >>> dir(str) >>> help(str.<metodo>) | |
nome = raw_input("nome\n") | |
nome = nome.capitalize | |
# O nome do produto precisa estar em maiusculas | |
# Ex: Entrada 'Nome do Produto' transforme em 'NOME DO PRODUTO' | |
print """################### | |
#___CATEGORIAS____# | |
# 1 = Eletronicos # | |
# 2 = Moveis # | |
# 3 = Alimentos # | |
# 4 = Roupas # | |
# 5 = Outros # | |
###################""" | |
valido = False | |
while not valido: | |
categoria = raw_input("Categoria:\n") | |
if categoria in ['1','2','3','4','5']: | |
valido = True | |
else: | |
print "Categoria inválida. Tente Novamente" | |
# Deixaremos o tratamento de erros de forma correta para depois, por enquanto | |
# caso o usuário digite uma categoria que não existe iremos exibir uma mensagem | |
# e solicitar que entre novamente com o numero da categoria (uma tentativa) | |
valido = False | |
while not valido: | |
valor_unitario = raw_input("Valor Unitario:\n") | |
if valor_unitario.isdigit(): | |
valido = True | |
else: | |
if valor_unitario.find(',') > 0: | |
valor_unitario = valor_unitario.replace(',','.') | |
valido = True | |
else: | |
if valor_unitario.find('.') > 0: | |
valido = True | |
else: | |
print "Apenas números. Tente Novamente" | |
valor_unitario = float(valor_unitario) | |
# Garantir que o valor seja convertido para float() | |
# Para isso preciso garantir que entre apenas numeros, pontos ou virgulas | |
# caso o usuario entre com , precisamos substituir por . | |
# Isso poderá ser feito com tratamento de Exception, mas por enquanto | |
# Os metodos da classe str podem ajudar | |
print """################### | |
#_____UNIDADE_____# | |
# 1 = Unidade # | |
# 2 = Caixa/Pcte # | |
# 3 = Duzia # | |
###################""" | |
valido = False | |
while not valido: | |
unidade = raw_input("Unidade:\n") | |
if unidade in ['1','2','3']: | |
valido = True | |
else: | |
print "Unidade inválida. Tente Novamente" | |
# Caso o usuario digite um numero de unidade que não existe, faça o mesmo que | |
# fez na categoria | |
# DESENVOLVA A ESTRUTURA CONDICIONAL ABAIXO | |
""" | |
# Teste aqui se a unidade é Caixa/Pcte | |
# Caso positivo | |
if unidade = '2': | |
itens = raw_input("Quantidade de itens por caixa/Pcte:\n") | |
quantidade = raw_input("Quantidade de Caixas/PCtes\n") | |
# Garanta que o valor de itens e quantidade seja convertido para int | |
# Se unidade não for caixa/pcte Teste aqui se a unidade é Duzia | |
itens = 12 | |
quantidade = raw_input("Quantidade de Duzias\n") | |
# Caso contrario | |
itens = 1 | |
quantidade = raw_input("Quantidade:\n")""" | |
if unidade == '2': | |
itens = raw_input("Quantidade de itens por caixa/Pcte:\n") | |
quantidade = raw_input("Quantidade de Caixas/PCtes\n") | |
else: | |
# Garanta que o valor de itens e quantidade seja convertido para int | |
# Se unidade não for caixa/pcte Teste aqui se a unidade é Duzia | |
if unidade == '3': | |
itens = 12 | |
quantidade = raw_input("Quantidade de Duzias\n") | |
else: | |
# Caso contrario | |
itens = 1 | |
quantidade = raw_input("Quantidade:\n") | |
# Convertendo para inteiro | |
itens = int(itens) | |
quantidade = int(quantidade) | |
# Garantir que o peso seja convertido para float | |
# caso o usuario entre com , precisamos substituir por . | |
# dica: dir(str) | |
valido = False | |
while not valido: | |
peso = raw_input("Peso total em quilos:\n") | |
if peso.isdigit(): | |
valido = True | |
else: | |
if peso.find(',') > 0: | |
peso = peso.replace(',','.') | |
valido = True | |
else: | |
print "Apenas números. Tente Novamente" | |
peso = float(peso) | |
################################################################################ | |
# | |
# FINALIZAÇÃO | |
# | |
# Procure sempre converter os tipos quando preciso | |
# | |
################################################################################ | |
#pdb.set_trace() | |
LINHA = "#" * 37 | |
BORDA = "#" + " " * 35 + "#" | |
CONTEUDO = "#" + "%s" + "#" | |
print LINHA | |
print CONTEUDO % "RESUMO DA TRANSAÇÃO".center(35) | |
print BORDA | |
texto = "Código do produto: %s" % codigo | |
print CONTEUDO % texto.center(35) | |
texto = "Categoria: %s" % TEXTO_CATEGORIA[categoria] # Consegue exibir o nome ao invés do numero? | |
print CONTEUDO % texto.center(35) | |
texto = "Valor unitário: %.2f" % float(valor_unitario) | |
print CONTEUDO % texto.center(35) | |
texto = "Unidade: %s" % TEXTO_UNIDADE[unidade] # Consegue exibir o nome ao invés do numero? | |
print CONTEUDO % texto.center(35) | |
# Esta linha só deve imprimir caso seja Caixa ou Duzia | |
if unidade in ['2','3']: | |
texto = "Itens por %s: %s" % (TEXTO_UNIDADE[unidade], itens) | |
print CONTEUDO % texto.center(35) | |
texto = "Quantidade: %s" % int(quantidade) | |
print CONTEUDO % texto.center(35) | |
texto = "Peso: %.3f" % peso | |
print CONTEUDO % texto.center(35) | |
print BORDA | |
print CONTEUDO % "TOTAIS".center(35) | |
# CALCULE AQUI QUANTIDADE * ITENS | |
total_itens = quantidade*itens | |
texto = "Total de itens: %s" % total_itens | |
print CONTEUDO % texto.center(35) | |
# CALCULE AQUI VALOR UNITARIO * TOTAL DE ITENS | |
valor_total = valor_unitario*itens*quantidade | |
texto = "Valor total: %.2f" % valor_total | |
print CONTEUDO % texto.center(35) | |
texto = "Peso total: %.3f" % peso | |
print CONTEUDO % texto.center(35) | |
# CALCULE AQUI O PESO TOTAL DIVIDIDO PELOS TOTAL DE ITENS | |
peso_total = peso/(quantidade*itens) | |
texto = "Peso por item: %.3f" % peso_total | |
print CONTEUDO % texto.center(35) | |
print BORDA | |
print LINHA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment