954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

whats wrong with this code?

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

pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
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.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: