python控制输入为数字_将python输入限制为int

1586010002-jmsa.png

How can i make the code limit the input of the user to integers. The code right now works perfect but i want it to instead of giving me an error when it inputs a String, it will just make an exception.

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!')

解决方案

you could convert the input with try statement.

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!')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值