School Project, Need Help Fast!

Brian van den Broek bvande at po-box.mcgill.ca
Tue Oct 12 17:58:36 EDT 2004


Gavin Bauer said unto the world upon 2004-10-12 17:05:
> Please respond ASAP, the project is due soon.

 > In science, we are doing a "creative mini project" about minerals.
 > People can write a song, or make a movie, etc. etc. Being the dry,
 > uncreative person I am, I chose to use my limited python knowledge to
 > create a "Mineral Program" the idea is that a person inputs a mineral
 > name, and then the program gives them all the information about the
 > mineral. At first I figured this would be easy. I would make an input
 > line for mineral. Then I would use and if statement, and a whole
 > bunch of elif statements, to give the info, here is the broken
 > program I came up with:
> 

Hi Gavin,

I am no Python expert, but here's my go on how to help:

1) Set your email client to wrap lines. It make quoting you a lot easier :-)

2) In general, tell people why you think your program is broken. Again, 
easier.

3) You are using input() to get the mineral name. Don't. Use raw_input() 
instead. Why?

The general, good practise, reason is that input() is scary, as it 
executes code. Thus, if someone is mean and knowledgeable, or just 
(un)lucky, they could type in some input to do evil things like delete a 
directory on your drive.

For your purposes it isn't the right thing, either. input() does an eval 
on whatever the user types, so it doesn't give you back a string. Since 
you don't have names floating around with all the possible user input 
values in them, this isn't working--there is nothing to execute when 
someone types Iron as input. raw_input() is both safe, and gives you a 
string directly from the user's input. So, go with that ;-)

4) Now that mineral will be some string (thanks to raw_input() ), all of 
your if tests need to compare the string to a string. So, you need an 
equality check, not an assignment, and a string, not a name. Thus, they 
should be like

if mineral == "Augite":
     # Do Augite stuff

5) It is probably too late in the day, but rather than 30 if clauses, 
you'd be better of using a dictionary approach, or at least iterating 
over a list of the strings of the mineral names you have answers for. If 
you've got time to reactor and need help, ask again.

6) If you keep going with Python, you might want to check out the tutor 
list: http://mail.python.org/pipermail/tutor/

Hope that helps,

Brian vdB

> print "Hi! Welcome to Gavins mineral project!"
> print "We currently have a database of 30 minerals!"
> print "Simply type the mineral's name, with the first letter capitalized."
> print "To see what minerals we have available, type: List Minerals"
> print "For futher help, type: Help"
> print "To leave, type: Exit"
> print ""
> mineral = input("Mineral:")
> while mineral != Exit:
>     if mineral = Augite:
>         print "a"
>     elif mineral = Bauxite:
>         print "a"

<SNIP'ed a bunch of elifs>

>     elif mineral = Talc:
>         print "a"
>     elif mineral = List Minerals:
>         print "a"
>     elif mineral = Help:
>         print "a"
>     else:
>         print "That mineral isn't in our database."
>         print "Type Help for further assistance or try again."
> 
> raw_input("Press enter to exit")
> 
> The "a"'s would be where I would put all the mineral information. I figured I'd get the program straight before I did research. The "press enter to exit" thing is because my DOS shuts off the second it finishes computing, which leaves me without seeing the final information. I would love it if someone knew how I could change my Dos preference, but I would be pushing my luck to get a response in time at all.
> 
> Thanks a lot   -Gavin
> 





More information about the Python-list mailing list