【问题标题】:limit python input to int将python输入限制为int
【发布时间】:2017-03-30 00:40:59
【问题描述】:

如何使代码将用户的输入限制为整数。代码现在可以完美运行,但我希望它在输入字符串时不会给我一个错误,它只会产生一个异常。

def miles_to_km():
        miles=eval(input('Amount of miles: '))
        result= float(miles*1.609344)
        print(miles, 'Miles are equivalent to ', result, 'Kilometers')
        print('----------------------------------------------------------------')
def km_to_miles():
        km=eval(input('Amount of kilometers: '))
        result= float(km*0.62137119)
        print(km, 'Kilometers are equivalent to ', result, 'Miles')
        print('----------------------------------------------------------------')
def pounds_to_kg():
        pounds=eval(input('Amount of pounds: '))
        result=float(pounds/2.2046226218)
        print(pounds, 'Pounds are equivalent to ', result, 'Kilograms')
        print('----------------------------------------------------------------')
def kg_to_pounds():
        kg=eval(input('Amount of kilograms: '))
        result=float(kg*2.2046226218)
        print(kg, 'Kilograms are equivalent to ', result, 'Pounds')
        print('----------------------------------------------------------------')
def C_to_F():
    temp=eval(input('Temperature in Celsius: '))
    result= float((temp*1.8)+32)
    print(temp, '°C are equivalent to ', result, '°F')
    print('----------------------------------------------------------------')
def F_to_C():
    temp=eval(input('Temperature in Fahrenheit '))
    result= float((temp-32)/1.8)
    print(temp, '°F are equivalent to ', result, '°C')
    print('----------------------------------------------------------------')
def mph_to_kph():
        mph=eval(input('Amount of Miles per hour: '))
        result= float(mph*1.609344)
        print(mph, 'Miles/Hour are equivalent to ', result, 'Kilometers/Hour')
        print('----------------------------------------------------------------')
def kph_to_mph():
        kph=eval(input('Amount of Kiometers per hour: '))
        result= float(kph*0.62137119)
        print(kph, 'Kilometers/Hour are equivalent to ', result, 'Miles/Hour')
        print('----------------------------------------------------------------')
def exit_function():
    print('Thanks for using the conversion program.')
    print('----------------------------------------------------------------')
    import sys
    sys.exit()


def menu():
    print('----------------------------------------------------------------')
    print('Welcome to the unit conversion program. Please select an option.')
    print('1. Miles to Kilometers')
    print('2. Kilometers to miles')
    print('3. Pounds to Kilograms')
    print('4. Kilograms to Pounds')
    print('5. Celsius to Fahrenheit')
    print('6. Fahrenheit to Celsius')
    print('7. Miles/hour to Kilometers/hour')
    print('8. Kilometers/hour to Miles/Hour')
    print('9. Exit')

screen=True

while screen:
    menu()
    choice=eval(input('Enter option: '))
    if choice==1:
        miles_to_km()
    elif choice==2:
        km_to_miles()
    elif choice==3:
        pounds_to_kg()
    elif choice==4:
        kg_to_pounds()
    elif choice==5:
        C_to_F()
    elif choice==6:
        F_to_C()
    elif choice==7:
        mph_to_kph()
    elif choice==8:
        kph_to_mph()
    elif choice==9:
        exit_function()
    else:
        print('Try a number between 1 and 9!')

【问题讨论】:

    标签: python input user-input


    【解决方案1】:

    input() 将始终返回一个字符串。所以你可以写一个这样的辅助函数:

    def input_int(message):
        while True:
            try:
                number = int(input(message))
                return number
            except ValueError:
                print('Invalid number string, try again')
                continue
    

    类似的帮助器可以 input_float 等。

    毫无疑问,如果您需要,Python 包还有更多花里胡哨的功能。

    【讨论】:

      【解决方案2】:

      您可以使用try 语句转换输入。

      while True:
          menu()
          choice = input('Enter option: ')
          try:
              choice = int(choice)
          except ValueError:
              print('invaild choice.')
              print('Try a number between 1 and 9!')
              continue
          if choice==1:
              miles_to_km()
          elif choice==2:
              km_to_miles()
          elif choice==3:
              pounds_to_kg()
          elif choice==4:
              kg_to_pounds()
          elif choice==5:
              C_to_F()
          elif choice==6:
              F_to_C()
          elif choice==7:
              mph_to_kph()
          elif choice==8:
              kph_to_mph()
          elif choice==9:
              exit_function()
          else:
              print('Try a number between 1 and 9!')
      

      【讨论】:

      • 感谢 XD,我刚刚发布了固定代码作为答案
      【解决方案3】:
      while screen:
          menu()
      while True:
             try:
      
          choice=eval(input('Enter option: '))
          if choice==1:
              miles_to_km()
          elif choice==2:
              km_to_miles()
          elif choice==3:
              pounds_to_kg()
          elif choice==4:
              kg_to_pounds()
          elif choice==5:
              C_to_F()
          elif choice==6:
              F_to_C()
          elif choice==7:
              mph_to_kph()
          elif choice==8:
              kph_to_mph()
          elif choice==9:
              exit_function()
          else:
              print('Try a number between 1 and 9!')
      

      【讨论】:

        猜你喜欢
        • 2020-03-20
        • 2013-05-27
        • 1970-01-01
        • 1970-01-01
        • 2015-02-16
        • 2013-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多