In: Computer Science
For this portion of the lab you will design the solution so that you perform some conditional tests. For this lab: 1. You will validate input to ensure that the user enters inputs within a certain range or larger than a certain minimum value. You will validate the inputs as follows: (LO 1, 2, 3) a. The user cannot enter a negative number for: i. Miles to kilometers ii. Gallons to liters iii. Pounds to kilograms iv. Inches to centimeters b. The user cannot enter a value above 1000 degrees for Fahrenheit to Celsius (LO1) c. You MUST design a logical program exit. You may NOT use exit, break, quit, or system exit, or ANY OTHER forced exit. Do not use a menu. Use LOGIC to exit the program. 2. If the user enters an invalid value, then the program will issue an error message and terminate immediately. (Do NOT accept further data). 3. Save the program as firstname_lastname_Lab3a.py where you will replace firstname and lastname with your actual first and last name. 4. Test all conditions prior to submitting.
Screenshot of the code:
Sample Output:
#Sample Run 1:
#Sample Run 2:
#Sample Run 3:
#Sample Run 4:
#Sample Run 5:
Code To Copy:
#Run the code in python version 3.x.
#Indent the code before executing as per the above screenshot of the code.
#Otherwise it may give error. Indentation is important in python.
#Prompt the user to enter the miles.
Miles = float(input('Enter the miles to convert into kilometer: '))
#Check the miles.
if Miles >= 0 :
#Convert the miles to kilometers.
Miles_To_km = Miles*1.6
#Display the result.
print (Miles, "miles equivalent to", Miles_To_km, "kilometer.")
#Prompt the user to enter the gallons.
Gallon = float(input('Enter the gallons to convert into liter: '))
#Check the validity of the Gallons entered.
if Gallon >= 0:
#Convert gallons into liters.
Gal_To_Lit = Gallon*3.9
#Display the result.
print (Gallon, "gallons equivalent to", Gal_To_Lit, "liters.")
#Prompt the user to enter the pounds.
Pound = float(input('Enter the pounds to convert into kilograms: '))
#Check the validity of the Pounds entered.
if Pound >= 0:
#Convert pounds into kilograms.
Pounds_To_Kg = Pound*0.45
#Display the result.
print (Pound, "pounds equivalent to", Pounds_To_Kg, "kilograms.")
#Prompt the user to enter the temperature in Fahrenheit.
f = float(input('Enter the temperature in Fahrenheit: '))
#Check the value to be not greater than 1000.
if f < 1000:
#Convert Fahrenheit into celsius.
F_To_C = (f -32)*5/9
#Display the result.
print (f, "Fahrenheit equivalent to", F_To_C, "celsius.")
#Otherwise.
else:
#Display the error message.
print ("Invalid temperature (greater than 1000) !!!")
else:
#Display the error message.
print ("Pounds cannot be negative !!!")
else:
#Display the error message.
print ("Gallons cannot be negative !!!")
else:
#Display the error message.
print ("Miles cannot be negative !!!")