Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looping with if/elif
#1
I'm not sure why this does not print grade value. I tried moving indent. I am trying to put grade after weighted average.

students = ['Tony','Steve', 'Bruce', 'Natasha', 'Carol', 'Sam']
category = [ 'Assignment', 'Quizzes', 'Projects', 'Essays', 'Exams']
weighted = [.1, .1, .3, .25, .25]

for i in students:
    print(i)
    total = 0

    for num in weighted:

        score = float(input('Enter scores: ')) * num
        total += score
        total /= sum(weighted)
            if total >= 90:
                grade = 'A'
            elif total >= 80:
                grade = 'B'
            elif total >= 70:
                grade = 'B'
            elif total >= 60:
                grade = 'D'
            else:
                grade = 'F'
    print('wieghted average = ' + str(total)
    print()
    print('Letter Grade: ' + grade)
I get this as output.
Tony
Enter scores: 95
Enter scores: 95
Enter scores: 80
Enter scores: 85
Enter scores: 82
wieghted average = 84.75
Steve
Enter scores:
Reply
#2
With the code published here you will get error, because indentation of lines 14-23 is wrong. Post the actual code you run.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I made a new file with data but ran old program. here is the actual program where I go indent error.
students = ['Tony',]
category = [ 'Assignment', 'Quizzes', 'Projects', 'Essays', 'Exams']
weighted = [.1, .1, .3, .25, .25]

for i in students:
    print(i)
    total = 0

    for num in weighted:

        score = float(input('Enter scores: ')) * num
        total += score
        total /= sum(weighted)
            if total >= 90:
                print('A')
            elif total >= 80:
                print('B')
            elif total >= 70:
                print('c')
            elif total >= 60:
                print('d')
            else:
                print('f')

    print('wieghted average = ' + str(total)
Error Message:
Error:
File "/Users/c/Downloads/#3.py", line 14 if total >= 90: ^ IndentationError: unexpected indent Process finished with exit code 1
Reply
#4
What don't you understand? Why are lines 14-23 indented so much?
Reply
#5
If I decrease indent, I get the following parsing error message:
Error:
File "/Users/c/Downloads/#3.py", line 28 ^ SyntaxError: unexpected EOF while parsing Process finished with exit code 1
Reply
#6
you have one missing closing parenthesis on last line
students = ['Tony',]
category = [ 'Assignment', 'Quizzes', 'Projects', 'Essays', 'Exams']
weighted = [.1, .1, .3, .25, .25]
 
for i in students:
    print(i)
    total = 0
 
    for num in weighted:
 
        score = float(input('Enter scores: ')) * num
        total += score
total /= sum(weighted)
if total >= 90:
    print('A')
elif total >= 80:
    print('B')
elif total >= 70:
    print('c')
elif total >= 60:
    print('d')
else:
    print('f')

print('wieghted average = ' + str(total))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thank you.
Reply


Forum Jump:

User Panel Messages

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