简体   繁体   中英

using same variable in different functions maybe

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM