Python 如何使用break语句

Python 如何使用break语句,python,python-2.7,Python,Python 2.7,我在2.7中有一个可用的货币转换器,但我想确保程序不会得到用户输入的无法处理的数据 如何理解与案例无关的用户输入 如果用户输入错误,如何让程序重新启动;i、 e.休息一下,但尽管四处搜索并测试了一些方法,我还是无法找到如何做到这一点 我留下了代码的其余部分,因为它实际上是使用预设数字的第一组乘法的副本 currency = str(raw_input ("""what currency would you like to covert: GBP, EURO, USD OR YEN? """))

我在2.7中有一个可用的货币转换器,但我想确保程序不会得到用户输入的无法处理的数据

  • 如何理解与案例无关的用户输入
  • 如果用户输入错误,如何让程序重新启动;i、 e.休息一下,但尽管四处搜索并测试了一些方法,我还是无法找到如何做到这一点
  • 我留下了代码的其余部分,因为它实际上是使用预设数字的第一组乘法的副本

    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?
                          """))
    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)
    
    1) 您可以使用相同的大小写来比较用户输入字符串

    if currency.lower()=“gbp”

    if currency.upper()=“GBP”

    2) 您可以在while循环中运行程序,这样,如果不满足条件,您可以
    继续
    到循环的下一次迭代(这将从一开始重新启动程序)


    像这样的东西会帮助你开始

        valid_input = ('EUR', 'GBP', 'USD', 'JPY')
    
        while True:
            # Obtain user data
    
            # Make sure all its in caps
            currency = currency.upper()
            exchange = exchange.upper()
    
            if currency in valid_input and exchange in valid_input:
                break 
    
             print ("Error Invalid input, try again...")
    
        # Proccess data...
    

    没有循环。您不能将
    从非循环中断开。不相关:
    str(原始输入(…)
    是冗余的,因为
    raw\u输入
    返回字符串
    int(input())
    应该是
    int(raw\u input())
    。感谢您回答关于第二部分的问题,是否有一个函数可以表示没有错误?例如,当true=errors时:重新启动程序
        valid_input = ('EUR', 'GBP', 'USD', 'JPY')
    
        while True:
            # Obtain user data
    
            # Make sure all its in caps
            currency = currency.upper()
            exchange = exchange.upper()
    
            if currency in valid_input and exchange in valid_input:
                break 
    
             print ("Error Invalid input, try again...")
    
        # Proccess data...