Python Forum
Practice Question Excersizes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Practice Question Excersizes
#1
nomstudents = 0
totalgrade = 0
avegrade = 0
morestudents = input("Do you have any students? (yes/no) ").lower()
while morestudents == 'yes':
    grade = float(input("Enter your students grade: "))
while grade <0 or grade >100:
    grade = float(input(("Enter your student's grade: ")))
morestudents = input("Do you have any students? (yes/no)").lower()
no_students += 1

avegrade = totalgrade/numstudents
print(avegrade)
Question: Let's write a program that computes the (a) average grade and (b) number of averaged grades for a class.
***valid grades range from 0 to 100
***the number of students is not known in advance
Reply
#2
What is your question? You may want to start by checking the spelling of one of your variables.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
As already pointed out, one variable name is misspelled, you would benefit from a syntax highlighting IDE.

Your " while morestudents == 'yes': " has no way of stopping once the program flow gets into it. Also when user inputs a new grade, it just overwrites the previous one, there's no storage of data.

I recommend you to tackle this task pen&paper first, define the goals and draw a diagram of how you want the program flow to be ;) Turning completed diagram into code piece by piece will be easier and less error prone.
Reply
#4
thank you i see that spelling error. we did this example in class and I am just trying to get my mind around how the teacher did it.... unfortunately i wrote it down wrong and am stuck as to how to get the program to get multiple inputs from the user for different class grades than calculate that. there is a example in python that is similar, but not the same way....

example in the python visual quick start guide that has a similar coding example with while loops with unknown amount of entries until user types in "DONE". if someone could explain or show me how the piece of code could do the same example in the book ("summing an unknown number of numbers" (pg 62 edition 3)). That would be much appreciated.
Reply
#5
I always find it helpful to add "print()" statements at various points to verify that the value of variables are what they should be.

You have the line:
morestudents = input("Do you have any students? (yes/no) ").lower()
What happens if you enter "no", you have nothing so far to handle that and will end up with an error.

If you enter "yes", you progress to your first "while" statement which says if morestudents == 'yes', ask for a score.
Since nothing ever changes the value of 'morestudents' you will be stuck in this loop forever and you will not progress to the rest of your script.

Here is one simple example of using a 'while' loop:
while True:
   number = int(input("enter a number between 1 and 10: "))    # Get a number
   if 0 <= number <= 10:
       print("number = ", number)
       # Go back and get another number
   else:
       print("out of range")
       break    # Break out of 'while' loop and continue with next command

print("Done")
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
(Oct-04-2016, 03:19 PM)sparkz_alot Wrote: I always find it helpful to add "print()" statements at various points to verify that the value of variables are what they should be.

You have the line:
morestudents = input("Do you have any students? (yes/no) ").lower()
What happens if you enter "no", you have nothing so far to handle that and will end up with an error.

If you enter "yes", you progress to your first "while" statement which says if morestudents == 'yes', ask for a score.
Since nothing ever changes the value of 'morestudents' you will be stuck in this loop forever and you will not progress to the rest of your script.

Here is one simple example of using a 'while' loop:
while True:
   number = int(input("enter a number between 1 and 10: "))    # Get a number
   if 0 <= number <= 10:
       print("number = ", number)
       # Go back and get another number
   else:
       print("out of range")
       break    # Break out of 'while' loop and continue with next command

print("Done")

Thanks for the break down! Awesome advice =)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  frustrating practice taubee 9 4,210 Nov-22-2020, 04:23 AM
Last Post: taubee
  Sorting a DataFrame - Best Practice? lummers 1 1,769 Feb-10-2020, 04:41 AM
Last Post: satyashetty
  Pre-release practice help! Wilson1218 3 3,349 Nov-05-2017, 04:21 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020