Print statement defaults to first 'if' statement no matter the input given - python

I'm extremely new to python, so this code is probably a complete mess, but the issue I'm having is that the code is executing the first 'if' statement no matter what average is calculated (ie. the average calculated is a 76.0, but it's still executing an 'A' grade, rather than a 'C' grade). What am I doing wrong?
Also, just for information's sake, the code is supposed to have the user input four test scores and the code has to randomize a score for the fifth test in range of 70-85, calculate and return the average of all five test scores and assign a grade based off of the average.
def rand():
import random
num5 = random.randint(70,85)
calcAverage(num5)
def calcAverage(num5):
num1 = float(input("Enter the first test score: "))
num2 = float(input("Enter the second test score: "))
num3 = float(input("Enter the third test score: "))
num4 = float(input("Enter the fourth test score: "))
print("\nThe random test score generater on your behalf was", num5)
total = num1 + num2 + num3 +num4 + num5
average = total // 5
letterGrade(average)
def letterGrade(average):
if average >= 90.00 and average <= 100.00:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is A.")
elif average >= 80.00 and average <= 89.99:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is B.")
elif average >= 70.00 and average <= 79.99:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is C.")
elif average >= 60.0 and average <= 69.99:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is D.")
else:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is F.")
rand()

Related

I have just started coding and am stuck solving this python problem

Write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1.
This is what I have come up with so far
num = int(input("How many number's are there?"))
total_sum = 0
avg = total_sum / num
for n in range (num):
number = float(input("Enter a number"))
while number <0:
print = ("Average is", avg)
number >0
print(number)
total_sum += number
avg = total_sum / num
print = ("Average is", avg)
I need it to stop at -1 one and still give an average of the numbers listed.
Easy.
emp = []
while True:
ques = int(input("Enter a number: "))
if ques != -1:
emp.append(ques)
continue # continue makes the code go back to beginning of the while loop
else:
if len(emp) != 0:
avg = sum(emp) / len(emp) # average is the sum of all elements in the list divided by the number of elements in said list
print(f"Average of all numbers entered is {avg}")
break
else:
print("Give me at least one number that's not -1")
continue # emp is empty we need at least one number to calculate the average so we go back to the beginning of the loop

Python Grading Program

So I need to make a program that grades 10 students individually, then displays the average grade for all 10 students.
I think this is how the grading should look, but im not sure how to set up a count on the number of times its graded, or how to set up the average function. Help would be most welcome. I am a horrible coder.
score = int(input("Enter a score between 0 and 100: "))
if score >=89:
print("The grade is an A")
elif score>=79:
print("The grade is a B")
elif score>=69:
print("The grade is a C")
elif score>=59:
print("The grade is a D")
else:
print("The grade is a F")
You can try using a counter variable that goes from 0-9 and use a while loop to check and increment that counter value and after each loop calculates the average and keeps doing this till the last value has been entered
Counter = 0
average = 0
while (Counter <= 10):
score = int(input("Enter a score between 0 and 100: "))
if score >= 89:
print("The grade is an A")
elif score >= 79:
print("The grade is a B")
elif score >= 69:
print("The grade is a C")
elif score >= 59:
print("The grade is a D")
else:
print("The grade is a F")
Counter = Counter + 1
average = (score + average)
average = average/Counter
print("Average of all 10 students:",average)
So right now the code is configured specifically for just 10 loops but you can give the user the power to determine this by suggesting a value to end the loop and the while loop will check for this value and when the loop sees the value it exits the loop and gives a value
Also with each loop there is a counter value that keeps increasing and an average value that holds the sum of the values entered and when the loop exits and average is calculated by dividing the sum by the counters and this gives the results
This should do the trick.
total = 0
for i in range(10):
score = int(input("Enter a score between 0 and 100: "))
if score >= 89:
print("The grade is an A")
elif score >= 79:
print("The grade is a B")
elif score >= 69:
print("The grade is a C")
elif score >= 59:
print("The grade is a D")
else:
print("The grade is a F")
total += score
print("Average of all 10 students:", total/10)
In the for loop you get the score of each student and then add it to the total of all students. When the loop ends you divide it with the total number of students so 10.
total score / total number of students total / 10
You can define a function that finds the average score. Store all the scores in a list and then pass the list as an argument in the function
def avg(scores): #assuming scores is the name of the list
avg = sum(scores)/len(scores)
return "Average score is {}".format(avg)
Or
return f'Average score is {avg}' #for python3
Not sure I understand what you mean by number of times it is graded but I think you can include a while loop and a variable that increases by 1 every time it grades.

Dropping the lowest test score

I'm writing a python program that drops the lowest of four test scores. The program first prompts the user to enter their first four test scores, then the program should drop the lowest test score and take the average of the remaining three test scores. The program should then print the final letter grade. This is what I have so far and I keep receiving an error when dropping the lowest score.
#Enter four test scores as percentages (%)
test1 = int(input("Enter grade 1: 90"))
test2 = int(input("Enter grade 2: 80"))
test3 = int(input("Enter grade 2: 70)")
test4 = int(input("Enter grade 2: 80)")
#Drop lowest test score
print("The average, with the lowest score dropped" )
total =(test1 + test2 + test3)
#Calculate average
def calc_average(total):
return total /3
#Grade scale
def determine_score(grade):
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >=70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
#Calculate final letter grade
print("The final grade is")
main()
I tried to write a program with the things that I understood from you.
Here is the explanation:
Taking four grades from user.
Dropping the lowest one.
Taking average.
Giving a letter grade according to the average.
Here is the code:
test1 = int(input("Enter grade 1: "))
test2 = int(input("Enter grade 2: "))
test3 = int(input("Enter grade 3: "))
test4 = int(input("Enter grade 4: "))
x = min(test1,test2,test3,test4)
total = float(test1 + test2 + test3 + test4 - x)
avg = total / 3
print("Your average is " + str(avg))
def determine_letter(grade):
letter =""
if grade >= 90:
letter = "A"
elif grade >= 80:
letter = "B"
elif grade >= 70:
letter = "C"
elif grade >= 60:
letter = "D"
else:
letter = "F"
print("The final letter grade is " + letter)
determine_letter(avg)

Grade report sentinal loop test in python

I could use some help solving an error. This is my code that takes in multiple inputs, and outputs ( the amount of inputs, the amount of students who miss the test, the students who got a grade higher then 80, the average of those who took the test, and the average of the entire class including those who missed.
#number of students who miss, and get a highscore
num_miss = 0
num_high = 0
total = 0
count = 0
#The minimum and maximum values
min_score = 100.00
max_score = 0.0
score = int(input("Enter a Score(-1 to quit): "))
#The loop to test score inputs, and the students who missed or got a high score
while score >-1:
if score >= 80:
num_high += 1
elif score == 0:
num_miss += 1
if score < min_score:
min_score = score
if score > max_score:
max_score = score
score = int(input("Enter a Score(-1 to quit): "))
# an incriment to count the total for average, and the amount of students that took the exam
total += score
count += 1
students = count
#formulas for creating the averages
average = total / count
average2 = total / (count + -num_miss)
#prints grade report with a dashed line under it
print("Grade Report")
print("-" * 38)
if count >= 0:
print("Total number of students in the class: {}".format(students))
print("Number of students who missed the exam: {}".format(num_miss))
print("Number of students who took the exam: {}".format(count - num_miss))
print("Number of students who scored high: {}".format(num_high))
print("Average of all students in the class: {0:.2f}".format(average))
print("Average of students taking the exam: {0:.2f}".format(average2))
This is the problem: by inputing all these values: 65, 98, 45, 76, 87, 94, 43, 0, 89, 80, 79, 0, 100, 55, 75, 0, 77, -1 to end
This is what my professor gets for an output
Grade Report
1. Total number of students in the class: 17
2.Number of students who missed the exam: 3
3.Number of students who took the exam: 14
4.Numberof students who scored high: 6
5.Average of all students in the class:62.5
6.Average of students taking the exam: 75.9
This is what i get for an output
Grade Report
1.Total number of students in the class: 17
2.Number of students who missed the exam: 3
3.Number of students who took the exam: 14
4.Number of students who scored high: 6
5.Average of all students in the class: 58.64
6.Average of all students taking the exam: 71.21
How do I fix my averages so that they are the same as my professors?
You're including the last "-1" as part of your data set.
Also, Why are you doing this:
average2=total/(count+-numMiss)
as opposed to this:
average2=total/(count-numMiss)
The first problem is that you include the -1. You should do
while True:
...
score=int(input("Enter a Score(-1 to quit): "))
if score == -1:
break
...
You only need this line once:
score = int(input("Enter a Score(-1 to quit): "))
And it should be inside the loop.
The code should look something like this:
while True:
score = int(input("Enter a Score(-1 to quit): "))
if score <= -1:
break
Also, this line can move outside the loop:
students = count

How do I get the sum of all numbers in a range function in Python?

I can't figure out how to take x (from code below) and add it to itself to get the sum and then divide it by the number of ratings. The example given in class was 4 ratings, with the numbers being 3,4,1,and 2. The average rating should be 2.5, but I can't seem to get it right!
number_of_ratings = eval(input("Enter the number of difficulty ratings as a positive integer: ")) # Get number of difficulty ratings
for i in range(number_of_ratings): # For each diffuculty rating
x = eval(input("Enter the difficulty rating as a positive integer: ")) # Get next difficulty rating
average = x/number_of_ratings
print("The average diffuculty rating is: ", average)
Your code doesn't add anything, it just overwrites x in each iteration. Adding something to a variable can be done with the += operator. Also, don't use eval:
number_of_ratings = int(input("Enter the number of difficulty ratings as a positive integer: "))
x = 0
for i in range(number_of_ratings):
x += int(input("Enter the difficulty rating as a positive integer: "))
average = x/number_of_ratings
print("The average diffuculty rating is: ", average)
try:
inp = raw_input
except NameError:
inp = input
_sum = 0.0
_num = 0
while True:
val = float(inp("Enter difficulty rating (-1 to exit): "))
if val==-1.0:
break
else:
_sum += val
_num += 1
if _num:
print "The average is {0:0.3f}".format(_sum/_num)
else:
print "No values, no average possible!"

Categories