Python Forum
Collecting Average User Statistics? 1st semester programmer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Collecting Average User Statistics? 1st semester programmer
#1
Hey guys! I'm currently taking my first programming class (hopefully my last lol) and have little to no prior experience with programming. I'm at a standstill in my first "major" project, but have 90% of the code written. I appreciate everybody who takes their time to read this.

I think it's relevant to show you guys my teachers specifications for this project:

Your fellow students need help. This is their first year in college and they need to determine how many hours they need to study to get good grades.

Study Hours Per Week Per Class Grade
15 = A
12 = B
9 = C
6 = D
0 = F

1. The user enters their full name and the number of credits they are taking.
2. The user will then enter the grade they want assuming the same grade for all classes.
3. The program displays for each student: student’s name, number of credits, total number of weekly study hours, and grade they should expect to receive. In the following format –
Name: FirstName LastName
Credits: 12
Study Hours: 60
Grade: A
## I'm stuck on number 4
4. At the end of the program, the program displays the total number of students who used the program, the average credits taken, and the average study hours. In the following format –
Total Students: 3
Average Credits: 9
Average Study Hours: 20

So leading up to this project I did miss a couple classes when we talked about for loops, I assume I must use an accumulator to get the total number of students. Getting the average credits and average study hours, I assume I would use a for loop. I'm not having a syntax issue I'm having a logical issue right now because this project is jumbled I cant see a clear solution. My code did run perfectly until I decided to code in step 4, then I realized I don't know how to.

Here's what I have coded so far:
# This program will help my fellow students to determine how
# many hours they need to study to get good grades.
# Study and Grade Calculator Version 1.00 updated Feb 12, 2020

# Initialize variables
runAgain = 'Y'
totalStudents = 0

averageCredits = 0
averageStudy = 0


##### Welcome user to the Study and Grade Calculator program.
print("Welcome to the Study and Grade Calculator\n" +
      "By Andrew Rodriguez \n" +
      "Version 1.00 updated Feb 12, 2020\n\n")

# Loop continues as long as user enter Y or y
while runAgain == 'Y' or runAgain == 'y' or runAgain == 'yes' or runAgain == 'Yes': 
    

##### Collect user data: Name, Credits, and Grade desired.
# Ask user's name and validate data
    userName = input('Please enter your full name (First, Last): \n')
    while userName == "" or userName.isdigit():
        print('Invalid name')
        userName = input('Please enter your full name (First, Last): \n')
    totalStudents = 1 + totalStudents

# Ask user for number of credits they are taking and validate data
    creditAmount = input("Please enter how many credits you're taking: \n")  
    while not creditAmount.isdigit()or int(creditAmount) < 0 or int(creditAmount) >= 20:
        print('Invalid amount of credits')
        creditAmount = input("Please enter how many credits you're taking: \n")
# Accumulator
    userCredits = creditAmount + userCredits
    
# Ask user to enter the grade they want and validate data
    desiredGrade = input('Please enter your desired letter grade: \n')
    while desiredGrade == "" or desiredGrade.isdigit():
        print('Invalid grade')
        desiredGrade = input('Please enter a valid desired letter grade: \n')


# Desired grade decision structure
    if desiredGrade == "A" or desiredGrade == "a":
        studyHours = 15
    elif desiredGrade == "B" or desiredGrade == "b":
        studyHours = 12
    elif desiredGrade == "C" or desiredGrade == "c":
        studyHours = 9
    elif desiredGrade == "D" or desiredGrade == "d":
        studyHours = 6
    elif desiredGrade == "F" or desiredGrade == "f":
        studyHours = 0
    else:
        print('Invalid Grade')
        studyHours = 'Invalid Grade'
        
#Accumulator
    averageStudy = averageStudy + studyHours
    
    
##### Display results
    print("\nName: ", userName, "\nCredits: ", creditAmount, "\nStudy Hours: ", studyHours,
          "\nGrade: ", desiredGrade)


    for averageCredits in range(userCredits):
        
    for averageStudy in range(userStudy):
        
    
# Ask the user if they want to run this program again
    runAgain = input('Would you like to run this program again? (Y/N)')
          
# Thank the user for using the application
print('Thank you for caring about your grades!')
Of course I've tried to refer to my textbook for help and looked up examples that I could maybe adapt to my situation, but I just can't think clearly about this anymore, I've been working on this for over a week, if anybody could help point me in the right direction or explain what I have to do to collect the total number of users (I think I coded that correctly), and calculate the average credit hours and average study hours.

-Much appreciated,
Andrew
Reply
#2
(Mar-05-2020, 01:54 AM)Jagsrs28 Wrote: Getting the average credits and average study hours, I assume I would use a for loop.
I didn't understand: why do you need to use a for-loop. You have the number of students and total value of credits. Mean value is a fraction of the total amount of credits and the number of students, isn't it?

I would also suggest some refactorings, e.g.
# define a dict

grade_mapper = {'a': 15,
                'b': 12,
                'c': 9,
                etc... }
# you can rewrite if/elif-series as follows:

study_hours = grade_mapper.get(desiredGrade.lower(), None)  # I leave camel-case just for clarity
if study_hours is None:
    print("Invalid grade")
Also, you can convert variables to lowercase in if-conditions, e.g.
Instead of coding

if some_var == 'a' or some_var == 'A':
    ...
you can do:

if some_var.lower() == 'a':
   ...
Reply
#3
(Mar-05-2020, 11:14 AM)scidam Wrote:
(Mar-05-2020, 01:54 AM)Jagsrs28 Wrote: Getting the average credits and average study hours, I assume I would use a for loop.
I didn't understand: why do you need to use a for-loop. You have the number of students and total value of credits. Mean value is a fraction of the total amount of credits and the number of students, isn't it?

This was so painfully obvious! I'm so happy you replied because I got my code working 100%! Thank you thank you thank you thank you. Also great advice/tips for better ways to code. +1 Rep from me
Reply
#4
For documentation and maybe to help other people, here's my functioning code.

# This program will help my fellow students to determine how
# many hours they need to study to get good grades.
# Study and Grade Calculator Version 1.00 updated Feb 12, 2020

# Initialize variables
runAgain = 'Y'
totalStudents = 0

averageCredits = 0
averageStudy = 0 
totalCredits = 0
totalStudy = 0


##### Welcome user to the Study and Grade Calculator program.
print("Welcome to the Study and Grade Calculator\n" +
      "By Andrew Rodriguez \n" +
      "Version 1.00 updated Feb 12, 2020\n\n")

# Loop continues as long as user enter Y or y
while runAgain == 'Y' or runAgain == 'y' or runAgain == 'yes' or runAgain == 'Yes': 
    


##### Collect user data: Name, Credits, and Grade desired.
# Ask user's name and validate data
    userName = input('Please enter your full name (First, Last): \n')
    while userName == "" or userName.isdigit():
        print('Invalid name')
        userName = input('Please enter your full name (First, Last): \n')

    totalStudents = 1 + totalStudents

# creditAmount.isdigit()
# Ask user for number of credits they are taking and validate data
    creditAmount = int(input("Please enter how many credits you're taking: \n")) 
    while (creditAmount) < 0 or (creditAmount) >= 20:
        print('Invalid amount of credits')
        creditAmount = int(input('Please enter how many credits you are taking: \n'))
        
    totalCredits = creditAmount + totalCredits
    
# Ask user to enter the grade they want and validate data
    desiredGrade = input('Please enter your desired letter grade: \n')
    while desiredGrade == "" or desiredGrade.isdigit():
        print('Invalid grade')
        desiredGrade = input('Please enter a valid desired letter grade: \n')


# Desired grade decision structure
    if desiredGrade == "A" or desiredGrade == "a":
        studyHours = 15
    elif desiredGrade == "B" or desiredGrade == "b":
        studyHours = 12
    elif desiredGrade == "C" or desiredGrade == "c":
        studyHours = 9
    elif desiredGrade == "D" or desiredGrade == "d":
        studyHours = 6
    elif desiredGrade == "F" or desiredGrade == "f":
        studyHours = 0
    else:
        print('Invalid Grade')
        studyHours = 'Invalid Grade'
        
    totalStudy = totalStudy + studyHours

# Average user statistics calculator

    averageStudy = totalStudy / totalStudents
    averageCredits = totalCredits / totalStudents
    
    
##### Display results
    print("\nName: ", userName, "\nCredits: ", creditAmount, "\nStudy Hours: ", studyHours,
          "\nGrade: ", desiredGrade)

    print()
    print(-User Stats-)
    print("\nTotal Students: ", totalStudents, "\nAverage Study Hours: ",
          averageStudy, "\nAverage Credit Hours: ", averageCredits)
   
    
# Ask the user if they want to run this program again
    runAgain = input('Would you like to run this program again? (Y/N)')
          
# Thank the user for using the application
print('Thank you for caring about your grades!')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics EvanCahill 0 5,212 Jul-21-2018, 07:32 PM
Last Post: EvanCahill
  Statistics from text file Zatoichi 1 4,227 Feb-05-2018, 04:52 AM
Last Post: ka06059

Forum Jump:

User Panel Messages

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