break和pass函数处理不正确

时间:2013-05-12 09:37:06

标签: python python-2.7

函数break和pass似乎无法正常输入程序停止并且没有正确输入程序决定不打破但打印字符串告诉用户存在问题并继续运行。< / p>

currency = str(raw_input ("""what currency would you like to covert: GBP, EURO, USD OR YEN?
"""))


exchange = str(raw_input("""what currency would you like in exchange? : GBP, EURO, USD OR YEN?
                              """))

amount = int(input("""how much would you like to convert?
                      """))



valid_input = ('EUR','eur','GBP','gbp' ,'USD','usd','JPY','jpy')

while True:

    if currency in valid_input and exchange in valid_input:

        pass

    else:

        print("incorrect input, please restart program")
        break


decision = str(raw_input("""Please enter u for user input exchange rate or s for the preset exchange rate
    """))


if decision == "u" :
    user_rate = raw_input("Please enter the current exchange rate")

    exchange_value = int(amount) *  int(user_rate)

    print ("At the user found exchange rate you will receive",exchange_value,exchange)



    elif decision == "s" :


    if currency  == "GBP" and exchange == "USD":

        exchange_value= int(amount) * 1.6048

        print ("At the preset exchange rate you will receive",exchange_value,exchange)

    if currency  == "GBP" and exchange == "EUR":

        exchange_value= int(amount) * 1.2399

        print ("At the preset exchange rate you will receive",exchange_value,exchange)

2 个答案:

答案 0 :(得分:1)

你的break语句突破你的循环,继续执行程序的其余部分。如果您想要退出该计划,请使用sys.exit

之类的内容

pass根本不做任何事情;它是一个空块的占位符。当你使用它时,它基本上会导致你的循环永远循环。 是你打算使用break的地方。

答案 1 :(得分:-2)

“break”仅适用于开关内和for / while