Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Really need help with coverting my pre existing code into what is required. I ha

ID: 3912017 • Letter: R

Question

Really need help with coverting my pre existing code into what is required. I have to use for and while loops. Please help!

Below the assignment I copied my current code.

Part A: Building upon an Existing Solution

For this portion of the lab, you will reuse the program you wrote in Lab 3A. Redesign this solution so that some portions of the code are repeated. In lab 3 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 changes 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.

############################

print('Welcome to the converter program!')

#This program will convert various US measurements to metric measurements

miles = float(input('William, how many miles do you want to convert to kilometers? '))

if miles >= 0:

kilometers = miles * 1.6

print("The distance in kilometers is: ", kilometers)

fahrenheit = float(input('William, what degrees fahrenheit do you want to convert to celsius? '))

if fahrenheit <= 1000:

celsius = (fahrenheit - 32) * 5/9

print("The degrees in celsius is: ", celsius)

gallons = float(input('William, how many gallons do you want to convert to liters? '))

if gallons >= 0:

liters = gallons * 3.9

print("The amount in liters is: ", liters)

pounds = float(input('William, how many pounds do you want to convert to kilograms? '))

if pounds >= 0:

kilograms = pounds * 0.45

print("The weight in kilograms is: ", kilograms)

inches = float(input('William, how many inches do you want to convert to centimeters? '))

if inches >= 0:

centimeters = inches * 2.54

print("The distance in centimeters is: ", centimeters)

else:

print('ERROR - Negative Value [ENDING PROGRAM]')

else:

print('ERROR - Negative Value [ENDING PROGRAM]')

else:

print('ERROR - Negative Value [ENDING PROGRAM]')

else:

print('ERROR - Invalid Value [ENDING PROGRAM]')

else:

print('ERROR - Negative Value [ENDING PROGRAM]')

  

Explanation / Answer


print('Welcome to the converter program!')
counter = 0
miles = float(input('William, how many miles do you want to convert to kilometers? '))
while miles < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
miles = float(input("Enter the correct value for miles "))
counter+=1
if counter > 2:
break
if counter <= 2:
kilometers= miles * 1.6
print(The distance in kilometers is: ", kilometers)
else:
print("Exceeded error count")
counter1 = 0
fahrenheit = float(input('William, what degrees fahrenheit do you want to convert to celsius? '))
while fahrenheit > 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
fahrenheit = float(input("Enter the correct value for fahrenheit "))
counter1+=1
if counter1 > 2:
break
if counter1 <= 2:
celsius = (fahrenheit - 32) * 5/9
print("The degrees in celsius is: ", celsius)
else:
print("Exceeded error count")
counter2 = 0
gallons = float(input('William, how many gallons do you want to convert to liters? '))
while gallons < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
gallons = float(input("Enter the correct value for gallons "))
counter2+=1
if counter2 > 2:
break
if counter2 <= 2:
liters = gallons * 3.9
print("The amount in liters is: ", liters)
else:
print("Exceeded error count")
counter3 = 0
pounds = float(input('William, how many pounds do you want to convert to kilograms? '))
while pounds < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
pounds = float(input("Enter the correct value for pounds "))
counter3+=1
if counter3 > 2:
break
if counter3 <= 2:
kilograms = pounds * 0.45
print("The weight in kilograms is: ", kilograms)
else:
print("Exceeded error count")
counter4 = 0
inches = float(input('William, how many inches do you want to convert to centimeters? '))
while inches < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
inches = float(input("Enter the correct value for miles "))
counter4+=1
if counter4 > 2:
break
if counter4 <= 2:
centimeters = inches * 2.54
print("The distance in centimeters is: ", centimeters)
else:
print("Exceeded error count")