1

I'm writing my first script in python, it's a currency converter. there's only one last think I need but I can't get it to work.

here's the script

print "                               Conversor de moeda"
print "                                      by DB \n"
def voltar():
     opcao=raw_input("--------------------------------------------------------------------------\nPara converter outro valor Inserir 1 \nPara voltar ao menu     Inserir 2")
     if opcao == "1":
          pass
     elif opcao == "2":
          pass
     else:
          voltar()     
def conversor():
     tipo_conv=raw_input("Inserir o número correspondente ao tipo de conversão desejado e carregar no enter: \n1 - Euros -> Dólares  \n2 - Dólares -> Euros \n3 - Euros -> Libras  \n4 - Libras -> Euros \n") 
     if tipo_conv == "1":
          qtd=input("Inserir quantidade de Euros a converter:")
          qtd2=qtd * 1.09212
          print qtd, "Euros =" , qtd2, "Dólares"
          voltar()
     elif tipo_conv == "2":
          qtd=input("Inserir quantidade de Dólares a converter:")
          qtd2=qtd * 0.915650
          print qtd, "Dólares =" , qtd2, "Euros"
          voltar()
     elif tipo_conv == "3":
          qtd=input("Inserir quantidade de Euros a converter:")
          qtd2=qtd * 0.751910 
          print qtd, "Euros =" , qtd2, "Libras"
          voltar()
     elif tipo_conv == "4":
          qtd=input("Inserir quantidade de Libras a converter:")
          qtd2=qtd * 1.32995
          print qtd, "Libras =" , qtd2, "Euros"
          voltar()
     else:
          print "Erro. Escolher uma das quatro opções disponíveis"
          conversor()
def voltar():
     opcao=raw_input("--------------------------------------------------------------------------\nPara converter outro valor - Inserir 1 \nPara voltar ao menu - Inserir 2 \n--------------------------------------------------------------------------\n")
     if opcao == "1":
          pass
     elif opcao == "2":
          conversor()
     else:
          voltar() 



conversor()

it first asks the user to choose from the menu what kind of conversion they want. then it asks the amount they want to convert. after that it asks if they want to convert another amount or go back to the menu. I made the go back to the menu part work but can't write the part to go back to converting another amount of the previously converted coin. Any ideas?

1 Answer 1

1

You can let conversor() take a default argument, which gets sent to it from voltar(). If the user decides to go back the conversion with the same currency, that value is then sent back to conversor() and the question about which currency to use is skipped since this value was included in the call.

You also don't need to (and probably shouldn't) definite voltar() twice:

print "                               Conversor de moeda"
print "                                      by DB \n"
def voltar(tipo_conv=None):
     opcao=raw_input("--------------------------------------------------------------------------\nPara converter outro valor - Inserir 1 \nPara voltar ao menu - Inserir 2 \n--------------------------------------------------------------------------\n")
     if opcao == "1":
          conversor(tipo_conv)
     elif opcao == "2":
          conversor()
     else:
          voltar()  

def conversor(tipo_conv=None):
     if not tipo_conv:
         tipo_conv=raw_input("Inserir o número correspondente ao tipo de conversão desejado e carregar no enter: \n1 - Euros -> Dólares  \n2 - Dólares -> Euros \n3 - Euros -> Libras  \n4 - Libras -> Euros \n") 
     if tipo_conv == "1":
          qtd=input("Inserir quantidade de Euros a converter:")
          qtd2=qtd * 1.09212
          print qtd, "Euros =" , qtd2, "Dólares"
          voltar('1')
     elif tipo_conv == "2":
          qtd=input("Inserir quantidade de Dólares a converter:")
          qtd2=qtd * 0.915650
          print qtd, "Dólares =" , qtd2, "Euros"
          voltar('2')
     elif tipo_conv == "3":
          qtd=input("Inserir quantidade de Euros a converter:")
          qtd2=qtd * 0.751910 
          print qtd, "Euros =" , qtd2, "Libras"
          voltar('3')
     elif tipo_conv == "4":
          qtd=input("Inserir quantidade de Libras a converter:")
          qtd2=qtd * 1.32995
          print qtd, "Libras =" , qtd2, "Euros"
          voltar('4')
     else:
          print "Erro. Escolher uma das quatro opções disponíveis"
          conversor()

voltar()
Sign up to request clarification or add additional context in comments.

4 Comments

You solved my problem but created a new problem, when I run it, it starts asking the "convert another amount or go to menu" part when it should start with the menu of different conversions.
Then instead of initially calling voltar(), call conversor()
great, problem solved. Do you mind explaining me why (tipo_conv=None) and "if not tipo_conv:" works? I'm new to this and haven't learned those concepts yet
When conversor() is called it can take an optional argument, tipo_conv. If you don't supply this argument then it defaults tipo_conv to None and that first if not tipo_conv statement is checking for that condition. So essentially your function is saying "If someone calls me and sends a tipo_conv (conversor('1')) then I won't ask for a new one, but if they call me without a tipo_conv (conversor()) then I need to ask for one."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.