Question

In: Computer Science

For this portion of the lab, you will reuse the Python program you wrote before. That...

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

Solutions

Expert Solution

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


Related Solutions

For this portion of the lab, you will reuse the program you wrote before. That means...
For this portion of the lab, you will reuse the 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 in the following manner: 1. Create a menu and ask the user which of the following conversions they wish to perform: a. Miles to kilometers...
Description: In this program, you'll reuse the Monster data type you wrote in LA11A. Then, you'll...
Description: In this program, you'll reuse the Monster data type you wrote in LA11A. Then, you'll write a function that accepts a Monster as an argument by reference, then print the Monster to the screen. Again, this will be structured as a guided multiple choice quiz where you will choose the appropriate code for a program. After you have gotten a perfect score, write out the program and compile and run it to see it in action. Instructions: Choose the...
For this portion of Lab #2, write a program that prompts the user to enter ten...
For this portion of Lab #2, write a program that prompts the user to enter ten integer values between 1 and 100, determines the smallest and largest values entered by the user as well as the average of all the numbers entered by the user (expressed as a floating-point number), and then prints the smallest number, the largest number, and the average. Print each of these values with descriptive labels. Your program output should resemble the following (the user's input...
Summary In this lab, you complete a prewritten Python program that computes the largest and smallest...
Summary In this lab, you complete a prewritten Python program that computes the largest and smallest of three integer values. The three values are -50, 53, 78. Instructions Two variables named largestand smallest are assigned for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if statements, or elifstatements as appropriate....
Writing a Modular Program in Python In this lab, you add the input and output statements...
Writing a Modular Program in Python In this lab, you add the input and output statements to a partially completed Python program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared...
in python, sorry i thought i wrote it Write an interactive program that repeatedly asks the...
in python, sorry i thought i wrote it Write an interactive program that repeatedly asks the user to input a number until”Enter” is hit. Your program should create a file named “numbers.txt” where all the numbersare written one below the other, and the last line should display the sum of all the input, afterthe string “The sum of your numbers is ”.For example, if the user inputs 3, 5, 7, 10, -3, 5.2, then the file “numbers.txt” should contain: 3...
PYTHON In this lab we will design a menu-based program. The program will allow users to...
PYTHON In this lab we will design a menu-based program. The program will allow users to decide if they want to convert a binary number to base 10 (decimal) or convert a decimal number to base 2 (binary). It should have four functions menu(), reverse(), base2(), and base10(). Each will be outlined in detail below. A rubric will be included at the bottom of this document. Menu() The goal of menu() is to be the function that orchestrates the flow...
In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to...
In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to a sqlite database. Create a table called Horses with the following fields: id (integer): a primary key and not null name (text) breed (text) height (real) birthday (text) Next, insert the following data row into the Horses table: id: 1 name: 'Babe' breed: 'Quarter Horse' height: 15.3 birthday: '2015-02-10' Output all records from the Horses table. Ex: With the above row inserted, the output...
For this portion of the lab you will design the solution so that you perform some...
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...
6.23 LAB: Python insert/update sqlite3 datafiles Given is a Python program that connects to a sqlite...
6.23 LAB: Python insert/update sqlite3 datafiles Given is a Python program that connects to a sqlite database and has one table called writers with two columnns: name - the name of a writer num - the number of works the writer has written The writers table originally has the following data: name, num Jane Austen,6 Charles Dickens,20 Ernest Hemingway,9 Jack Kerouac,22 F. Scott Fitzgerald,8 Mary Shelley,7 Charlotte Bronte,5 Mark Twain,11 Agatha Christie,73 Ian Flemming,14 J.K. Rowling,14 Stephen King,54 Oscar Wilde,1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT