Question

In: Computer Science

In Python b) Modify your program that reads 3 grades from the user (and computes the...

In Python

  • b) Modify your program that reads 3 grades from the user (and computes the average and letter grade) so that it uses a while loop to read the 3 grades. In the loop body, you will just read one grade and update other variables appropriately. The loop header will ensure 3 iterations.
  • c) Modify your program in part b so that it asks the user how many grades there are and uses a while loop to read that many grades.
  • d) Use a for loop instead of a while loop, for the count-controlled loop you did for part b or c

def read_number():
''' Read the number from the user.
Return a real number -- the number entered by the user.
'''
x_str = input("Enter a number (a real number >=0.0):")
y_str = input("Enter a number (a real number >=0.0):")
z_str = input("Enter a number (a real number >=0.0):")
x_float = float(x_str)
y_float = float(y_str)
z_float = float(z_str)
return x_float, y_float, z_float

def the_sum (x_float, y_float, z_float):
''' Calculate the sum and average of the integers.
Parameter:
integer_float -- a real number at least 0.
Return a real number -- the number of sum.
'''
# Define constant:
result_float = x_float + y_float + z_float
return result_float

def average (sum_float):
'''Calculate the sum and average of the integers.
Parameter:
integer_float -- a real number at least 0.
Return a real number -- the number of average.
'''
# Define constant:
average_float = (sum_float)/3
return average_float

def convert_letter_grade ():
if (average_float >=90 and average_float <=100):
print("The grade is A")
  
elif (average_float >=80 and average_float <=90):
print("The grade is B")
  
elif (average_float >=70 and average_float <=80):
print("The grade is C")
  
elif (average_float >=90 and average_float <=100):
print("The grade is D")
  
else:
print("The grade is F")
  
def print_number (average_float):
''' Print the number of sum and average.
Parameter:
sum_float -- a real number at least 0.
average_float -- a real number at least 0.
'''
print("The given numbers are %f %f %f" % (x_float, y_float, z_float))
# Round the average to three decimal places.
print("The equivalent number of average is {:.3f}".format(average_float))

# Main part of the program
# Input (from the user): number from the user
# Output (to the screen): number of sum and average
# Assumptions: The user will enter valid input, i.e. a non-negative number.

# Read the number from the user.
x_float, y_float, z_float = read_number()


# Calculate the average.
sum_float = the_sum (x_float, y_float, z_float)
average_float = average (sum_float)

# Print the number of sum and average.
print_number (average_float)
convert_letter_grade()
# End of main part of the program.

Solutions

Expert Solution

Python Code :

b)Modify your program that reads 3 grades from the user (and computes the average and letter grade) so that it uses a while loop to read the 3 grades. In the loop body, you will just read one grade and update other variables appropriately. The loop header will ensure 3 iterations.

Code

def the_sum (x_float, y_float, z_float):
result_float = x_float + y_float + z_float
return result_float

def average (sum_float):
average_float = (sum_float)/3
return average_float

def convert_letter_grade ():
if (average_float >=90 and average_float <=100):
print("The grade is A")
  
elif (average_float >=80 and average_float <=90):
print("The grade is B")
  
elif (average_float >=70 and average_float <=80):
print("The grade is C")
  
elif (average_float >= 60 and average_float <= 70):
print("The grade is D")
  
else:
print("The grade is F")
  
def print_number (average_float):
print("The given numbers are %f %f %f" % (x_float, y_float, z_float))
print("The equivalent number of average is {:.3f}".format(average_float))

x_float=0
y_float=0
z_float = 0
i = 0
while(i < 3):
x_str = input("Enter a number (a real number >=0.0):")
y_str = input("Enter a number (a real number >=0.0):")
z_str = input("Enter a number (a real number >=0.0):")
x_float = float(x_str)
y_float = float(y_str)
z_float = float(z_str)
sum_float = the_sum (x_float, y_float, z_float)
average_float = average (sum_float)
print_number (average_float)
convert_letter_grade()
i += 1


Output :

c)Modify your program in part b so that it asks the user how many grades there are and uses a while loop to read that many grades.

Code

def the_sum (x_float, y_float, z_float):
result_float = x_float + y_float + z_float
return result_float

def average (sum_float):
average_float = (sum_float)/3
return average_float

def convert_letter_grade ():
if (average_float >=90 and average_float <=100):
print("The grade is A")
elif (average_float >=80 and average_float <=90):
print("The grade is B")
elif (average_float >=70 and average_float <=80):
print("The grade is C")
elif (average_float >= 60 and average_float <= 70):
print("The grade is D")
else:
print("The grade is F")
  
def print_number (average_float):
print("The given numbers are %f %f %f" % (x_float, y_float, z_float))
print("The equivalent number of average is {:.3f}".format(average_float))
x_float=0
y_float=0
z_float = 0
i = 0
user = int(input("Enter how many grades to calculate: "))
while(i < user):
x_str = input("Enter a number (a real number >=0.0):")
y_str = input("Enter a number (a real number >=0.0):")
z_str = input("Enter a number (a real number >=0.0):")
x_float = float(x_str)
y_float = float(y_str)
z_float = float(z_str)
sum_float = the_sum (x_float, y_float, z_float)
average_float = average (sum_float)
print_number (average_float)
convert_letter_grade()
i += 1

Output :

d) Use a for loop instead of a while loop, for the count-controlled loop you did for part b or c

Code

def the_sum (x_float, y_float, z_float):
result_float = x_float + y_float + z_float
return result_float

def average (sum_float):
average_float = (sum_float)/3
return average_float

def convert_letter_grade ():
if (average_float >=90 and average_float <=100):
print("The grade is A")
elif (average_float >=80 and average_float <=90):
print("The grade is B")
elif (average_float >=70 and average_float <=80):
print("The grade is C")
elif (average_float >= 60 and average_float <= 70):
print("The grade is D")
else:
print("The grade is F")
  
def print_number (average_float):
print("The given numbers are %f %f %f" % (x_float, y_float, z_float))
print("The equivalent number of average is {:.3f}".format(average_float))
x_float=0
y_float=0
z_float = 0
i = 0
user = int(input("Enter how many grades to calculate: "))
for i in range(user):
x_str = input("Enter a number (a real number >=0.0):")
y_str = input("Enter a number (a real number >=0.0):")
z_str = input("Enter a number (a real number >=0.0):")
x_float = float(x_str)
y_float = float(y_str)
z_float = float(z_str)
sum_float = the_sum (x_float, y_float, z_float)
average_float = average (sum_float)
print_number (average_float)
convert_letter_grade()

Output :

Please up vote.Thank you


Related Solutions

Python! Create a program that does the following: Reads a number from the user. Calls a...
Python! Create a program that does the following: Reads a number from the user. Calls a function that finds all the divisors of that number. Calls another function to see if the number 7 is a divisor of the original number. Keeps reading input from the user and calling the function above until the user enters the letter ‘q’. Create 2 functions findDivisors() and lucky7(). Use docstrings to explain what each function does. Use the help() function to output the...
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
Program must use Python 3 Your program must have a welcome message for the user. Your...
Program must use Python 3 Your program must have a welcome message for the user. Your program must have one class called CashRegister. Your program will have an instance method called addItem which takes one parameter for price. The method should also keep track of the number of items in your cart. Your program should have two getter methods. getTotal – returns totalPrice getCount – returns the itemCount of the cart Your program must create an instance of the CashRegister...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Design a program that asks the user for a number and the program computes the factorial...
Design a program that asks the user for a number and the program computes the factorial of that number and displays the result . Implement with two different modules - one that uses a for loop and one that uses a while loop Grading Rubrick Program Compiles Cleanly  syntax errors25 pts-5 per errorProgram runs without runtime errors ( validation)run-time errors 25 pts-5 per errorProgram give correct answersLogic errors30 pts-5 per errorProgram is repeatableNot repeatable5 pts-5 ptsProgram is well modularizedBarely Modularized10 pts-...
Write a program that reads in the radius and length of a cylinder and computes volume...
Write a program that reads in the radius and length of a cylinder and computes volume using the following formulas: area = radius * radius * PI volume = area * length
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
*C++* /* This program reads a movie rating from 1 to 10 from the user and...
*C++* /* This program reads a movie rating from 1 to 10 from the user and prints a corresponding word for the rating. Programmer: Your name here Date:       Current date here */ #include #include using namespace std; int main() {       int rating; // user entered rating       cout << "Enter your movie rating (an integer from 1 to 10): ";       cin >> rating;       cout << "\nThe movie was ";       // Select the appropriate word for the...
Design and implement a program that reads a series of 10 integers from the user and...
Design and implement a program that reads a series of 10 integers from the user and prints their average. Read each input value as a string, and then attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException (meaning that the input is not a valid number), print an appropriate error message and prompt for the number again. Continue reading values until 10 valid integers have been entered.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT