Python货币转换器使用具有两个功能的函数

时间:2016-03-23 22:30:10

标签: function converter currency

def main():

Print_menu()
currency_converter()

1)Print_menu函数应为void且无参数

def Print_menu():

print('Please choose from the menu.')       
print('============================')       
print('1: Convert between USD and EUR')     
print('2: Convert between USD and Canada')  
print('3: Convert between USD and UK (GBP)')
print('4: Convert between USD and China')   
print('5: Quit')                            
print('============================')       
menu = int(input('Enter your Choice: '))

2)此currency_converter函数需要两个参数,即汇率和

货币单位的名称。这可能是无效的,需要汇率和单位

计算并打印结果。我在编写代码时非常陌生,并且无法想象如何调用这些函数来创建一个工作程序。感谢任何输入

def currency_converter():

while menu >=1 and menu <=4:
    if menu == 1:
         menu = int(input('Enter the amount in dollar: '))
         men1 = menu 
         men1 = menu * 91/100 
         print('\t','$', menu,'is', format(men1,'.2f'),'euro')
    elif menu == 2:
        menu = int(input('Enter the amount in dollar: '))
        men1 = menu 
        men1 = menu * 137/100 
        print('\t','$', menu, 'is', format(men1,'.2f'), 'cad')
    elif menu == 3:
        menu = int(input('Enter the amount in dollar: '))
        men1 = menu 
        men1 = menu * 72/100 
        print('\t','$', menu, 'is', format(men1,'.2f'), 'pound')
    elif menu == 4:
        menu = int(input('Enter the amount in dollar: '))
        men1 = menu 
        men1 = menu * 653/100 
        print('\t','$', menu, 'is', men1, 'yuan')

if menu == 5:
    print('Thank you for using my program.')

else:
    print(menu, 'is an invalid choice.')

3)一旦选择了货币,我需要一个while循环来询问是否要转换为 -

另一个单位。我目前仍在撰写这篇文章,并且相信我能做到这一点。

main()的

1 个答案:

答案 0 :(得分:0)

我会在Print_menu中添加一个return语句来返回菜单值并将def currency_converter()更改为def currency_converter(menu):然后在main函数内调用另一个。

def main():
    currency_converter(Print_menu())

if __name__==__main__:main()