the scenario is a fitness test and the challenge was to return a string saying either Gold, Silver, Pass or Fail, depending on certain conditions, they were as follows;

gold - the candidate scored 4 or more at each station , and had a total of 13 or more points
silver - the candidate scored 3 or more at each station, and had a total of 11 or more points
pass - the candidate scored 2 or more at each station, and had a total of 7 or more points
fail - less than 2 points for any station or less than 7 over all points.

i wrote;

def Fitness(a,b,c):
	      f = [a,b,c]
	      if f[0:2] >= 4 and sum(f) >= 13:
		      print 'Gold'
	      elif f[0:2] >= 3 and sum(f) >= 10:
		      print 'Silver'
	      elif f[0:2] >= 2 and sum(f) >= 7:
		      print 'Pass'
	      else:
		      print 'Fail'

not particularly impressive, im a novice and im very tired, so its hard to think (got plenty of excuses, but not too many answers..)
so whats wrong with the code?
when i type in the following values it returns;
>>> Fitness(1,5,5)
Silver

however, its supposed to say Fail, since 1 is less than two.

i tried ;

elif sum(f) < 7:
		      print 'Fail'
	      elif f[0:2] < 2 :
		      print 'Fail'

but it still doesn't work.

please advise, and also provide a simpler and cleaner method, for future reference.
I should be able to do better than this, but im finding it hard to recall things at the moment.

thank you for any help

since 1 is less than two

Then it is not one. Add a print statement to show what is being compared.

def Fitness(a,b,c):
	      f = [a,b,c]
              print "comparing", f[0:2], "against 2, 3, and 4"
	      if f[0:2] >= 4 and sum(f) >= 13:
		      print 'Gold'
	      elif f[0:2] >= 3 and sum(f) >= 10:
		      print 'Silver'
	      elif f[0:2] >= 2 and sum(f) >= 7:
		      print 'Pass'
	      else:
		      print 'Fail'

Also, store the sum in a field instead of calculating it several times, and min will give you the smallest number in the list.

def Fitness(a,b,c):
    f = [a,b,c]
    total = sum(f)
    smallest = min(f)
    if smallest >= 4 and total >= 13:
        print 'Gold'
    ## etc.

Edited 5 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.