Question

Sorry if this seems like a stupid question, I'm rather new to Python. I need to create a program for a school project. The project outline says this: Students can earn their final mark in a course in two ways. First the course work can be worth 60%, the final project worth 20% and the final exam worth 20%. Alternately, the course work can be worth 70%, the final project worth 10% and the final exam worth 20%. Use the following code as a start and create a program that outputs the highest grade the student could achieve.

course = 87
finalProject = 75
exam = 82

Once again, I apologize if this seems like a stupid question, I'm quite new to Python. I just need to know the best way of going about doing this.

Was it helpful?

Solution 2

It's nothing but maths. Really...

# Your starting point
course = 87
finalProject = 75
exam = 82

# What I would "crunch" into a calculator besides the variables
total1 = (course * 0.6) + (finalProject * 0.2) + (exam * 0.2)
total2 = (course * 0.7) + (finalProject * 0.1) + (exam * 0.2)

# Printing my computed answers just to make sure I can tell if it gives the right output
print "Total1: %s\tTotal2: %s" % (total1, total2)

# Printing the highest one. 
print "\nYour mark is: %s" % max(total1, total2)

See it in action: http://codepad.org/UsfAVC30

You might find this interesting: Interesting article from meta.programmers.stackexchange.com

OTHER TIPS

The built-in max(...) function just returns the greatest argument passed to it; it's also usable with lists: max([1, 2, 3]) => 3.

In your case:

highest = max(
    course * 0.6 + finalProject * 0.2 + exam * 0.2,
    course * 0.7 + finalProject * 0.1 + exam * 0.2
)

It's a simple math problem, being new to Python is irrelevant. Calculate the final mark using both equations, then check which one is larger. Output the value of the largest one.

You are comparing the first and the second grading system right? Shouldn't it be just two variables? You can use max() to compare to numbers: max(a, b) returns the higher between two numbers. The rest you can solve yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top