Why the program does not want to calculate the average value from the dictionary?
grades = {}
def addGrade(grades):
course = input("input course: ")
grade = int(input("input grade: "))
grades[course] = grade
def printGrades(grades):
print("all grades:")
for c, grade in grades.items():
print(c + "\t" + str(grade))
def count(grades):
n = len(grades.keys())
print(n)
def printAverage(grades):
print("average grade:" + sum(grades)/int(count))
You have to add:
count = len(grades.keys())
Because it is not defined in your function.
The correct version of your function is:
def printAverage(grades):
count = len(grades.keys())
print("average grade:" , sum(grades.values())/count)
You have to use
sum(grades.values())
Otherwise it will be adding up all the keys instead of the values (grades).
The following should work:
grades = {}
def addGrade():
course = input("input course: ")
grade = int(input("input grade: "))
grades[course] = grade
def printGrades():
print("all grades:")
for c, grade in grades.items():
print(c + "\t" + str(grade))
def count():
n = len(grades.keys())
return n
def printAverage():
print(f"average grade: {sum(grades.values()) / len(grades):.2f}")
if __name__ == '__main__':
number_of_courses = int(input('Enter number of courses: '))
for _ in range(number_of_courses):
addGrade()
printAverage()
sample output:
Related
I am writing a program that asks the user to input a list of students. Once the list is made the user can input a student's name that was listed before to show 4 random integers from 1 to 100 that represent 4 different marks on assignments. When I run this program, however, the mark is the same number each time. How do I run the program so that each number for the mark is different, also how can I store these values in a separate list? Thank You!
def printMark(studentMark, studentName):
print("Enter the names of your students")
print("Press the enter key to store another name. print q or Q when you're done.")
names = input("Enter student name: \n")
classlist = []
while True:
names = input("")
if str.lower(names) == 'q':
break
classlist.append(names)
for name in classlist:
print(name)
student_choice = input("Enter a students in your classlist: ")
if student_choice not in classlist:
print("That students is not in your class")
elif student_choice in classlist:
mark = list(range(1, 101))
print("A1:" + str(studentMark) + "\nA2:" + str(studentMark) + "\nA3:" + str(studentMark) + "\nA4:" + str(studentMark))
classlist = []
import random
mark = list(range(1, 101))
printMark(random.sample(mark, k=4), classlist)
It seems like the error is in print("A1:" + str(studentMark) + "\nA2:" + str(studentMark) + "\nA3:" + str(studentMark) + "\nA4:" + str(studentMark)).
If studentMark is a list, which is what I gather from my last moment research on random.sample, it seems like the problem is that you are using the same value each time rather than different list elements from studentMark. (Note that I'm assuming the random.sample technique would work like mark = [random.randint(1, 101) for _ in range(4)], but without repeated values.)
Instead maybe use:
print("A1:" + str(studentMark[0]) + "\nA2:" + str(studentMark[1]) + "\nA3:" + str(studentMark[2]) + "\nA4:" + str(studentMark[3]))
You could also use:
print(*[f"A{i+1}: {mark}" for i, mark in enumerate(studentMark)])
This would simplify things but is more complicated than the original idea.
Your printMark function shouldn't take a studentMark argument if the intent is for it to generate a different set of marks each time. It also shouldn't take a studentName argument since it doesn't use it. You could very easily write this script without any functions at all, and it'd be more straightfoward:
import random
print("Enter the names of your students")
print("Press the enter key to store another name. print q or Q when you're done.")
classlist = []
while True:
names = input()
if str.lower(names) == 'q':
break
classlist.append(names)
print(*classlist, sep='\n')
student_choice = input("Enter a students in your classlist: ")
if student_choice not in classlist:
print("That student is not in your class")
else:
for a in range(1, 5):
print(f"A{a}:", random.sample(range(1, 101), k=4))
If you were going to use functions in this script, I'd suggest having one function to enter the class list and another to print a chosen set of marks given a class list:
import random
def get_class_list() -> list[str]:
print("Enter the names of your students")
print(
"Press the enter key to store another name. "
"Enter q or Q when you're done."
)
classlist = []
while True:
name = input()
if str.lower(name) == 'q':
return classlist
classlist.append(name)
def print_mark(classlist: list[str]) -> None:
student_choice = input("Enter a student in your classlist: ")
if student_choice not in classlist:
print("That student is not in your class")
return
for i in range(1, 5):
print(f"A{i}:", random.sample(range(1, 101), k=4))
print_mark(get_class_list())
Hi firstly 2 questions 1: why you put this program inside a def it just makes it more complicated 2: if the students mark are random why you are taking them as a argument to the def? any ways here is the solution i came up with
def printMark(studentName):
print("Enter the names of your students")
print("Press the enter key to store another name. print q or Q when you're done.")
names = input("Enter student name: \n")
classlist = []
while True:
names = input("")
if str.lower(names) == 'q':
break
classlist.append(names)
for name in classlist:
print(name)
student_choice = input("Enter a students in your classlist: ")
if student_choice not in classlist:
print("That students is not in your class")
elif student_choice in classlist:
mark = list(range(1, 101))
print("A1:" + str(random.sample(mark, k=4)) + "\nA2:" + str(random.sample(mark, k=4)) + "\nA3:" + str(random.sample(mark, k=4)) + "\nA4:" + str(random.sample(mark, k=4)))
classlist = []
import random
mark = list(range(1, 101))
printMark(classlist)
Here: instead of taking student marks a argument i generated them when they are printed this way it generates every time it prints one previously it was taking one random mark and using it 4 times now its all different.
Jhon, looks like your code was really close. I've made one edit:
def printMark(studentMark, studentName):
print("Enter the names of your students")
print("Press the enter key to store another name. print q or Q when you're done.")
names = input("Enter student name: \n")
classlist = []
while True:
names = input("")
if str.lower(names) == 'q':
break
classlist.append(names)
for name in classlist:
print(name)
student_choice = input("Enter a students in your classlist: ")
if student_choice not in classlist:
print("That students is not in your class")
elif student_choice in classlist:
mark = list(range(1, 101))
print("A1:" + str(studentMark[0]) + "\nA2:" + str(studentMark[1]) + "\nA3:" + str(studentMark[2]) + "\nA4:" + str(studentMark[3]))
classlist = []
import random
mark = list(range(1, 101))
printMark(random.sample(mark, k=4), classlist)
Here's a sample run:
Enter the names of your students
Press the enter key to store another name. print q or Q when you're done.
Enter student name:
a
b
c
q
b
c
Enter a students in your classlist: b
A1:7
A2:93
A3:63
A4:86
You should probably store the students in a dictionary when you want to bind their name to a list of their marks.
Here is the solution I came up with:
import random
students = {}
while True:
names = input("")
if names.lower() == 'q':
break
# this is list comprehension, you can write 4 times random.randint(1, 100) as well
students[names] = [random.randint(1, 100) for _ in range(4)]
student_choice = input("Enter a student from your list: ")
if student_choice not in students.keys():
print("This student is not in your list.")
else:
print(f"{student_choice}:\n"
f"A1: {students[student_choice][0]}\n"
f"A2: {students[student_choice][1]}\n"
f"A3: {students[student_choice][2]}\n"
f"A4: {students[student_choice][3]}")
It generates a list of 4 integers with random values every time a new user is added to the dictionary.
This means that you can access the student's marks later in the code - unlike your solution, where you only decided what the student's mark was while printing it.
I have some code that takes overlapping elements from two lists and creates a new one with these elements. there are two functions which check for a valid input, however, the return statement returns no value to the main function. Can someone help me?
This is the code:
import random
import sys
def try_again():
d = input("\nTry again? (y/n): ")
if d == 'y':
main_func()
elif d != 'y' and d != 'n':
print("Invalid entry")
try_again()
elif d == 'n':
sys.exit()
def first_list_test(length_a, range_a):
length_a = int(input("Enter the length of the first random list: "))
range_a = int(input("Enter the range of the first random list: "))
if length_a > range_a:
print("The length of the list must be greater than the range for unique elements to generate")
first_list_test(length_a, range_a)
else:
print("Length: " + str(length_a))
print("Range: " + str(range_a))
return length_a, range_a
def second_list_test(length_b, range_b):
length_b = int(input("Enter the length of the second random list: "))
range_b = int(input("Enter the range of the second random list: "))
if length_b > range_b:
print("The length of the list must be greater than the range for unique elements to generate")
second_list_test(length_b, range_b)
else:
print("Length: " + str(length_b))
print("Range: " + str(range_b))
return length_b, range_b
def main_func():
length_a = int()
range_a = int()
first_list_test(length_a, range_a)
print(length_a)
print(range_a)
print("\n")
length_b = int()
range_b = int()
second_list_test(length_b, range_b)
print(length_b)
print(range_b)
print("\n")
a = random.sample(range(1, range_a), length_a)
a.sort()
b = random.sample(range(1, range_b), length_b)
b.sort()
num = int()
print(a, "\n")
print(b, "\n")
c = []
d = []
for num in a:
if num in b:
c.append(num)
for test_num in c:
if test_num not in d:
d.append(test_num)
d.sort()
if len(d) == 0:
print("No matches found")
else:
print("Overlapping elements: ")
print(d)
try_again()
main_func()
Below I have created a Maths quiz in python. It consists of random equations to solve. The questions are generated using random numbers and functions that determine whether the question is add or subtract. I have completed the quiz, but I have tried adding a score counter so that if the user gets a question right, a point is added on. I am unsure how to do this, as I have tried implementing the score into the functions and it does not work. Here is the code:
import random
#Asks for name
name = input("What's your name?")
#Stops user from entering invalid input when entering their class
classchoices = ["A","B","C"]
classname = input("What class are you in?")
while classname not in classchoices:
classname = input("Not a valid class, try again:")
print(name, ",", classname)
print("Begin quiz!")
questions = 0
a = random.randint(1,12)
b = random.randint(1,12)
def add(a,b):
addQ = int(input(str(a) + "+" + str(b) + "="))
result = int(int(a) + int(b))
if addQ != result:
print ("Incorrect! The answer is", result)
else:
print("Correct")
def multiply(a,b):
score = 0
multQ = int(input(str(a) + "X" + str(b) + "="))
results = int(int(a) * int(b))
if multQ != results:
print ("Incorrect! The answer is", results)
else:
print("Correct")
def subtract(a,b):
subQ = int(input(str(a) + "-" + str(b) + "="))
resultss = int(int(a) - int(b))
if subQ != resultss:
print ("Incorrect! The answer is", resultss)
else:
print("Correct")
for questions in range(10):
Qlist = [add, subtract, multiply]
random.choice(Qlist)(random.randint(1,12), random.randint(1,12))
questions += 1
if questions == 10:
print ("End of quiz")
You can return a score for each task. An example for the add function:
if addQ != result:
print ("Incorrect! The answer is", result)
return 0
else:
print("Correct")
return 1
Of course you can give higher scores for more difficult tasks (e.g. 2 points for multiplications).
and then below:
score = 0
for questions in range(10):
Qlist = [add, subtract, multiply]
score += random.choice(Qlist)(random.randint(1,12),random.randint(1,12))
questions += 1
Use a global var and add +1 on every correct solution. The only negative is as soon as the programm is closed the score is lost.
something like this
You missed to define score globally and then access it within the function and updating it, Here's a working version:
import random
#Asks for name
name = input("What's your name?")
#Stops user from entering invalid input when entering their class
classchoices = ["A","B","C"]
classname = input("What class are you in?")
while classname not in classchoices:
classname = input("Not a valid class, try again:")
print(name, ",", classname)
print("Begin quiz!")
score = 0
questions = 0
a = random.randint(1,12)
b = random.randint(1,12)
def add(a,b):
global score
addQ = int(input(str(a) + "+" + str(b) + "="))
result = int(int(a) + int(b))
if addQ != result:
print ("Incorrect! The answer is", result)
else:
score += 1
print("Correct")
def multiply(a,b):
global score
multQ = int(input(str(a) + "X" + str(b) + "="))
results = int(int(a) * int(b))
if multQ != results:
print ("Incorrect! The answer is", results)
else:
score += 1
print("Correct")
def subtract(a,b):
global score
subQ = int(input(str(a) + "-" + str(b) + "="))
resultss = int(int(a) - int(b))
if subQ != resultss:
print ("Incorrect! The answer is", resultss)
else:
score += 1
print("Correct")
for questions in range(10):
Qlist = [add, subtract, multiply]
random.choice(Qlist)(random.randint(1,12), random.randint(1,12))
questions += 1
if questions == 10:
print ("End of quiz")
print('Score:', score)
Using return values of your functions as demonstrated by cello would be my first intuition.
But since you are already using a global variable to track your questions, you can introduce another one for the score.
questions = 0
score = 0
a = random.randint(1,12)
b = random.randint(1,12)
To use it (and not a local variable score, which would be only available in the function), your functions have to reference it by using globals:
def add(a,b):
global score
You can then increment it in the else-statements of your functions:
else:
print("Correct")
score += 1
At the end, you can print the score:
print("End of quiz")
print("Your score:", score)
Notice, that you are using questions variable in a strange fashion:
You first initialize it with 0:
questions = 0
Then you overwrite it during a loop:
for questions in range(10):
questions += 1
The for will give questions the values 0..9, so it is totally unneccessary to increase the number manually inside the loop. This increased value will immediately be overwritten by the next value of the loop.
After your loop exits, you are sure to have asked 10 questions. The following if-condition is superfluous (and only works, because you additionally increase questions in the loop). Just lose it:
print("End of quiz")
python 3.3.3
I am trying to write a program for class and I am lost. Here is what I need to do.
I need to calculate an average per student based on grades entered.
I need to calculate a class average.
if a student enters a grade of -1 input of grades stop.
need to print a message with each students grade.
the students grade should show a numeric grade and a letter grade.
the message will be based off of the students letter grade.
how do i collect and store students name and test grades.
so that i can output it all at once to where it will show the students name.
thier numeric average, a letter grade based off that average,
and a statement based off the letter grade they recieved?
heres the code i have so far:
def main():
another_student = 'y'
while another_student == 'y' or another_student == 'Y':
student_average()
print()
another_student = input('do you have another student to enter (y/n) ? ')
while another_student == 'n' or another_student == 'N':
student_average_list()
class_average()
break
def student_average():
total = 0.0
print()
student_name = input('what is the students name? ')
print()
print()
print(student_name)
print('-------------------')
number_of_tests = int(input('please enter the number of tests : '))
for test_num in range(number_of_tests):
print('test number', test_num + 1, end='')
score = float(input(': '))
total += score
student_average = total / number_of_tests
print ()
print(student_name,"'s average is : ",student_average, sep='')
def student_average_list():
print ('kahdjskh')
def class_average():
print ('alsjd')
main()
I think this is close to what you're basically looking for. It defines aStudentclass to make data storage and processing a little easier to manage.
class Student(object):
def __init__(self, name):
self.name, self.grades = name, []
def append_grade(self, grade):
self.grades.append(grade)
def average(self):
return sum(self.grades) / len(self.grades)
def letter_grade(self):
average = self.average()
for value, grade in (90, "A"), (80, "B"), (70, "C"), (60, "D"):
if average >= value:
return grade
else:
return "F"
def main():
print()
print('Collecting class student information')
a_class = [] # "class" by itself is a reserved word in Python, avoid using
while True:
print()
print('{} students in class so far'.format(len(a_class)))
another_student = input('Do you have another student to enter (y/n) ? ')
if another_student[0].lower() != 'y':
break
print()
student_name = input('What is the student\'s name? ')
a_class.append(Student(student_name))
print()
print('student :', student_name)
print('-------------------')
number_of_tests = int(input('Please enter the number of tests : '))
for test_num in range(1, number_of_tests+1):
print('test number {}'.format(test_num), end='')
score = float(input(' : '))
if score < 0: # stop early?
break
a_class[-1].append_grade(score) # append to last student added
print_report(a_class)
def print_report(a_class):
print()
print('Class Report')
print()
for student in sorted(a_class, key=lambda s: s.name):
print('student: {:20s} average test score: {:3.2f} grade: {}'.format(
student.name, student.average(), student.letter_grade()))
print()
print('The class average is {:.2f}'.format(class_average(a_class)))
def class_average(a_class):
return sum(student.average() for student in a_class) / len(a_class)
main()
You need to keep a list of marks for the whole class. student_average function is doing too many things. Maybe make a function get_student_marks that just returns the list of a student's marks. You'd need an average function to compute the average of a list, which you could use for both student average and class average. Good luck!
Here is a small chunk of a code I have:
studentName = ""
def getExamPoints (total):
for i in range (4):
total = 0.0
examPoints = input ("Enter exam score for " + studentName + str(i+1) + ": ")
total = ?????
total = total/.50
total = total * 100
where the ????? is I can't figure out how to get the string to add the four scores
student name I inputed later and it is required that later in the program I use examPoints = getExamPoints(studentName).
Without any error checking for bad input:
total = 0.0
for i in range (4):
total += int(input ("Enter exam score for " + studentName + str(i+1) + ": "))
total = total/.50
total = total * 100
Did you try
total += int(examPoints);