Re: [Tutor] understanding __import__()

2006-07-25 Thread python-tutor
Sean Perry wrote:
> Ok, this may be slightly above tutor's level, but hey, never hurts to 
> ask (-:

__import__ is dark magic; generally those who venture into that realm
would do well to read the C source for Python..

I'm guessing that '.' is not in your sys.path, so Python isn't finding
the relative path, while the absolute path works fine.

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


Re: [Tutor] assert

2005-12-23 Thread python-tutor

Shivayogi,

Sorry my last e-mail wasn't very helpful.  

Better would have been:

>>> a=myClass()
>>> b=myClass()
>>> a.howmany()
>>> myClass.count=0
>>> del a
which will (hopefully)  give you something like: 

Exception exceptions.AssertionError:  in > ignored

Assert is commonly used as a sanity check for things that you always expect to 
be true.

Hope this helps,

Todd Maynard


On Friday 23 December 2005 06:55, [EMAIL PROTECTED] wrote:
> Try this:
> >>>a=myClass()
> >>>b=myClass()
> >>>a.howmany()
> >>>a.count=0
> >>>del a
>
> Does this help clear things up?
>
>
> Todd Maynard
>
> On Friday 23 December 2005 06:18, shivayogi kumbar wrote:
> > class myClass:
> >                         count = 0
> >                          def __init__(self):
> >                                   myClass.count = myClass.count + 1
> >                           def __del__(self):
> >                                    myClass.count =myClass.count -1
> >                                     assert myClass.count>0
> >                           def howmany(self):
> >                                    return myClass.count
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] assert

2005-12-23 Thread python-tutor
Try this:

>>>a=myClass()
>>>b=myClass()
>>>a.howmany()
>>>a.count=0
>>>del a

Does this help clear things up?


Todd Maynard

On Friday 23 December 2005 06:18, shivayogi kumbar wrote:
> class myClass:
>                         count = 0
>                          def __init__(self):
>                                   myClass.count = myClass.count + 1
>                           def __del__(self):
>                                    myClass.count =myClass.count -1
>                                     assert myClass.count>0
>                           def howmany(self):
>                                    return myClass.count
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Auto package install?

2005-12-06 Thread python-tutor
Scott,

Take a look at setuptools:  http://peak.telecommunity.com/DevCenter/setuptools 

It should handle everything you are looking for with ease.  Turbogears  
( http://turbogears.org )is a real world project that uses it.

So far it seems to be flexible and reliabe for me.

Good Luck,

--Todd Maynard  


On Tuesday 06 December 2005 12:16, Nelson, Scott wrote:
> The Zen of Python (http://www.python.org/doc/Humor.html#zen) states:
>
> "There should be one-- and preferably only one --obvious way to do it."
>
>
>
> I'm searching for the obvious Pythonic way to achieve automated package
> installation (I believe Perl's CPAN can be used to accomplish this in
> Perl?).  I'd like to have a way where a Python script can automatically
> download/install/update the versions of packages it needs to run.  I'm
> looking to avoid requiring the users of my scripts to manually download
> and install a pile of packages (with potential for user error) just to
> run my scripts.  I use mostly common packages such as wxPython.  My
> scripts may eventually get out to > 100 people over 3 states so I want
> to minimize administration and potential for user error.
>
>
>
> I'm aware of the very handy CheeseShop (http://www.python.org/pypi), but
> this doesn't automate the installation process.  I've done some googling
> and found ciphon (http://sourceforge.net/projects/pythonsiphon) and
> pypan (http://starship.python.net/crew/theller/pypan/), but both seem
> somewhat abandoned and incomplete.  EasyInstall
> (http://peak.telecommunity.com/DevCenter/EasyInstall) seems new and I'm
> willing to hear any experiences with it.
>
>
>
> I also glanced thru c.l.p and most of the discussions seem a few years
> old and bordered on holy wars with no real obvious resolution...
>
>
>
> So, what is the obvious way to accomplish this?  If no such solution
> exists currently, what do people consider "best practices" when
> releasing scripts dependent on other packages to a number of users?
>
>
>
> Thanks for the help.  I'm relatively new to the group and it has been
> great so far!
>
>
>
> -Scott
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Console output

2005-10-05 Thread python-tutor

>>> import sys
>>> import time
>>> myList=range(10)
>>> for x in myList:
... sys.stdout.write(str(x) + "\r")
... sys.stdout.flush()
... time.sleep(1)

The "\r" causes a return without a newline feed.
Flush forces the text to be output (instead of getting buffered)

Enjoy!

Todd Maynard




On Wednesday 05 October 2005 02:13 pm, Oliver Maunder wrote:
> Does anyone know how I can update a line of console output without creating
> a new line? I'm not explaning this too well, so here's an example.
>
> When you download a file with wget, the console display looks like this:
>
> 14% [===> ] 344,192 16.28K/s ETA 02:19
>
> All the figures and the progress bar get continously updated. The only way
> I know of sending output to the console is to use print or
> sys.stdout.write(), but that would give me:
> 14% [===> ] 344,192 16.28K/s ETA 02:19
> 18% [=> ] 344,192 16.28K/s ETA 02:19
> 20% [> ] 344,192 16.28K/s ETA 02:19
>
> ...and that's really not what I'm after!
>
> Any help and ideas would be appreciated
>
> Olly
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SSH commands in Python on Linux

2005-08-10 Thread python-tutor
Ignoring the python stuff for the moment

In answer to Question 1., You want to use Public Key authentication...this 
will let you log in without a password.Google for SSH Public Key 
Authentication will give you several hits for the howto's 

One pretty good one was 
http://www.puddingonline.com/~dave/publications/SSH-with-Keys-HOWTO/document/html-one-page/SSH-with-Keys-HOWTO.html

If you have access to Linux Journal magazine, they just had a great article 
about it in the Sept issue.

Good luck and have fun,

Todd
On Wednesday 10 August 2005 11:11 am, Bernard Lebel wrote:
> Hello,
>
> I'm trying to make a script to send a SSH command from a Linux
> computer to another Linux compter.
>
> The Python syntax I'm using...
>
>
> import os
> os.system( 'ssh [EMAIL PROTECTED] "ls"' )
>
>
> Now the problem is that I'm always asked for the password. So my
> question is two-fold:
>
> 1- Is there a way to not be asked for the root password?
> 2- If not, then is it possible to feed the SSH password input with my
> Python script? I have about stdin redirection, but I have to admit
> that I'm a bit lost and I don't know if it applies to SSH input as
> well.
>
> Right now I'm using the ls command to list processes, however
> ultimately I want to be able to kill some processes within a loop
> (this is for render farm management).
>
>
>
> Thanks
> Bernard
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What can I do with this code?

2005-08-10 Thread python-tutor
How about changing it into a math quiz program?

You have the program output random problems ("What is 14 * 3 ?")

And then you can output appropriate random insults, words of encouragement, or 
praise as appropriate until the user gets the answer right.   Just be careful 
with division.  You probably don't want to randomly generate a problem like 
("What is 22 / 7 ?") (unless you are going to ask for the remainder too...)  
A trick to get around this problem is to use multiplication to pick a 
dividend.

Psuedocode:

Answer = randomNum
Divisor = randomNum
Dividend = Answer * Divisor

Print "What is (Dividend) / (Divisor) ?"

Have fun

--Todd

On Tuesday 09 August 2005 07:59 pm, Nathan Pinno wrote:
> I coded the following just for fun:
>
> import random
> cool = ['Cool math!','Awesome!','Way to go!','Happy problem solving!']
> while 1:
> start = raw_input("Do you want to do some math? Yes or No")
> if start == "Yes":
> x = int(raw_input("First number:"))
> y = int(raw_input("Second number:"))
> print x*y
> print random.choice(cool)
> elif start == "No":
> break
> print "Goodbye and happy problem solving!"
>
> What can I do with it, other than let it be a fun example of coding?
>
> Nathan
> ---
> Early to bed,
> Early to rise,
> Makes a man healthy, wealthy, and wise.
> --Benjamin Franklin
> ---
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Get information from a web page

2005-08-08 Thread python-tutor
Not sure exactly what you have going wrong without the code or the exact url 
you are using.  

Does something like:

import urllib

page = 
urllib.urlopen("http://slashdot.org/article.pl?sid=05/08/08/043236&tid=126";).read()
print page 

give you the correct results?


Is the problem specific to only to that url?

--Todd


On Monday 08 August 2005 04:46 am, David Holland wrote:
> Hi All,
> I am trying to save to disk a webpage (so I can extract useful info) with a
> url of type "http://xxx.co.uk/index.php?node=2371&pagetree=&fromid=20397";.
> However the following :-
> urllib.urlopen, urllib._urlopener, urllib2.Request,
> urllib2.urlopen,urllib2.urlretrieve just retrieve this webpage :-
> http://xxx.co.uk (the root page) not the page I wanted to retrieve, any
> ideas about to get the right page ? Thanks inadvance
>
> David
>
>
> -
> Yahoo! Messenger  NEW - crystal clear PC to PC calling worldwide with
> voicemail
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question About chdir()

2005-08-08 Thread python-tutor
Let's see if I can get this right.as I am working on memory and not enough 
sleep.

The 'r' means that using a raw string so the backslashes aren't escaped 
out

The equivalent without using the 'r' would be: os.chdir('c:\\temp')

--Todd

On Monday 08 August 2005 03:07 am, Don Parris wrote:
> The book, "Programming Python", shows an example of os.chdir() on the
> Windows platform, as follows:
>
> os.chdir(r'c:\temp')
>
> What's the 'r' for?  It didn't seem to make any difference in how
> Python works - at least not on the surface.
>
> Thanks,
> Don
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] deck dealing program

2005-08-08 Thread python-tutor
Are you:
  a.) Having trouble with the code and looking for help?
  b.) Looking for suggestions on how to improve the code?
  c.) Offering the code as a demo for Nathan et al.?

I was just doing stuff along the same lines and was having fun seeing the 
different approaches to the same problem.  

--Todd 

On Monday 08 August 2005 02:38 am, luke wrote:
> from random import randint
>
> def identify_card(n):
> cardname = ""
> royals = ["Jack","Queen","King","Ace"]
> temp = n % 13
> if temp > 8:
> cardname += royals[temp-9]
> else:
> cardname += str(temp+2)
> cardname += " of "
>
> suits = ["Spades","Hearts","Diamonds","Clubs"]
> cardname += suits[n/13]
> return cardname
>
> def main():
> deck = range(52)
> cards = []
> while 1:
> x = raw_input("how many cards do you want? ")
> try:
> x = int(x)
> except ValueError:
> print "Invalid value exiting for I have no error code. Please
> use an int next time." raise SystemExit
> if x <= 52 and x >= 0:
> y = 0
> while y < x:
>
> cards.append(identify_card(deck.pop(randint(0,len(deck)-1 y += 1
> break
> print cards
> if __name__ == "__main__":
> main()
>
> #Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the invaild syntax? Error message and relative code supplied.

2005-07-31 Thread python-tutor

Remember this problem from yesterday?

Take a look at the line before the one you are getting the error on.
And count the ('s and the )'s.

--Todd



On Sunday 31 July 2005 07:38 pm, Nathan Pinno wrote:
> What the invalid syntax? Here is the error message:
> SyntaxError: invalid syntax
>   File "D:/Python22/grades.py", line 66
> which = which-1
> ^
> SyntaxError: invalid syntax
>
> Here is the code:
>
> max_points = [25,25,50,25,100]
> assignments = ['hw ch 1','hw ch 2','quiz   ','hw ch 3','test']
> students = {'#Max':max_points}
>
> def print_menu():
> print "1. Add student"
> print "2. Remove student"
> print "3. Print grades"
> print "4. Record grade"
> print "9. Exit"
>
> def print_all_grades():
> print '\t',
> for i in range(len(assignments)):
> print assignments[1],'\t',
> print
> keys = students.keys()
> keys.sort()
> for x in keys:
> print x,'\t',
> grades = students[x]
> print_grades(grades)
>
> def print_grades(grades):
> for i in range(len(grades)):
> print grades[i],'\t\t',
> print
>
> def choice():
> return int(raw_input("Menu Choice: "))
>
> def school():
> return raw_input("Student: ")
>
> while 1:
> print_menu()
> menu_choice = choice()
> if menu_choice == 1:
> print "Add student"
> name = school()
> students[name] = [0]*len(max_points)
> elif menu_choice == 2:
> print "Remove student"
> name = school()
> if students.has_key(name):
> del students[name]
> else:
> print "Student: ",name," not found."
> elif menu_choice == 3:
> print_all_grades()
>
> elif menu_choice == 4:
> print "Record Grade"
> name = school()
> if students.has_key(name):
> grades = students[name]
> print "Type in the number of the grade to record"
> print "Type in a 0 (zero) to exit"
> for i in range(len(assignments)):
> print i+1,' ',assignments[i],'\t',
> print
> print_grades(grades)
> which = 1234
> while which != -1:
> which = int(raw_input("Change which Grade: ")
> which = which-1
> if 0 <= which < len(grades):
> grade = int(raw_input("Grade: ")
> grades[which] = grade
> elif which != -1:
> print "Invalid Grade Number"
> else:
> print "Student not found"
> elif menu_choice == 9:
> break
> else:
> print "That's not a choice!"
> print "Goodbye."
>
> Thanks for the help in advance!
> Nathan
--- Begin Message ---



Here's the message:
  File "D:\Python22\prog4.py", line 19    while 
option != 9:    ^SyntaxError: 
invalid syntax
And the code:
#This program finds the area and perimeter of circles, rectangles, and 
squares.def menu():    print '1) Area 
(Circle)'    print '2) Area 
(Rectangle)'    print '3) Area 
(Square)'    print '4) Perimeter 
(Circle)'    print '5) Perimeter 
(Square)'    print '6) Perimeter 
(Rectangle)'    print '9) Exit'
 
import mathprint 'By Nathan Pinno'print 'ID# 
2413448'printprint 'Program 4 - Functions'printmenu()option 
= int(raw_input('Option (1,2,3,4,5,6,9): ')while option != 
9:    if option == 
1:    r = int(raw_input('Radius: 
')    print 'The area of the circle = 
',pi*(r**2)    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
2:    l = int(raw_input('Length: 
')    w = int(raw_input('Width: 
')    print 'The area of the 
rectangle = ',l*w    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
3:    s = int(raw_input('Side: 
')    print 'The area of a square = 
',s**2    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
4:    d = int(raw_input('Diameter: 
')    print 'The perimeter of the 
circle = ',pi*d    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
5:    l = int(raw_input('Length: 
')    w = int(raw_input('Width: 
')    print 'The perimeter of the 
rectangle = ',(2*l)+(2*w)    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    elif option == 
6:    s = int(raw_input('Side: 
')    print 'The perimeter of a 
square = ',s*4    option = 
int(raw_input('Option (1,2,3,4,5,6,9): ')    
else:    print 'That is not an 
option. Please choose an option.'    
option = int(raw_input('Option (1,2,3,4,5,6,9): ')print 
'Goodbye.'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
--- End Message ---
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Thanks to all!

2005-07-31 Thread python-tutor

Congratulations!  So what are you going to try next?

--Todd

On Sunday 31 July 2005 06:27 pm, Nathan Pinno wrote:
> The Giant Calculator is perfect also!
>   - Original Message -
>   From: Nathan Pinno
>   To: tutor@python.org
>   Sent: Sunday, July 31, 2005 3:34 PM
>   Subject: [Tutor] Thanks to all!
>
>
>   Thank you to all. All my calculators run perfect. I can now make a
> perfect Giant Calculator as well.
>
>   Thanks again,
>   Nathan
>
>
> ---
>---
>
>
>   ___
>   Tutor maillist  -  Tutor@python.org
>   http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with file I/O.

2005-07-30 Thread python-tutor
Have you worked through the diveintopython.org tutorial/book?  Chapter 6 
covers file handling.  If/When you get stuck post your code/question and ask 
for help.

--Todd

On Saturday 30 July 2005 11:49 pm, Nathan Pinno wrote:
> If anyone will help me learn file I/O, it would be appreciated. I went
> through Josh Cogliani's Non-Programmer's Tutorial for Python, and I still
> can't understand it.
>
> Thanks in advance,
> Nathan Pinno
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Now what do I do?(was Re: When I run this code, it just keeps repeating.)

2005-07-30 Thread python-tutor
You are almost there.

Do you undestand why you are getting the output that you are getting?

Pretend that you are the computer and walk through what happens when you enter 
9 at the menu:


 while cal_opt != 9:
 menu()
 cal_opt = cal() # This is where you entered '9' so cal_opt = '9'   
 if cal_opt == 1:   # False so skipped
 X = input("First number:" )  
 Y = input("Second number:" )
 print X, "+", Y, "= ",X + Y


<-- code cut out -->

 elif cal_opt == 7:  # False so skipped to else
 X = input("Number to find the square root of:" )
 print "The square root of", X, " = ",X**0.5
 else:  # skipped to here. You never checked for an == '9'
 print "That's not an option. Try again."
 menu()  #menu is printed
 cal()  # You entered 6, but it isn't saved in any variable
  # From here, the loop starts over at the while up top.
  # this time the while is false because cal_opt = '9' so
  # the computer goes to the first line outside of the loop
 print "Goodbye"   # which is this.


So the output makes sense, even if it isn't what you intended.  There are a 
couple of ways to fix this.  Can you figure it out on your own?

--Todd



On Saturday 30 July 2005 10:39 pm, Nathan Pinno wrote:
> Here is another screen shot:
> Mini Calculator
> By Nathan Pinno
>
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 3
> First number:4
> Second number:6
> 4 * 6 =  24
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 9
> That's not an option. Try again.
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 6
> Goodbye
>
> And the latest code:
> # This is a small calculator.
> def menu():
> print "CALCULATE MENU"
> print "1) Add"
> print "2) Subraction"
> print "3) Multiplication"
> print "4) Division w/o remainder"
> print "5) Division with remaider"
> print "6) Exponation"
> print "7) Square roots"
> print "9) Exit"
>
> def cal():
> return int(raw_input("Option: "))
>
> print "Mini Calculator"
> print "By Nathan Pinno"
> print
> while cal_opt != 9:
> menu()
> cal_opt = cal()
> if cal_opt == 1:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "+", Y, "= ",X + Y
> elif cal_opt == 2:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "-", Y, "= ",X - Y
> elif cal_opt == 3:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "*", Y, "= ",X * Y
> elif cal_opt == 4:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y
> elif cal_opt == 5:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y," R ", X % Y
> elif cal_opt == 6:
> X = input("First number:" )
> Y = input("Power:" )
> print X, "**", Y, "= ",X**Y
> elif cal_opt == 7:
> X = input("Number to find the square root of:" )
> print "The square root of", X, " = ",X**0.5
> else:
> print "That's not an option. Try again."
> menu()
> cal()
> print "Goodbye"
>
> Thanks in advance,
> Nathan Pinno.
> - Original Message -
> From: <[EMAIL PROTECTED]>
> To: 
> Sent: Saturday, July 30, 2005 8:10 PM
> Subject: Re: [Tutor] When I run this code, it just keeps repeating.
>
> > Looks like you are making some pretty good progress.
> >
> > The short answer to your question is that the menu & user input need to
> > be inside the while loop.  That way cal_opt has a chance to change value
> > before
> > it gets evaluated by the while loop again.
> >
> > Another comment - the global cal_opt is considered evil by many.  Better
> > would
> > be:
> >
> > def cal():
> >   return  int(raw_input("Option: "))
> >
> > print "Mini Calculator"
> > print "By Nathan Pinno"
> > print
> >
> > while cal_opt != 9:
> >   menu()
> >   cal_opt=cal()
> >
> >   if cal_opt == 1:
> > X = input("First number:" )
> > Y = input("Second number:" )
> > print X, "+", Y, "= ",X + Y
> >
> >   
> >
> >
> > Keep hacking away at it...  you're making good progress.
> >
> > --Todd
> >
> > On Saturday 30 July 2005 09:55 pm, Nathan Pinno wrot

Re: [Tutor] When I run this code, it just keeps repeating.

2005-07-30 Thread python-tutor
Looks like you are making some pretty good progress.

The short answer to your question is that the menu & user input need to be 
inside the while loop.  That way cal_opt has a chance to change value before 
it gets evaluated by the while loop again.

Another comment - the global cal_opt is considered evil by many.  Better would 
be:

def cal():
   return  int(raw_input("Option: "))

print "Mini Calculator"
print "By Nathan Pinno"
print

while cal_opt != 9:
   menu()  
   cal_opt=cal()

   if cal_opt == 1:
 X = input("First number:" )
 Y = input("Second number:" )
 print X, "+", Y, "= ",X + Y

   


Keep hacking away at it...  you're making good progress.

--Todd


On Saturday 30 July 2005 09:55 pm, Nathan Pinno wrote:
> When I run the following code it just keeps repeating. Here is a screen
> shot showing what I mean: Mini Calculator
> By Nathan Pinno
>
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 5
> First number:4
> Second number:2
> 4 / 2 =  2  R  0
> First number:3
> Second number:6
> 3 / 6 =  0  R  3
> First number:5
> Second number:6
> 5 / 6 =  0  R  5
> First number:
>
> Here is the relevant code:
> # This is a small calculator.
> def menu():
> print "CALCULATE MENU"
> print "1) Add"
> print "2) Subraction"
> print "3) Multiplication"
> print "4) Division w/o remainder"
> print "5) Division with remaider"
> print "6) Exponation"
> print "7) Square roots"
> print "9) Exit"
>
> def cal():
> global cal_opt
> cal_opt = int(raw_input("Option: "))
>
> print "Mini Calculator"
> print "By Nathan Pinno"
> print
> menu()
> cal()
> while cal_opt != 9:
> if cal_opt == 1:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "+", Y, "= ",X + Y
> elif cal_opt == 2:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "-", Y, "= ",X - Y
> elif cal_opt == 3:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "*", Y, "= ",X * Y
> elif cal_opt == 4:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y
> elif cal_opt == 5:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y," R ", X % Y
> elif cal_opt == 6:
> X = input("First number:" )
> Y = input("Power:" )
> print X, "**", Y, "= ",X**Y
> elif cal_opt == 7:
> X = input("Number to find the square root of:" )
> print "The square root of", X, " = ",X**0.5
> else:
> print "That's not an option. Try again."
> menu()
> cal()
> print "Goodbye"
>
> How do I stop this, and make it go back to the main menu?
>
> Thanks in advance,
> Nathan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I make a Python program keep repeating?

2005-07-30 Thread python-tutor
How does the user indicate that they are done with the program?

One solution is to ask at the end of each iteration if they want to repeat.

Psuedocode:

keep_going=True
while(keep_going)
   Run your program stuff
   keep_going = get_user_response("Do you want to run again?")


--Todd



On Saturday 30 July 2005 08:10 pm, Nathan Pinno wrote:
> Hi all,
>
> How do I make a program keep repeating until the user is done with it?
>
> Thanks,
> Nathan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the invalid synax? Error message and code supplied.

2005-07-30 Thread python-tutor
   
I'd count the parenthesis on the prior line:


option = int(raw_input('Option (1,2,3,4,5,6,9): ')
while option != 9:


Also when you notice that you are copying and pasting the same line over and 
over, it may be time to think about reorganizing the code a little bit (Once 
you have it working).

Happy Debugging 

--Todd


On Saturday 30 July 2005 06:19 pm, Nathan Pinno wrote:
> Here's the message:
>   File "D:\Python22\prog4.py", line 19
> while option != 9:
> ^
> SyntaxError: invalid syntax
>
> And the code:
> #This program finds the area and perimeter of circles, rectangles, and
> squares. def menu():
> print '1) Area (Circle)'
> print '2) Area (Rectangle)'
> print '3) Area (Square)'
> print '4) Perimeter (Circle)'
> print '5) Perimeter (Square)'
> print '6) Perimeter (Rectangle)'
> print '9) Exit'
>
> import math
> print 'By Nathan Pinno'
> print 'ID# 2413448'
> print
> print 'Program 4 - Functions'
> print
> menu()
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> while option != 9:
> if option == 1:
> r = int(raw_input('Radius: ')
> print 'The area of the circle = ',pi*(r**2)
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 2:
> l = int(raw_input('Length: ')
> w = int(raw_input('Width: ')
> print 'The area of the rectangle = ',l*w
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 3:
> s = int(raw_input('Side: ')
> print 'The area of a square = ',s**2
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 4:
> d = int(raw_input('Diameter: ')
> print 'The perimeter of the circle = ',pi*d
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 5:
> l = int(raw_input('Length: ')
> w = int(raw_input('Width: ')
> print 'The perimeter of the rectangle = ',(2*l)+(2*w)
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> elif option == 6:
> s = int(raw_input('Side: ')
> print 'The perimeter of a square = ',s*4
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> else:
> print 'That is not an option. Please choose an option.'
> option = int(raw_input('Option (1,2,3,4,5,6,9): ')
> print 'Goodbye.'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor