My grade entering program is calculating the wrong average - python

Hiiii, thank you for all the answers! I've figured this out! I can't delete this message tho, and I'm not sure how to make it inactive. basically i am a mess. but thank you everyone!!!
Okay i am actually having 456344 problems, but this is my current one
here is my code:
def averages():
grade = 0
x = eval(input("How many grades will you be entering? "))
if type(x) != type(int(x)):
x = eval(input("You can't enter that many grades. How many grades will you be entering? "))
for i in range(x):
y = eval(input("Please enter a grade between 0 and 100: "))
if 0 <= y <= 100:
grade = grade + y
else:
print("Your number is out of range!")
y = eval(input("Please enter a grade between 0 and 100: "))
average = grade/x
print (y)
print (x)
print (grade)
print (average)
averages()
basically whenever i run the code this part doesn't work:
if 0 <= y <= 100:
grade = grade + y
and it only calculates the last number entered in the average.
Also, I'm supposed to make it give an error message if the number entered by the user is out of range (not between 0 and 100), but the error message isn't coming up? I'm not sure what's happening. Please help, thank you.

I have made some changes...
1) Replaced if-else statement with while
2) This condition checking makes sure to give out error message
def averages():
grade = 0
x = int(input("How many grades will you be entering? "))
if type(x) != type(int(x)):
x = int(input("You can't enter that many grades. How many grades will you be entering? "))
for i in range(x):
y = int(input("Please enter a grade between 0 and 100: "))
while not (0 <= y <= 100):
print("Your number is out of range!")
y = int(input("Please enter a grade between 0 and 100: "))
grade = grade + y
average = grade/x
print (y)
print (x)
print (grade)
print (average)
averages()

As grade addition is outside for it considers last input only.
There are some indentation issues and eval issue. Most of all, don't use input() on Python 2 or eval() on Python 3. If you want integer numbers, use int() instead. Try this code :
def averages():
grade = 0
while True:
try:
x = int(raw_input("How many grades will you be entering? "))
except:
print "Input limit exceeds"
continue
else:
break
for i in range(x):
y = int(raw_input("Please enter a grade between 0 and 100: "))
if 0 <= y and y <= 100:
grade = grade + y
else:
print("Your number is out of range!")
y = int(input("Please enter a grade between 0 and 100: "))
average = grade/x
print (y)
print (x)
print (grade)
print (average)
averages()

Related

How can I implement a input range condition of 0-100 in python?

So Below is my code, and what I am trying to do is keep the marks entered between 0 and 100 If they aren't to then give them an error or something. Thank you.
print("please enter your 5 marks below")
#read 5 inputs
mark1 = int(input("enter mark 1: "))
mark2 = int(input("enter mark 2: "))
mark3 = int(input("enter mark 3: "))
mark4 = int(input("enter mark 4: "))
mark5 = int(input("enter mark 5: "))
#create array/list with five marks
marksList = [mark1, mark2, mark3, mark4, mark5]
#print the array/list
print(marksList)
#calculate the sum and average
sumOfMarks = sum(MarksList)
averageOfMarks = sum(marksList)/5
#display results
print("The sum of your marks is: "+str(sumOfMarks))
print("The average of your marks is: "+str(averageOfMarks))
Use this way(while):
i=0
marksList = []
print("please enter your 5 marks below")
while i < 5:
mark = int(input(f"enter mark{i+1}: "))
if mark<=100 and mark>=0:
marksList.append(mark)
i+=1
else:
print("Please enter between 0 and 100")
#print the array/list
print(marksList)
#calculate the sum and average
sumOfMarks = sum(marksList)
averageOfMarks = sum(marksList)/5
#display results
print("The sum of your marks is: "+str(sumOfMarks))
print("The average of your marks is: "+str(averageOfMarks))

I get a syntaxt error when I try to run the program

I get a syntax error on the last else statement. This is supposed to be a grade calculator. If someone enters anything that isn't between 0 and 100, then the program should send a message, and then loop until a valid number is entered. Also I am new to programming, so if there is something else wrong with my code, please let me know!
number = int(input("Enter the numeric grade: "))
if number > 89:
letter = 'A'
elif number > 79:
letter = 'B'
elif number > 69:
letter = 'C'
else:
letter = 'F'
print("The letter grade is", letter)
number = int(input("Enter the numeric grade: "))
if number > 100:
print("Error: grade must be between 100 and O")
elif number < 0:
print("Error: grade must be between 100 and O")
else:
# The code to compute and print the result goes here
number = int(input("Enter the numeric grade: "))
if number > 100 or number < 0:
print("Error: grade must be between 100 and 0")
else:
# The code to compute and print the result goes here
number = int(input("Enter the numeric grade: "))
if number >= 0 and number <= 100:
else:
print("Error: grade must be between 100 and O")
Your issue is at the bottom, where you've got an empty if statement followed by an else statement, as well as incorrect indenting. From your code, I believe you are trying to use not.
I would suggest doing one of two things:
1.
if not (number >= 0 and number <= 100):
2.
if number < 0 or number > 100:
These two pieces of code will both produce the same result.
Also, it seems as though you are repeating the same code several times to try to ensure that the user inputs a number between 0 and 100. If so, this can be achieved with a while loop. I've put an example of this below:
number = int(input("Enter the numeric grade: "))
while number < 0 or number > 100:
print("Error: grade must be between 100 and O")
number = int(input("Enter the numeric grade: "))
In the last line.
if number >= 0 and number <= 100:
else:
print("Error: grade must be between 100 and O")
you didn't write anything after the if statement.
This code will run
if number >= 0 and number <= 100:
print('Write something here')
else:
print("Error: grade must be between 100 and O")
or you can just remove the last if statement its really of no use like try doing something else because there are a lot of if and else statements which don't look nice.
try this:
n = int(input())
first = 0
last = 100
while n < first or n > last:
print(n,'is not a valid input')
n = int(input())
From what I've understood, you're trying loop indefinitely until the user input is between 0 and 100.
My solution would be this:
Define a function that starts by requesting an input from the user.
Use a while loop that will check if the input is 'correct'. In case the input is not correct, the loop will print the error and call back the function again indefinitely till the user's input is between 0 and 100.
if the input is between that range, it will then evaluate the grade and return it.
def yfunc():
n = int(input('Enter the numeric grade: '))
while n < 0 or n > 100:
print("Error: grade must be between 100 and O")
return yfunc()
if n > 89:
letter = 'A'
elif n > 79:
letter = 'B'
elif n > 69:
letter = 'C'
else:
letter = 'F'
return letter
yfunc()
If you have any questions, feel free to ask.

Write an application that allows a user to enter any number of student test scores until the user enters 999

Write a python application that allows a user to enter any number of student test scores until the user enters 999. If the score entered is less than 0 or more than 100, display an appropriate message and do not use the score. After all the scores have been entered, display the number of scores entered and the arithmetic average.
I am having trouble breaking the loop when the score entered is less than 0 or more than 100.
The code I have is
total_sum = 0
count = 0
avg = 0
numbers = 0
while True:
num = input("Enter student test score: ")
if num == "999":
break
if num < "0":
break
print("Number is incorrect")
if num > "100":
break
print ("Number is incorrect")
total_sum += float(num)
count += 1
numbers = num
avg = total_sum / count
print("The average is:", avg)
print("There were " +str(count) + " numbers entered")
There are multiple issues with your code.
You want to compare numbers, not strings. Try using float(input(...))
break will break out of the loop in exactly the same way for numbers under 0, above 100, or for the "finished" token 999. That's not what you described that you want. Rather, for "incorrect numbers" just print the message, and don't add the numbers and counter...But no need to break the loop, amirite?
You've got an indentation problem for the < 100 case. Your print is missing an indentation.
No need to have avg = 0
numbers isn't used
Try something like -
total_sum = 0
count = 0
while True:
num = float(input("Enter student test score: "))
if num == 999:
break
elif num < 0 or num > 100:
print("Number is incorrect")
else:
total_sum += num
count += 1
avg = total_sum / count
print("The average is:", avg)
print("There were " +str(count) + " numbers entered")

How do I make a program ask the user for the number of inputs, and add the result of their inputs togther?

I'm currently trying to make a program that calculates your final semester grade based on the grades you received previously and how big of an impact they have on your semester grade. The problem is I'm struggling to figure out how you can make the program ask the user for the number of grades they received and then ask for the same amount of grades.
In a nutshell, the program is supposed to do the following:
Ask for the number of received grades
Ask what grade the user got and the percentage of that grade x amounts of times, where x is the number the user gave on the first question.
Calculate the final semester grade based on the inputs.
I hope someone can help, I've made this code so far in case it helps you to understand what I'm trying to do.
grade1 = int(input("What was the first grade you received? " ))
percentage1 = float(input("What's the percentage of the first grade? "))
grade_value1 = grade1 * percentage1
grade2 = int(input("\nWhat was the second grade you received? "))
percentage2 = float(input("What's the percentage of the second grade? "))
grade_value2 = grade2 * percentage2
grade3 = int(input("\nWhat was the third grade you received? " ))
percentage3 = float(input("What's the percentage of the third grade? "))
grade_value3 = grade3 * percentage3
finale_grade = grade_value1 + grade_value2 + grade_value3
if finale_grade < 2.9:
print("\nYour finale grade is 2")
elif finale_grade < 5.4:
print("\nYour finale grade is 4")
elif finale_grade < 8.4:
print("\nYour finale grade is 7")
elif finale_grade < 10.9:
print("\nYour finale grade is 10")
else:
print("\nYour finale grade is 12")
#I hope this works , I'm a beginner tho
num_of_recieved_grades = int(input("number of recieved
grades ?"))
final_grade = 0
# for loop in range of the number entered
for x in range (num_of_recieved_grades ):
grade = int(input("what is the grade"))
percentage = float(input("what is the percentage"))
final_grade += grade * percentage

Running into some logical errors with my code

I am the given the following problem and asked to write a solution algorithm for it using python.
problem:
Write a Python program to determine the student with the highest average. Each student takes a midterm and a final. The grades should be validated to be between 0 and 100 inclusive. Input each student’s name and grades and calculate the student’s average. Output the name of the student with the best average and their average.
Here is my code:
def midTerm():
midtermScore = int(input("What is the midterm Score: "))
while (midtermScore <= 0 or midtermScore >= 100):
midtermScore = int(input("Please enter a number between 0 and 100: "))
return midtermScore
def final():
finalScore = int(input("What is the final Score: "))
while (finalScore < 0 or finalScore > 100):
finalScore = int(input("Please enter a number between 0 and 100: "))
return finalScore
total = 0
highest = 0
numStudents = int (input("How Many Students are there? "))
while numStudents < 0 or numStudents > 100:
numStudents = int (input("Please enter a number between 0 and 100? "))
for i in range (1, numStudents+1):
students = (input("Enter Student's Name Please: "))
score = (midTerm()+ final())
total += score
avg = total/numStudents
if (highest < avg):
highest = avg
winner = students
print ("The Student with the higgest average is: ", winner, "With the highest average of: ", avg)
The issue I am running into is the last part. The program does not print the name of the person with the highest average, but the name of the person that was entered at the very last. I am very confused on how to go forward from here. Can you please help? Thanks in advance for any help.
You're not far off. Take a look here:
for i in range (1, numStudents+1):
students = (input("Enter Student's Name Please: "))
score = (midTerm()+ final())
total += score
avg = total/numStudents
if (highest < avg):
highest = avg
winner = students
Besides the indentation error (hopefully just clumsy copy-pasting) You're not actually calculating each student's average score anywhere. Try something like this:
for i in range (numStudents):
student_name = (input("Enter Student's Name Please: "))
student_avg = (midTerm() + final()) / 2 # 2 scores, summed and divided by 2 is their average score
if (highest < student_avg):
highest = student_avg
winner = student_name # save student name for later
print ("The Student with the higgest average is: ", winner, "With the highest average of: ", highest)
It looks like you were originally trying to calculate the total class average, which is not what's described by the problem statement. Hope this helps!

Categories

Resources