Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Python.. running into infinite loop
New Posts  All Forums:Forum Nav:

# Python.. running into infinite loop

I'm having problems with my python code that is for a school assignment. I keep running into an infinite loop. I tried everything I've learned so far and still can't figure out what I am doing wrong. If I enter -50 or anything of that nature it will run an infinite loop saying "The score must be greater than 0 and less than 100" so I tried messing with my while loop and got no where. If you can provide any help it would be greatly appreciated.

Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following functions in the program:
calc_average--This function should accept five test scores as arguments and return tthe average of the scores
determine_grade--This function should accept a test score as an argument and return a letter grade for the score based on the following grade scale:

90-100 A
80-89 B
70-79 C
60-69 D
Below 60 F

My teacher asked to implement these changes to the program also:

• Print grades for all individual tests and for the average test score. You must calculate grades in a function that has the test scores passed to it, one at a time.
• Have an additional function that accepts the score. The score must be at least 0 and no more than 100. All scores must be validated in a separate function that has the scores passed to it, one at a time.
• The program should print out 4.0 instead of an “A”, 3.0 instead of a “B”, 2.0 instead of a “C”, 1.0 instead of a “D” and 0.0 instead of an “F”
Code:
``````#
def main():
#
#Variable           Type                Purpose
#testavg            int                 holds the test average all scored tests
#

testavg = calc_average(validate)

print("The avergage Test score is ", testavg, "that makes it", grade)

def calc_average(validate):
#
#This function asks for accepts the test scores of 5 tests
#Then it will calculate the average and return it to the main for later use
#
#Variable           Type            Purpose
#test1              float           holds Test score for test 1
#test2              float           holds Test score for test 2
#test3              float           holds Test score for test 3
#test4              float           holds test score for test 4
#test5              float           holds test score for test 5
#testavg            float           holds the average test score
#

test1 = float(input ("Please enter the score for Test #1: "))
test1 = validate(test1)
print ("The Score for Test #1 is: ", test1, " which is a ", grade)
print()

test2 = float(input ("Please enter the score for Test #2: "))
test2 = validate(test2)
print ("The Score for Test #2 is: ", test2, " which is a ", grade)
print()

test3 = float(input ("Please enter the score for Test #3: "))
test3 = validate(test3)
print("The Score for Test #3 is: ", test3, " which is a ", grade)
print()

test4 = float(input ("Please enter the score for Test #4: "))
test4 = validate(test4)
print("The Score for Test #4 is: ", test4, "which is a ", grade)
print()

test5 = float(input ("Please enter the score for Test #5: "))
test5 = validate(test5)
print("The Score for Test #5 is: ", test5, "which is a ", grade)
print()

testavg = (test1 + test2 + test3 + test4 + test5) / float(5)

return testavg

def validate(score):
#
#This function validates the test scores. It makes sure the test scores are
#at least a 0 and no more than 100.
#
#Variable           Type            Purpose
#score              float           holds the test score

while score < 0 or score < 100:
print ("The score must be at least 0 and no more then 100")

return score

#
#Thie function gives the test score its letter grade (GPA) for the test and returns it
#
#Variable       Type            Purpose
#testavg
if testavg >= 90:
elif testavg >= 80:
elif testavg >= 70:
elif testavg >= 60:
elif testavg <= 59:

main()```
```
 Big Blue (13 items)
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518
 Big Blue (13 items)
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518
In your validate function, you need
Code:
````while score < 0 or score > 100:`
```

I.e., ask for input while the score is less than 0 or greater than 100. Your current solution would stop of the user input 101.

Another way to write this in python is:
Code:
````while not 0 <= score <= 100:`
```
 Cube (9 items)
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540
Mouse
Corsair M65
 Cube (9 items)
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540
Mouse
Corsair M65
Quote:
Originally Posted by jvolkman

In your validate function, you need
Code:
````while score < 0 or score > 100:`
```

I.e., ask for input while the score is less than 0 or greater than 100. Your current solution would stop of the user input 101.

Another way to write this in python is:
Code:
````while not 0 <= score <= 100:`
```

Hello jvolkman,

Thanks for the reply! I tried what you suggested with both codes and its still running into an infinite loops when you enter -50

Thanks again,
ALLAN
 Big Blue (13 items)
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518
 Big Blue (13 items)
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518
That's because you're not assigning a new value to score.

Your program flow is as follows:
Code:
``````input score ----> is score between 0 and 100 --YES--> carry on
^              |
|              |NO
|              |
|              V
recheck score```
```

You need to break that look to re-request the user to input the score:
Code:
``````input score ----> is score between 0 and 100 --YES--> carry on
^              |
|              |NO
|              |
|              V
|        read input for new score
|              |
|              |
|              V
recheck score   ```
```

edit: looking at your code, that might just be an indentation problem
Edited by Plan9 - 11/11/13 at 3:20am
Quote:
Originally Posted by Plan9

edit: looking at your code, that might just be an indentation problem

Ah, yes, he's assigning a value to score outside of the while loop which will never work. Unless the forum software messed with the indentation.

Should look like:
Code:
``````def validate(score):
#
#This function validates the test scores. It makes sure the test scores are
#at least a 0 and no more than 100.
#
#Variable           Type            Purpose
#score              float           holds the test score

while score < 0 or score > 100:
print ("The score must be at least 0 and no more then 100")

return score```
```
 Cube (9 items)
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540
Mouse
Corsair M65
 Cube (9 items)
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540
Mouse
Corsair M65
Thanks for the replys guys, I was banging my head trying to figure out what the problem was and I can't believe it was an indentation problem. I never thought to check that I guess python is really picky about spaces and indents when coding with it. I'll have to be more careful next time.

Thanks again,
ALLAN
 Big Blue (13 items)
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518
 Big Blue (13 items)
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518
Quote:
Originally Posted by xXxALLANxXx

Thanks for the replys guys, I was banging my head trying to figure out what the problem was and I can't believe it was an indentation problem. I never thought to check that I guess python is really picky about spaces and indents when coding with it. I'll have to be more careful next time.

Thanks again,
ALLAN

Yeah, it's a white space driven language so you have to be very careful in that regard.

In fact that's the main reason I don't like Python. But that's only my own personal preference - I'm not criticising the language in any way.
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• Python.. running into infinite loop
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Python.. running into infinite loop