Re: [Tutor] My First Program

2005-12-09 Thread Trent Rigsbee
Here's my new and improved program:

def quiz_question(some_question, some_right_answer):
question = raw_input(some_question)
if question == some_right_answer:
print "Yes!\n"
else:
print "Wrong\n"

quiz_question("Name the capital of NC? ", "Raleigh")
quiz_question("Name the capital of SC? ", "Columbia")
quiz_question("Name the capital of NY? ", "Albany")
quiz_question("Name the capital of OH? ", "Columbus")
quiz_question("Name the capital of TX? ", "Austin")

I took Danny's advice and plagerized his function code (sorry about that, 
Danny). I like it! It's much neater and I'm kicking myself for not realizing 
this. I didn't like the if-else chain but I was stuck on what to do. 
Creating programs is the way to learn! I know you've heard countless of 
wannabe coders say this but I'll read the material and I think I know what 
functions and loops (etc) are but when I try to create something, I'm stuck 
and not sure how to proceed. Speaking of stuck, I'm not sure how to create 
the counting variable that Greg suggested. Can someone lead me but don't 
tell me the answer? Thanks!


>From: Danny Yoo <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>CC: Joseph Quigley <[EMAIL PROTECTED]>, Tutor 
>Subject: Re: [Tutor] My First Program
>Date: Thu, 8 Dec 2005 13:52:54 -0800 (PST)
>
>
> > > Hi! My first "ever" program that I've created is a simple game called
> > > "State Capitals". It's straight forward; 5 simple questions about
> > > state capitals in a non-GUI format.
>
>[some code cut]
>
>Hi Trent,
>
>
>Looks good so far!  There is one direct thing we can do to make the
>program a little shorter: we can use "functions".  If you want, we can go
>through an example to see what these things do.
>
>
>There's a fairly consistant pattern to each of the quiz questions.  If we
>look at the first two blocks of questions, for example:
>
>
> > > question = raw_input("Question 1 - What is the capital of NC, a: 
>Raleigh
> > > or
> > > b: Atlanta? ")
> > > if question == "a":
> > > print "Yes\n"
> > > else:
> > > print "WRONG\n"
>
>
> > > question = raw_input("Question 2 - What is the capital of SC, a:
> > > Greenville
> > > or b: Columbia? ")
> > > if question == "b":
> > > print "Yes\n"
> > > else:
> > > print "WRONG\n"
>
>
>and if we squint our eyes a bit, we might see a pattern here, something
>like:
>
> question = raw_input(  {some question here}  )
> if question ==  {some right answer here}:
> print "Yes\n"
> else:
> print "Wrong\n"
>
>where I've put placeholders (the stuff in braces) to mark the places
>that are different.
>
>
>There is a feature in many programming languages called the "function"
>that allows us to capture this pattern and give it a name.  Think Mad
>Libs: what we can do is make a mad-lib game form, and then let people fill
>in what they want.
>
>The block above can be turned into this function:
>
>###
>def quiz_question(some_question, some_right_answer):
> question = raw_input(some_question)
> if question == some_right_answer:
> print "Yes\n"
> else:
> print "Wrong\n"
>###
>
>
>'quiz_question' is the name I've given this, though if you want to call
>it something else, please feel free to change the name.
>
>
>Once we have 'quiz_question', how do we use this?  Let try this from the
>interactive interpreter:
>
>##
> >>> quiz_question("what's your favorite color?", "green")
>what's your favorite color?green
>Yes
>
> >>> quiz_question("what's your favorite color?", "green")
>what's your favorite color?blue no red!
>Wrong
>##
>
>
>Does this make sense so far?
>
>By having that function, it'll lets you add more questions to your quiz by
>just adding the stuff that's really important: the content of the
>questions and their right answers.
>
>
>The tutorials on:
>
> http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
>
>should talk about functions a bit.  Try playing with functions: it should
>make your program shorter, and that'll make it a little easier to think
>about how to do the three-strikes-you're-out! thing.
>


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] My First Program

2005-12-07 Thread Trent Rigsbee
Hi! My first "ever" program that I've created is a simple game called "State 
Capitals". It's straight forward; 5 simple questions about state capitals in 
a non-GUI format. Can someone look over my code and offer tips, suggestions, 
criticism? Also, I'd like to be able to end the program if the user misses 3 
questions (i.e., kick them out after the 3rd wrong answer). How can I do 
this? Thanks!


print "\nState Capitals! Answer a or b to the following questions.\n"
question = raw_input("Question 1 - What is the capital of NC, a: Raleigh or 
b: Atlanta? ")
if question == "a":
print "Yes\n"
else:
print "WRONG\n"

question = raw_input("Question 2 - What is the capital of SC, a: Greenville 
or b: Columbia? ")
if question == "b":
print "Yes\n"
else:
print "WRONG\n"

question = raw_input("Question 3 - What is the capital of NY, a: Albany or 
b: Buffalo?")
if question == "a":
print "Yes\n"
else:
print "WRONG\n"

question = raw_input("Question 4 - What is the capital of OH, a: Cleveland 
or b: Columbus? ")
if question == "b":
print "Yes\n"
else:
print "WRONG\n"

question = raw_input("Question 5 - What is the capital of TX, a: Houston or 
b: Austin? ")
if question == "b":
print "Yes\n"
else:
print "WRONG\n"


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Dictionary Error: 'dict' object has no attribute '_contains_'

2005-11-06 Thread Trent Rigsbee
Hi! I'm on the version 2.4, going through Beginning Python (Wrox),  and I'm 
getting the above error. I'm trying to do this:

menu_specials._contains_("test")

Any ideas on what I'm doing wrong? Thanks!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Creating Programs (Basics?)

2005-10-23 Thread Trent Rigsbee
Hi! I'm on Gauld's wonderful "Learning to Program" tutor. I've got to the 
end of the chapter on "Error Handling" and I'm trying the sample ideas for 
creating my own programs. My problem is getting started! Is there a "step by 
step" method for creating programs or some basic guidelines for this? Also, 
how are computer science students taught this? Thanks!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] First Project - Ping Sweeper!

2005-07-12 Thread Trent Rigsbee
Hi! I've completed Learning to Program (off the website) and I've started to 
play around with code that I've found on Useless Python. I'm ready to start 
my first project! This may be way over my head, but I want to make a ping 
sweeper (put a range of IP addresses & ping to see which are vaild). I'm 
playing around with Mark Kels' Port Scanner off of Useless Python and I 
wanted to modify this for pinging but I'm stuck. I've ordered Network 
Programming for Python but I've started to play around with this until the 
book arrives. Any suggestions? Should I throw in the towel and make 
something simpler instead? Thanks! Here's the code that I'm tinkering with:

#A simple port scanner to scan a range of ports on a single host.

#Developers:
# 1.Mark Kels ([EMAIL PROTECTED]) - The basic port scanner
#Last updated:
# 21.5.2005

#--Imports--
from Tkinter import * #Used to make the GUI
import tkMessageBox #Used for error display
import socket #Used for connecting to ports
import threading #Used to make a difrent thread for the scan so it could be 
stopped

#--- Function to start a scan ---
def go():
global app
result.delete(1.0,END)
app=scan()
app.start() #start() is definde in threading.Thread
#--- Function to stop a scan ---
def stop():
app.flag='stop'
#--- Function to clear the input and output ---
def clear():
host_e.delete(0,END)
start_port_e.delete(0,END)
end_port_e.delete(0,END)
result.delete(1.0,END)

#---The scan class which does the port scan itself---
class scan(threading.Thread):
def _init_(self):
threading.thread._init_(self)
def run(self):
self.host=host_e.get()
self.start_port=int(start_port_e.get())
self.end_port=int(end_port_e.get())
self.open_counter=0
self.flag='scan'
start.config(text="Stop",command=stop)
root.update()
result.insert(END,"Scanning "+str(self.host)+"...\n\n")
root.update()
while self.start_port<=self.end_port and self.flag=='scan':
self.sk=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sk.settimeout(0.01) #closed ports take a long time to 
connect to, so if there is no connection after 0.01 seconds the port is 
closed
try:
self.sk.connect((self.host,self.start_port))
except:
pass #if connection fails (port is closed) pass and try with 
another port
else:
result.insert(END,str(self.start_port)+"\n")
root.update()
self.open_counter=self.open_counter+1
self.sk.close()
self.start_port=self.start_port+1
if self.flag=='scan':
result.insert(END,"\nDone !!\nFound "+str(self.open_counter)+" 
opened ports")
root.update()
start.config(text="Scan",command=go)
root.update()
elif self.flag=='stop':
result.insert(END,"\n Scan stopped.")
start.config(text="Scan",command=go)
root.update()

#---The GUI---
root=Tk()
Label(root,text="Host: ").grid(row=1,column=1,sticky="w")
host_e=Entry(root)
host_e.grid(row=1,column=2,sticky="WE")
Label(root,text="Start port: ").grid(row=2,column=1,sticky="w")
start_port_e=Entry(root)
start_port_e.grid(row=2,column=2,sticky="WE")
Label(root,text="End port: ").grid(row=3,column=1,sticky="w")
end_port_e=Entry(root)
end_port_e.grid(row=3,column=2,sticky="WE")
start=Button(root,text="Scan",command=go)
start.grid(row=5,columnspan=3,sticky="WE")
clear=Button(root,text="Clear",command=clear)
clear.grid(row=6,columnspan=3,sticky="WE")
result=Text(root,width=20,height=20)
result.grid(row=7,columnspan=3,sticky="WENS")


root.wm_maxsize(width='190',height='370') #Set max size
root.wm_minsize(width='190',height='370') #Sat min size same as max size (so 
the window is unresizeable)
root.title("PPS 0.1") #Set the title of the window

root.mainloop()


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor