In: Computer Science
For this portion of the lab, you will reuse the Python program
you wrote before.
That means you will open the lab you wrote in the previous
assignment and change it. You should NOT start from scratch. Also,
all the requirements/prohibitions from the previous lab MUST also
be included /omitted from this lab.
Redesign the solution so that some portions of the code are
repeated. In lab 4 you validated input to ensure that the user
entered inputs within certain values. If the user entered an
invalid value, the program terminated. Now you will add a loop such
that the user gets three chances to enter a valid value. If the
user enters an invalid value more than three times in a row, the
program should issue an error message and terminate.
The program I wrote before is shown below.
How can I apply the new requirements to reuse this program?
#Get a value from user.
Miles = float(input('How many miles would you like to convert into
kilometers: '))
#Condition
if Miles >= 0:
#Convert miles to kilomters
Kilometers = Miles * 1.6
#Display result
print(Miles,"miles is equal to", Kilometers,"kilometers.")
#Get a value from user.
Gallons = float(input('How many gallons would you like to convert
into liters: '))
#Condition
if Gallons >= 0:
#Convert gallons to liters
Liters = Gallons * 3.9
#Display result
print(Gallons,"gallons is equal to", Liters,"liters.")
#Get a value from user.
Pounds = float(input('How many pounds would you like to convert
into kilograms: '))
#Condition
if Pounds >= 0:
#Convert pounds to kilograms
Kilograms = Pounds * 0.45
#Display result
print(Pounds,"pounds is equal to", Kilograms,"kilograms.")
#Get a value from user.
Inches = float(input('How many inches would you like to convert
into centimeters: '))
#Condition
if Inches > 0:
#Convert inches to centimeters
Centimeters = Inches * 2.54
#Display result
print(Inches,"inches is equal to",Centimeters,"centimeters.")
#Get a value from user.
Fahrenheit = float(input('How many fahrenheit would you like to
convert into celsius: '))
#Condition
if Fahrenheit < 1000:
#Convert fahrenheits to celsius
Celsius =(Fahrenheit - 32) * 5/9
#Display result
print(Fahrenheit,"fahrenheits is equal
to",Celsius,"celsius.")
else:
#Display error message
print('Invalid value!')
else:
#Display error message
print('Invalid value!')
else:
#Display error message
else:
#Display error message
print('Invalid value!')
else:
#Display error message
print('Invalid value!')
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
# Get a value from user.
Miles = float(input('How many miles would you like to
convert into kilometers: '))
#attempts remaining to get a valid input
attempts_remaining=3
#looping if input value is less than 0
while Miles<0:
#displaying error message and how many
attempts are left
print('Invalid input! You have
'+str(attempts_remaining)+' chance(s) to enter a
valid input')
#reading value again
Miles=float(input('How many miles
would you like to convert into kilometers: '))
#if value is valid, exiting loop
if Miles>=0:
break
#otherwise, decrementing
attempts_remaining
attempts_remaining-=1
#if no more attempts remaining, displaying
error and exiting
if
attempts_remaining==0:
print("You
entered wrong input many times. Program is
terminated")
exit(0) #exiting
program
#if valid input is entered, converting miles to
kilomters
Kilometers = Miles * 1.6
# Display result
print(Miles, "miles is equal to", Kilometers,
"kilometers.")
# Get a value from user.
Gallons =
float(input('\nHow many
gallons would you like to convert into liters: '))
#attempts remaining to get a valid input
attempts_remaining=3
#looping if input value is less than 0
while Gallons<0:
#displaying error message and how many
attempts are left
print('Invalid input! You have
'+str(attempts_remaining)+' chance(s) to enter a
valid input')
#reading value again
Gallons=float(input('How many
gallons would you like to convert into liters: '))
#if value is valid, exiting loop
if Gallons>=0:
break
#otherwise, decrementing
attempts_remaining
attempts_remaining-=1
#if no more attempts remaining, displaying
error and exiting
if
attempts_remaining==0:
print("You
entered wrong input many times. Program is
terminated")
exit(0) #exiting
program
# Convert gallons to liters
Liters = Gallons * 3.9
# Display result
print(Gallons, "gallons is equal to", Liters,
"liters.")
# Get a value from user.
Pounds =
float(input('\nHow many
pounds would you like to convert into kilograms: '))
#attempts remaining to get a valid input
attempts_remaining=3
#looping if input value is less than 0
while Pounds<0:
#displaying error message and how many
attempts are left
print('Invalid input! You have
'+str(attempts_remaining)+' chance(s) to enter a
valid input')
#reading value again
Pounds=float(input('How many pounds
would you like to convert into kilograms: '))
#if value is valid, exiting loop
if Pounds>=0:
break
#otherwise, decrementing
attempts_remaining
attempts_remaining-=1
#if no more attempts remaining, displaying
error and exiting
if
attempts_remaining==0:
print("You
entered wrong input many times. Program is
terminated")
exit(0) #exiting
program
# Convert pounds to kilograms
Kilograms = Pounds * 0.45
# Display result
print(Pounds, "pounds is equal to", Kilograms,
"kilograms.")
# Get a value from user.
Inches =
float(input('\nHow many
inches would you like to convert into centimeters:
'))
#attempts remaining to get a valid input
attempts_remaining=3
#looping if input value is less than 0
while Inches<0:
#displaying error message and how many
attempts are left
print('Invalid input! You have
'+str(attempts_remaining)+' chance(s) to enter a
valid input')
#reading value again
Inches=float(input('How many inches
would you like to convert into centimeters: '))
#if value is valid, exiting loop
if Inches>=0:
break
#otherwise, decrementing
attempts_remaining
attempts_remaining-=1
#if no more attempts remaining, displaying
error and exiting
if
attempts_remaining==0:
print("You
entered wrong input many times. Program is
terminated")
exit(0) #exiting
program
# Convert inches to centimeters
Centimeters = Inches * 2.54
# Display result
print(Inches, "inches is equal to", Centimeters,
"centimeters.")
# Get a value from user.
Fahrenheit =
float(input('\nHow many
fahrenheit would you like to convert into celsius:
'))
#attempts remaining to get a valid input
attempts_remaining=3
#looping if input value is greater than or equal to
1000
while Fahrenheit>=1000:
#displaying error message and how many
attempts are left
print('Invalid input! You have
'+str(attempts_remaining)+' chance(s) to enter a
valid input')
#reading value again
Fahrenheit=float(input('How many
fahrenheit would you like to convert into celsius:
'))
#if value is valid, exiting loop
if
Fahrenheit<1000:
break
#otherwise, decrementing
attempts_remaining
attempts_remaining-=1
#if no more attempts remaining, displaying
error and exiting
if
attempts_remaining==0:
print("You
entered wrong input many times. Program is
terminated")
exit(0) #exiting
program
# Convert fahrenheits to celsius
Celsius = (Fahrenheit - 32) * 5 / 9
# Display result
print(Fahrenheit, "fahrenheits is equal to",
Celsius, "celsius.")
#output
How many miles would you like to convert into kilometers: -2
Invalid input! You have 3 chance(s) to enter a valid input
How many miles would you like to convert into kilometers: -4
Invalid input! You have 2 chance(s) to enter a valid input
How many miles would you like to convert into kilometers: -2673
Invalid input! You have 1 chance(s) to enter a valid input
How many miles would you like to convert into kilometers: 1000
1000.0 miles is equal to 1600.0 kilometers.
How many gallons would you like to convert into liters: -2
Invalid input! You have 3 chance(s) to enter a valid input
How many gallons would you like to convert into liters: 20
20.0 gallons is equal to 78.0 liters.
How many pounds would you like to convert into kilograms: -1
Invalid input! You have 3 chance(s) to enter a valid input
How many pounds would you like to convert into kilograms: -2
Invalid input! You have 2 chance(s) to enter a valid input
How many pounds would you like to convert into kilograms: -23
Invalid input! You have 1 chance(s) to enter a valid input
How many pounds would you like to convert into kilograms: -134
You entered wrong input many times. Program is terminated