d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some Python Help
Add Reply New Topic New Poll
Member
Posts: 3,373
Joined: Dec 2 2006
Gold: 0.56
Feb 12 2013 08:25pm
So I have an assignment to do with data structures. This is bonus work so no teaching has been done on the subject.

I've used basic lists in python but never anything else. I may just be not thinking about this the right way and missing something.

I need to take a test grade input from the user from a test graded on a 100 point scale. 100-90 is an A, 89-80 is a B, 79-70 is a C, 69-65 is a D, and 64-0 is an F. The program then needs to determine the what letter grade the inputted score is and output it. My professor told me it can be done in 3 lines, but I just don't see how. It doesn't HAVE to be 3 lines, he just said it could be. He also said that no IF statements were needed.

Very confused and help would be greatly appreciated.

This post was edited by KratosGOW on Feb 12 2013 08:26pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 12 2013 08:33pm
considering the hints, i'm guessing 1 line to set up the mapping, 1 line to get input, 1 line to produce output. if you dont need if statements, then your mapping must move from int -> letter grade. sounds like an array is good for that.

eg:

mapping = ['F', 'F', ....., 'D', 'D', 'D', ..... , 'A', 'A', 'A' ...]
score = cast_int(raw_input("enter grade 0-100"));
print mapping[score]

where your array is constructed in such a way that indexes 0 to 64 returns F, 65-69 D, etc etc.

of course that would fail if they enter invalid input (eg: 33.3, 101, "blahblah", -11, etc)
Member
Posts: 3,373
Joined: Dec 2 2006
Gold: 0.56
Feb 12 2013 08:37pm
Quote (carteblanche @ Feb 12 2013 09:33pm)
considering the hints, i'm guessing 1 line to set up the mapping, 1 line to get input, 1 line to produce output. if you dont need if statements, then your mapping must move from int -> letter grade. sounds like an array is good for that.

eg:

mapping = ['F', 'F', ....., 'D', 'D', 'D', ..... , 'A', 'A', 'A' ...]
score = cast_int(raw_input("enter grade 0-100"));
print mapping[score]

where your array is constructed in such a way that indexes 0 to 64 returns F, 65-69 D, etc etc.

of course that would fail if they enter invalid input (eg: 33.3, 101, "blahblah", -11, etc)


Wow.. Thank you so much. Yet again, I was looking too far into the problem. I like your solution. Not so elegant, but definitely pretty simple.

Thanks again!
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Feb 12 2013 08:49pm
starting with carteblanche's code, can be cheesy and cut it down to 2 lines

Code
mapping = ['F', 'F', 'D', 'D', 'D','A','A', 'A']
print mapping[int(raw_input("enter grade 0-100\n"))]

/e
even cheesier, 1 line!:
Code
print ['F', 'F', 'D', 'D', 'D','A','A', 'A'][int(raw_input("enter grade 0-100\n"))]


This post was edited by Azrad on Feb 12 2013 08:52pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 12 2013 08:55pm
Quote (Azrad @ Feb 12 2013 09:49pm)
taking carteblanche code, can be cheesy and cut it down to 2 lines

Code
mapping = ['F', 'F', 'D', 'D', 'D','A','A', 'A']
print mapping[int(raw_input("enter grade 0-100\n"))]


now that you mention it, we can compress into one line

print ["F", "F", ....][int(raw_input("enter grade 0-100\n"))];

gives you two extra lines for whatever else you want
Member
Posts: 3,373
Joined: Dec 2 2006
Gold: 0.56
Feb 12 2013 08:58pm
Quote (carteblanche @ Feb 12 2013 09:55pm)
now that you mention it, we can compress into one line

print ["F", "F", ....][int(raw_input("enter grade 0-100\n"))];

gives you two extra lines for whatever else you want


Haha.. Yeah, I submitted this one being all cheeking and what not.. Maybe I can get some bonus bonus points.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll