У меня возникла проблема с программой, в которой она запускает исключение вместе с командой try, и я не знаю, что делать, поскольку она печатает преобразование и сообщение об ошибке.
def main():
print('What would you like to convert?')
print('1. Miles to Kilometers')
print('2. Fahrenheit to Celcius')
print('3. Gallons to Liters')
print('4. Pounds to Kilograms')
print('5. Inches to Centimeters')
try:
option = int(input('Menu Option:'))
if option == 1:
Inptmiles = float(input('Enter number of miles to convert:'))
milestokm(Inptmiles)
usercontinue()
if option == 2:
InptFrnht = float(input('Enter degrees that you would like to convert:'))
FrnhttoClcs(InptFrnht)
usercontinue()
if option == 3:
InptGllns = float(input('Enter number of Gallons you would like to convert:'))
GllnstoLtrs(InptGlls)
usercontinue()
if option == 4:
InptLbs = float(input('Enter number of pounds you would like to convert:'))
LbstoKgs(InptLbs)
usercontinue()
if option == 5:
InptInches = float(input('Enter number of Inches you would like to convert:'))
InchestoCm(InptInches)
usercontinue()
except:
print('Sorry there was an error with your input')
def milestokm(miles):
convkm = miles * 1.6
print(f'{miles} Miles is equal to {convkm:.2f} Kilometers')
def FrnhttoClcs(Fahrenheit):
convClcs = (Fahrenheit - 32) * 5/9
print(f'{Fahrenheit} Degrees Fahrenheit is equal to {convClcs:.2f} Degrees Celcius')
def GllnstoLtrs(Gallons):
convLtrs = Gallons * 3.9
print(f'{Gallons} Gallons is equal to {convLtrs:.2f} Liters')
def LbstoKgs(Pounds):
convKgs = Pounds * 0.45
print(f'{Pounds} Pounds is equal to {convKgs:.2f} Kilograms')
def InchestoCm(Inches):
convCm = Inches * 2.54
print(f'{Inches} Inches is equal to {convCm:.2f} Centimeters')
main()
Я попытался переместить исключение за пределы команд определения, которые использовали сохраненные переменные, но это вернулось как ошибка.
У меня возникла проблема с программой, в которой она запускает исключение вместе с командой try, и я не знаю, что делать, поскольку она печатает преобразование и сообщение об ошибке. [code]def main(): print('What would you like to convert?') print('1. Miles to Kilometers') print('2. Fahrenheit to Celcius') print('3. Gallons to Liters') print('4. Pounds to Kilograms') print('5. Inches to Centimeters') try: option = int(input('Menu Option:')) if option == 1: Inptmiles = float(input('Enter number of miles to convert:')) milestokm(Inptmiles) usercontinue() if option == 2: InptFrnht = float(input('Enter degrees that you would like to convert:')) FrnhttoClcs(InptFrnht) usercontinue() if option == 3: InptGllns = float(input('Enter number of Gallons you would like to convert:')) GllnstoLtrs(InptGlls) usercontinue() if option == 4: InptLbs = float(input('Enter number of pounds you would like to convert:')) LbstoKgs(InptLbs) usercontinue() if option == 5: InptInches = float(input('Enter number of Inches you would like to convert:')) InchestoCm(InptInches) usercontinue() except: print('Sorry there was an error with your input') def milestokm(miles): convkm = miles * 1.6 print(f'{miles} Miles is equal to {convkm:.2f} Kilometers') def FrnhttoClcs(Fahrenheit): convClcs = (Fahrenheit - 32) * 5/9 print(f'{Fahrenheit} Degrees Fahrenheit is equal to {convClcs:.2f} Degrees Celcius') def GllnstoLtrs(Gallons): convLtrs = Gallons * 3.9 print(f'{Gallons} Gallons is equal to {convLtrs:.2f} Liters') def LbstoKgs(Pounds): convKgs = Pounds * 0.45 print(f'{Pounds} Pounds is equal to {convKgs:.2f} Kilograms') def InchestoCm(Inches): convCm = Inches * 2.54 print(f'{Inches} Inches is equal to {convCm:.2f} Centimeters')
main() [/code] Я попытался переместить исключение за пределы команд определения, которые использовали сохраненные переменные, но это вернулось как ошибка.