PDA

View Full Version

: Help with Python please?


K-Dub
03-10-2009, 08:50 PM
Ask the user to enter three names. Print the names in alphabetical order.

This is what I got so far.. sort of. Not working though. :/

name1 = raw_input ("Enter a name:")
name2 = raw_input ("Enter a second name:")
name3 = raw_input ("Enter a third name:")

if name1 < name2:
....if name1 < name3:
........print name1
....elif name3 < name2:
........print name3
elif name2 < name3:
....print name2
....if name2 < name1:
.......print name2
else:
....print name3


Ask the user to enter a percentage, between 0 and 100. Tell them what letter grade this corresponds to, with an A between 100 and 85, a B between 84 and 70, a C between 69 and 60, a D between 59 and 50 and an F below 50.

This one I'm having more trouble understanding/charting out.

percentage = raw_input ("Enter a percent between 0-100:")
if percentage <50:
....print ""+ percentage +"% is equal to an F"
........if percentage >=50:
............print ""+ percentage +"% is equal to a D"
........elif percentage >=60:
............print ""+ percentage +"% is equal to a C"
........elif percentage >=70:
............print ""+ percentage +"% is equal to a B"
else:
....print ""+ percentage +"% is equal to a A"

MegaMx
03-10-2009, 08:51 PM
put it in an array and order it for the first one (that way you can have infinite number of names). OR beginner way (wayyyyyy more code)

if name1 > name2:
if name1 > name3:
print name1
if name2 > name3:
print name2 + "\n" + name3;
else
print name3 + "\n" + name2;
else
print name3 + "\n" + name1 + "\n" + name2

..... then elif to see if the scenario is that another one is bigger

As for the second one you can use "case" or if you want to do it the long way "ifs" do work. just do it this way

percentage = raw_input ("Enter a percent between 0-100:")
if percentage >= 80:
print percentage +"% is equal to an A"
elif percentage >= 70:
print percentage +"% is equal to a B"
elif percentage >=60:
print percentage +"% is equal to a C"
elif percentage >=50:
print percentage +"% is equal to a D"
else:
print percentage +"% is equal to a F"

k20a
03-10-2009, 09:35 PM
Fuck this is like rocket science. What are you guys talking about? Is it about girls? I want in too.

bbbj
03-10-2009, 09:50 PM
CMPT 120? I HATE THAT FUCKING CLASS

pandalove
03-10-2009, 09:54 PM
You need to construct additional Pylons.

BoS_DC2
03-10-2009, 10:09 PM
You need to construct additional Pylons.

LOL. :haha:

Goliath online.

c_loke
03-10-2009, 10:14 PM
Fuck this is like rocket science. What are you guys talking about? Is it about girls? I want in too.
girls dont exist in computer science.

bbbj
03-10-2009, 10:20 PM
girls dont exist in computer science.

Does Tifa and Aeris count?

RFlush
03-10-2009, 10:55 PM
Isn't this CMPT 165? If so, I can give you my assignments

StaxBundlez
03-10-2009, 11:00 PM
yah i got some pythons to help you right here buddy
http://www.criticalbench.com/images/bigarm.jpg

BATHROOMS OVER THERE!!
<---------------

w00tgasm
03-10-2009, 11:09 PM
Ask the user to enter three names. Print the names in alphabetical order.



Fuck this one haha.
I can't remember what to use for this.


Ask the user to enter a percentage, between 0 and 100. Tell them what letter grade this corresponds to, with an A between 100 and 85, a B between 84 and 70, a C between 69 and 60, a D between 59 and 50 and an F below 50.

This one I'm having more trouble understanding/charting out.



You're going to want to limit the user input between going below 0 or above 100.

To do that you may want to use a while statement


getgrade = 1;
while getgrade==1:
percentage = raw_input("Enter your grade between 0-100")
if (percentage > 100 or percentage < 0):
getgrade = 1;
else:
getgrade = 0;

runcode = 1;
while runcode == 1:
if percentage < 50:
print "You got an F for your percentage of " + percentage;
runcode = 0;
elif percentage < 59:
print "You got an D for your percentage of " + percentage;
runcode = 0;
elif percentage < 69:
print "You got a C for your percentage of " + percentage;
runcode = 0;
elif percentage < 84:
print "You got a B for your percentage of " + percentage;
runcode = 0;
elif percentage < 100:
print "You got a A for your percentage of " + percentage;
runcode = 0;


Because you're working up from 0 you're going to end the code there so it doesn't print F, D, C, etc.

My python may be off, but hope it makes sense to you.

w00tgasm
03-10-2009, 11:11 PM
put it in an array and order it for the first one (that way you can have infinite number of names). OR beginner way (wayyyyyy more code)

if name1 > name2:
if name1 > name3:
print name1
if name2 > name3:
print name2 + "\n" + name3;
else
print name3 + "\n" + name2;
else
print name3 + "\n" + name1 + "\n" + name2

..... then elif to see if the scenario is that another one is bigger

As for the second one you can use "case" or if you want to do it the long way "ifs" do work. just do it this way

percentage = raw_input ("Enter a percent between 0-100:")
if percentage >= 80:
print percentage +"% is equal to an A"
if percentage >= 70:
print percentage +"% is equal to a B"
elif percentage >=60:
print percentage +"% is equal to a C"
elif percentage >=50:
print percentage +"% is equal to a D"
else:
print percentage +"% is equal to a F"

If the percentage input is 90, it'll print out the print statements for A B C and D...

K-Dub
03-10-2009, 11:29 PM
Fuck this one haha.
I can't remember what to use for this.



You're going to want to limit the user input between going below 0 or above 100.

To do that you may want to use a while statement


getgrade = 1;
while getgrade==1:
percentage = raw_input("Enter your grade between 0-100")
if (percentage > 100 or percentage < 0):
getgrade = 1;
else:
getgrade = 0;

runcode = 1;
while runcode == 1:
if percentage < 50:
print "You got an F for your percentage of " + percentage;
runcode = 0;
elif percentage < 59:
print "You got an D for your percentage of " + percentage;
runcode = 0;
elif percentage < 69:
print "You got a C for your percentage of " + percentage;
runcode = 0;
elif percentage < 84:
print "You got a B for your percentage of " + percentage;
runcode = 0;
elif percentage < 100:
print "You got a A for your percentage of " + percentage;
runcode = 0;


Because you're working up from 0 you're going to end the code there so it doesn't print F, D, C, etc.

My python may be off, but hope it makes sense to you.
Thanks for the help, but I have no idea what the code of line that you put in means
(getgrade / run code line) nor have we covered it in class [CMPT165] haha.

Blaaaaaaaaahhhhhhhhh.

w00tgasm
03-10-2009, 11:31 PM
basically it terminates the program after it prints the statement, so it doesn't run through a series of the print statements...

but if you're not comfortable using it, don't.

meepmeepdeath
03-11-2009, 06:41 AM
all the code you require to finish the assignment is covered in his notes. I would be careful with the way you code your hw or else you might be bitch slapped by the prof, I've seen him ask students to explain their hw when he does not believe the student did the work

i would skip the getgrade = 1 and runcode = 1. you also dont require while loops for this hw

Thanks for the help, but I have no idea what the code of line that you put in means
(getgrade / run code line) nor have we covered it in class [CMPT165] haha.

Blaaaaaaaaahhhhhhhhh.

K-Dub
03-11-2009, 07:25 AM
all the code you require to finish the assignment is covered in his notes. I would be careful with the way you code your hw or else you might be bitch slapped by the prof, I've seen him ask students to explain their hw when he does not believe the student did the work

i would skip the getgrade = 1 and runcode = 1. you also dont require while loops for this hw
I'm not taking it with Baker, but I know. I do want to understand it, just need a little....guidance.

Anyways, figured out the second one. I was missing the int(), if I didn't have it in before it wouldn't do anything else after the first if.

percentage = int(raw_input("Enter a percent between 0-100:"))

if percentage >=85:
print percentage, "% is equal to a A"
elif percentage >=70:
print percentage, "% is equal to a B"
elif percentage >=60:
print percentage, "% is equal to a C"
elif percentage >=50:
print percentage, "% is equal to a D"
else:
print percentage, "% is equal to a F"

Still stuck on the first one. :/

jtanner_
03-11-2009, 09:15 AM
I'm not taking it with Baker, but I know. I do want to understand it, just need a little....guidance.

Anyways, figured out the second one. I was missing the int(), if I didn't have it in before it wouldn't do anything else after the first if.

percentage = int(raw_input("Enter a percent between 0-100:"))

if percentage >=85:
print percentage, "% is equal to a A"
elif percentage >=70:
print percentage, "% is equal to a B"
elif percentage >=60:
print percentage, "% is equal to a C"
elif percentage >=50:
print percentage, "% is equal to a D"
else:
print percentage, "% is equal to a F"

Still stuck on the first one. :/

Hey Colin here, Im watching you.

K-Dub
03-11-2009, 09:29 AM
Hey Colin here, Im watching you.
Liessssssssssss!

Orion
03-11-2009, 09:44 AM
hahaha i took cmpt 165 way before they implemented python but was forced to learn it in another comp class...

jtanner_
03-11-2009, 10:01 AM
Liessssssssssss!

I like your glasses. :thumbsup:

AzNightmare
03-11-2009, 12:00 PM
Does Tifa and Aeris count?

They do!!!!

MegaMx
03-11-2009, 02:11 PM
If the percentage input is 90, it'll print out the print statements for A B C and D...

yea i forgot "elif" on the second one. it would just print out one because it stops. a while statement may be necessary if you're giving it to somebody retarded who is going to put something other than 0-100 but otherwise redundant

MegaMx
03-11-2009, 02:13 PM
I'm not taking it with Baker, but I know. I do want to understand it, just need a little....guidance.

Anyways, figured out the second one. I was missing the int(), if I didn't have it in before it wouldn't do anything else after the first if.

percentage = int(raw_input("Enter a percent between 0-100:"))

if percentage >=85:
print percentage, "% is equal to a A"
elif percentage >=70:
print percentage, "% is equal to a B"
elif percentage >=60:
print percentage, "% is equal to a C"
elif percentage >=50:
print percentage, "% is equal to a D"
else:
print percentage, "% is equal to a F"

Still stuck on the first one. :/
What's wrong with that one? (what error are you getting?)

SuperSlowSS
03-11-2009, 04:34 PM
you suck at logic. drop it now. lol

K-Dub
03-11-2009, 05:34 PM
What's wrong with that one? (what error are you getting?)

I didn't get the one about sorting alphabetically. I drew out a flowchart, etc..things were going good but just one variation of "ABC" would fuck it up.


'''
Question6
'''
name1 = raw_input ("Enter a name:")
name2 = raw_input ("Enter a second name:")
name3 = raw_input ("Enter a third name:")

if name1 < name2:
....if name2 < name3:
........print name1 + "\n" + name2 + "\n" + name3
....elif name1 < name3:
........print name1 + "\n" + name3 + "\n" + name2
....else:
........print name3 + "\n" + name1 + "\n" + name2
elif name1 < name3:
....if name2 < name3:
........print name3 + "\n" + name1 + "\n" + name2
........if name1 < name2:
............print name2 + "\n" + name3 + "\n" + name1
........else:
............print name3 + "\n" + name2 + "\n" + name1
....elif name3 < name2:
........print name2 + "\n" + name3 + "\n" + name1

else:
....print name3 + "\n" + name2 + "\n" + name1

Run these through it
ABC
ACB
BAC
BCA
CAB
CBA

The only one that will fuck it up will be BAC. I can't single out how to get that one to sort properly.

Anyways, fuck it, you know what?
The assignment is worth 1.25%.
Getting 6/7 questions right is enough.

you suck at logic. drop it now. lol

Programmers logic I do not have. :(

dual
03-11-2009, 10:42 PM
you suck at logic. drop it now. lol

That's actually good advice. If you can't even get a B in this CMPT 120, it only gets harder from there. Next is CMPT 125, 225, 275. Make sure you have Cukierman as your prof for 125. CMPT 225 and 275 is basically hell. If you do happen to do well in this course, take CMPT 165 and own all the art majors who don't know what python is.

Anyways, looking at your previous post, it looks like your algorithm is wrong in the second half. There should only be 6 'print' statements b/c there are only 6 possible ways to arrange the names (which you have listed out) assuming the names can't be exactly the same as each other. So your algorithm should be

If name1 before name2
..if name2 before name3
....print "name1, name2, name3"
..elif name1 before name3
....print "name1, name3, name2"
..else
....print "name3, name1, name2"
else //at this point, name2 is before name1
..if name3 before name2
....print "name3, name 2, name1"
..elif name3 before name1
....print "name2, name3, name1"
..else
....print "name2, name1, name3"

K-Dub
03-11-2009, 10:51 PM
That's actually good advice. If you can't even get a B in this CMPT 120, it only gets harder from there. Next is CMPT 125, 225, 275. Make sure you have Cukierman as your prof for 125. CMPT 225 and 275 is basically hell. If you do happen to do well in this course, take CMPT 165 and own all the art majors who don't know what python is.
....this is cmpt165.
im not majoring in cmpt this is just an elective.

c_loke
03-11-2009, 11:12 PM
I didn't get the one about sorting alphabetically. I drew out a flowchart, etc..things were going good but just one variation of "ABC" would fuck it up.


'''
Question6
'''
name1 = raw_input ("Enter a name:")
name2 = raw_input ("Enter a second name:")
name3 = raw_input ("Enter a third name:")

if name1 < name2:
....if name2 < name3:
........print name1 + "\n" + name2 + "\n" + name3
....elif name1 < name3:
........print name1 + "\n" + name3 + "\n" + name2
....else:
........print name3 + "\n" + name1 + "\n" + name2
elif name1 < name3:
....if name2 < name3:
........print name3 + "\n" + name1 + "\n" + name2
........if name1 < name2:
............print name2 + "\n" + name3 + "\n" + name1
........else:
............print name3 + "\n" + name2 + "\n" + name1
....elif name3 < name2:
........print name2 + "\n" + name3 + "\n" + name1

else:
....print name3 + "\n" + name2 + "\n" + name1

Run these through it
ABC
ACB
BAC
BCA
CAB
CBA

The only one that will fuck it up will be BAC. I can't single out how to get that one to sort properly.

Anyways, fuck it, you know what?
The assignment is worth 1.25%.
Getting 6/7 questions right is enough.



Programmers logic I do not have. :(

I've never done python myself, but the way you can look at sorting alphabetically is by taking the word and tokenizing it. So for example, Duck could be place in an array of size 4, each element holds the letter of the word, then you do that for your other words, and then do a string compare. its super tedious but it works.

Suppose,

a[] = Duck
b[] = Dick

Compare the a[0] to b[0], if they are equal, then compare a[1] b[1]. if b[1]<a[1] then ouput dick, duck.

it gets real nasty when you have 3 words, thats why we use the built in language functions :D

dual
03-11-2009, 11:20 PM
....this is cmpt165.
im not majoring in cmpt this is just an elective.

Haha I bet one of your CMPT friends told you it was ezpz. Have you tried the algorithm I posted above? It should work assuming you don't have names that are the same.