有效循环此程序的最佳方法是什么?

时间:2017-12-12 02:11:59

标签: python

循环此程序的最佳方法是什么。我才开始学习编码。我希望它在执行后保持循环,直到用户按5退出 这是我的代码

# Naira conversion calculations
def NG_USD(choice):
    print(str(user/350)+"USD")
def NG_GBP(choice):
    print(str(choice/400)+"GBP")
def NG_EUR(choice):
    print(str(choice/450)+"EUR")

# Dollar conversion calculations
def USD_NG(choice):
    print(str(choice*350)+"NG")
def USD_GBP(choice):
    print(str(choice*0.75)+"GBP")
def USD_EUR(choice):
    print(str(choice*0.85)+"EUR")

# Pounds conversion calculations
def GBP_USD(choice):
    print(str(choice/0.75)+"USD")
def GBP_NG(choice):
    print(str(choice*400)+"NG")
def GBP_EUR(choice):
    print(str(choice*1.13)+"EUR")

# Euro conversion calculations
def EUR_NG(choice):
    print(str(choice*450)+"NG")
def EUR_GBP(choice):
    print(str(choice/1.13)+"GBP")
def EUR_USD(choice):
    print(str(choice/0.85)+"USD")


def conversion_NG(user):
    if user == 1 and choice ==1:
        print("You can't convert to the same value")
    elif user == 2 and choice == 1:
        value = eval(input("Enter your value: "))
        return NG_USD(value)
    elif user == 3 and choice == 1:
        value = eval(input("Enter your value: "))
        return NG_GBP(value)
    elif user == 4 and choice == 1:
        value = eval(input("Enter your value: "))
        return NG_EUR(value)

def conversion_USD(user):
    if user == 2 and choice == 2:
        print("You can't convert to the same value")
    elif user == 1 and choice == 2:
        value = eval(input("Enter your value: "))
        return USD_NG(value)
    elif user == 3 and choice == 2:
        value = eval(input("Enter your value: "))
        return USD_GBP(value)
    elif user == 4 and choice == 2:
        value = eval(input("Enter your value: "))
        return USD_EUR(value)

def conversion_GBP(user):
    if user == 3 and choice == 3:
        print("You can't convert to the same value")
    elif user == 1 and choice == 3:
        value = eval(input("Enter your value: "))
        return GBP_NG(value)
    elif user == 2 and choice == 3:
        value = eval(input("Enter your value: "))
        return GBP_USD(value)
    elif user == 4 and choice == 3:
        value = eval(input("Enter your value: "))
        return GBP_EUR(value)
def conversion_EUR(user):
    if user == 4 and choice == 4:
        print("You can't convert to the same value")
    elif user == 1 and choice == 4:
        value = eval(input("Enter your value: "))
        return EUR_NG(value)
    elif user == 2 and choice == 4:
        value = eval(input("Enter your value: "))
        return EUR_USD(value)
    elif user == 3 and choice == 4:
        value = eval(input("Enter your value: "))
        return EUR_GBP(value)

print("This program exchanges currencies for you"
        "\n(1.) NG\n(2.) USD\n(3.) GBP\n(4.) EUR\n(5.) QUIT")
choice = int(input("What currency do you have?: "))
    if choice == 1:
        user = int(input("What currency would you like to convert to?: "))
        string = "\n[You're converting from Naira!]"
        dotted = "\n" + len(string) * "-"
        print(dotted, string, dotted)
        conversion_NG(user)
    elif choice == 2:
        user = int(input("What currency would you like to convert to? "))
        string = "\n[You're converting from US Dollars!]"
        dotted = "\n" + len(string) * "-"
        print(dotted, string, dotted)
        conversion_USD
    elif choice == 3:
        user = int(input("What currency would you like to convert to? "))
        string = "\n[You're converting from British Pounds!]"
        dotted = "\n" + len(string) * "-"
        print(dotted, string, dotted)
        conversion_GBP
    elif choice == 4:
        user = int(input("What currency would you like to convert to? "))
        string = "\n[You're converting from Euro!]"
        dotted = "\n" + len(string) * "-"
        print(dotted, string, dotted)
        conversion_EUR
    elif choice == 5:
        print("Thanks for using\n")
    else:
        print("Error! Invalid Input")

1 个答案:

答案 0 :(得分:0)

尝试将if-else梯形图置于无限循环中,直到用户输入5,然后退出程序

print("This program exchanges currencies for you"
    "\n(1.) NG\n(2.) USD\n(3.) GBP\n(4.) EUR\n(5.) QUIT")
choice = int(input("What currency do you have?: "))
while True:
    if choice == 1:
        user = int(input("What currency would you like to convert to?: "))
        string = "\n[You're converting from Naira!]"
        dotted = "\n" + len(string) * "-"
        print(dotted, string, dotted)
        conversion_NG(user)
    elif choice == 2:
        user = int(input("What currency would you like to convert to? "))
        string = "\n[You're converting from US Dollars!]"
        dotted = "\n" + len(string) * "-"
        print(dotted, string, dotted)
        conversion_USD(user)
    elif choice == 3:
        user = int(input("What currency would you like to convert to? "))
        string = "\n[You're converting from British Pounds!]"
        dotted = "\n" + len(string) * "-"
        print(dotted, string, dotted)
        conversion_GBP(user)
    elif choice == 4:
        user = int(input("What currency would you like to convert to? "))
        string = "\n[You're converting from Euro!]"
        dotted = "\n" + len(string) * "-"
        print(dotted, string, dotted)
        conversion_EUR(user)
    elif choice == 5:
        print("Thanks for using\n")
        break                     #You could also use sys.exit()
    else:
        print("Error! Invalid Input")