0

I am calculating the overall result for university year end marks. I have run into some trouble whereby the totalCredits and userCredits variables are not incrementing, thus my else statement is always being returned.

""" Python University Grade Calculator """
def main():
    totalCredits = 0
    userCredits = 0
    noOfModules = int(raw_input("How many modules do you wish to add? "))
    while noOfModules > 0:
        mod_worth = int(raw_input("How many credits is the module worth? "))
        totalCredits += mod_worth
        mod_user_percentage = int(raw_input("What was your overall percentage for this module? "))
        mod_user_mark = mod_worth * (mod_user_percentage/100)
        userCredits += mod_user_mark
        noOfModules -= 1
    user_percentage = (userCredits/totalCredits) * 100
    print "You earned a total of %i out of %i credits." %(userCredits, totalCredits)
    if user_percentage > 70:
        print "This resulted in a 1st for this University year!"

    elif user_percentage > 65:
        print "This resulted in a 2:1 for this University year!"

    elif user_percentage > 60:
        print "This resulted in a 2:2 for this University year!"

    else:
        print "You have unfortunately failed this year."

main()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
matt_isbell
  • 23
  • 1
  • 6
  • Have you done any debugging (`print`s, etc.)? Why do you think the values aren't incrementing? Could this be an integer division issue? – jonrsharpe Jul 05 '16 at 14:17
  • Briefly: in Python 2, `x/y` when `x` is smaller than `y` returns 0, because it performs integer division by default. Therefore, `mod_user_mark = mod_worth * (mod_user_percentage/100)` is `mod_worth * (0)`, which is 0. – TigerhawkT3 Jul 05 '16 at 14:17
  • you are trying to do floating point division with integers. change `int()` to `float()` for your raw inputs... – Aaron Jul 05 '16 at 14:17
  • Thanks guys, and apologies for not seeing the duplicate question! the float worked perfectly and is something I guess I should've seen!!! – matt_isbell Jul 05 '16 at 14:23

0 Answers0