Imagine That You Wanted to Write a Program That Asks The User to Enter in 5 Grade Values

Hi guys, in this article you will learn about a python script that takes the 5 grades you scored in your exams. The process of asking a user to enter the grade keeps repeating until the program gets a total of five grades.

I will use the while loop that keeps repeating the process of asking the user until the total number of grades entered by the user becomes 5. I will also be using a try and expect block to convert the user entered input into an integer until the valid integer entered by the user.

Let's try to write a python script and solve this program.

 

Imagine That You Wanted to Write a Program That Asks The User to Enter in 5 Grade Values. The User May or May Not Enter Valid Grades, And You Want to Ensure That You Obtain 5 Valid Values From The User. Which Nested Loop Structure Would You Use

 

 


Imagine That You Wanted to Write a Program That Asks The User to Enter in 5 Grade Values. The User May or May Not Enter Valid Grades, And You Want to Ensure That You Obtain 5 Valid Values From The User. Which Nested Loop Structure Would You Use

Let's take an empty list variable named as grades and initialize it to an empty list. The grades variable is a type of list containing an integer as its item.

grades = []



Now use the iterate until the list length becomes 5.  Inside the while loop use a try block to ask a user to enter the grade, try to convert the grades to an integer if grade is not an integer, use the expect block to catch a value error Exception and print the appropriate message.

If the user has entered the right integer, using the built in function append() add it to the list using the else block.

while len(grades) < 5:
   try:
       temp = int(input("Enter a Grade : "))
   except ValueError:
       print("You Entered Invalid Grade, Please Enter correct Grade")
   else:
       grades.append(temp)



Once the value completes  the list of integer variables named as greats will store the total 5 grades you achieved in your exams. Use the print function call  to print the grades as shown below.

print("The grades you entered are ...")
print(grades)





Complete Python Program
============================

grades = []
while len(grades) < 5:
   try:
       temp = int(input("Enter a Grade : "))
   except ValueError:
       print("You Entered Invalid Grade, Please Enter correct Grade")
   else:
       grades.append(temp)

print("The grades you entered are ...")
print(grades)





Output 1
==============

Enter a Grade : 34
Enter a Grade : 89
Enter a Grade : 54
Enter a Grade : 87
Enter a Grade : 67
The grades you entered are ...
[34, 89, 54, 87, 67]




Output 2
============

Enter a Grade : 45
Enter a Grade : ee
You Entered Invalid Grade, Please Enter correct Grade
Enter a Grade : 89
Enter a Grade : 64
Enter a Grade : ww
You Entered Invalid Grade, Please Enter correct Grade
Enter a Grade : 49
Enter a Grade : io
You Entered Invalid Grade, Please Enter correct Grade
Enter a Grade : 76
The grades you entered are ...
[45, 89, 64, 49, 76]





Output 3
=================

Enter a Grade : 87
Enter a Grade : 81
Enter a Grade : 53
Enter a Grade : new
You Entered Invalid Grade, Please Enter correct Grade
Enter a Grade : 55
Enter a Grade : 73
The grades you entered are ...
[87, 81, 53, 55, 73]




Conclusion
================

Execute the above Python program by providing the random integer and string values as an input to the python script.  The python script will  keep  prompting the user to enter  integer values until the list total length is 5.

Comment it down below if you have any queries regarding the above python script


Post a Comment

0 Comments