In: Computer Science
In Python
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.
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