简体   繁体   English

在不同的功能中使用相同的变量

[英]using same variable in different functions maybe

I'm writing my first script in python, it's a currency converter. 我正在用python编写我的第一个脚本,这是一个货币转换器。 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() . 您可以让conversor()接受默认参数,该参数从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. 如果用户决定使用相同的货币返回转换,那么该值将被发送回conversor()并且由于要在呼叫中包含该值,因此将跳过有关使用哪种货币的问题。

You also don't need to (and probably shouldn't) definite voltar() twice: 您也不需要两次(也可能不必)确定voltar()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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