Re: [Tutor] Running Files with Command Lines

2012-02-12 Thread Alan Gauld

On 12/02/12 07:24, Yony Torres wrote:


"Once you’ve saved this text file, you can ask Python to run it

> by listing its full filename as the first argument to a
> python command, typed at the system shell prompt:"

% python script1.py


Lets break down what this line is saying. It comprises three parts:
1) the operating system command prompt, shown as '%'
2) the python interpreter shown as 'python'
3) the python script to be interpreted, shown as script1.py

You only need to type the last two since your OS will provide the first 
one. On Windows that will usually look like


C:\WINDOWS>

or similar.

The path to the interpreter normally looks like

C:\python31\python

And the path to your script we know is:

c:\users\myusername\documents\varios 2\python journey\script1.py

And because you have a space in there we must put quotes aroundd it:

"c:\users\myusername\documents\varios 2\python journey\script1.py"

So the command that you type in your CMD shell window should look like:

C:\python31\python "c:\users\myusername\documents\varios 2\python 
journey\script1.py"



all on one line.

Now we can shorten that significantly by setting Python to be in the 
PATH. That way you don't need to provide the full path to Python.
(To set the path follow the instructions in my tutor in the "Windows 
Command prompt" box in the "Getting started" topic.)


Next, you can change into the folder with your script using

CD "c:\users\myusername\documents\varios 2\python journey"

(The prompt should now show the path to your script)

Now you only need to type:

python script1.py


HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2012-02-12 Thread Evert Rol
  Hi,

Tip: use a meaningful subject line; attracts more/better attention and makes it 
easier to trace your email in the archives.

Continued at the bottom.

> import numpy as np
> import matplotlib.pyplot as plt
> E=[81.97400737666324, 322.0939978589591, 694.5766491226185]
> V0=1000
> a=0.1
> def V(x):
>if x > -a and x < a:
>return 0
>return V0
> 
> V=np.vectorize(V)
> #psi=np.vectorize(psi)
> x= np.linspace(-1.5*a,1.5*a,100)
> 
> plt.plot(x,V(x))
> plt.xlim(-5*a,5*a)
> plt.ylim(-.001*V0,1.01*V0)
> for m in E:
>x1=np.linspace(-a,+a,100)
>#y=m
>#plt.xlim(-5*a,5*a)
>#plt.axhline(m)
>#y=np.vectorize(y)
>plt.plot(x1,m)
>#plt.show()
>print m
> 
> Error:
> ---
> ValueErrorTraceback (most recent call last)
> C:\Python27\lib\site-packages\IPython\utils\py3compat.pyc in
> execfile(fname, glob, loc)
>166 else:
>167 filename = fname
> --> 168 exec compile(scripttext, filename, 'exec') in glob, loc
>169 else:
>170 def execfile(fname, *where):
> 
> C:\Users\as\uy.py in ()
> 23 #plt.axhline(m)
> 
> 24 #y=np.vectorize(y)
> 
> ---> 25 plt.plot(x1,m)
> 26 #plt.show()
> 
> 27 print m
> 
> C:\Python27\lib\site-packages\matplotlib\pyplot.pyc in plot(*args, **kwargs)
>   2456 ax.hold(hold)
>   2457 try:
> -> 2458 ret = ax.plot(*args, **kwargs)
>   2459 draw_if_interactive()
>   2460 finally:
> 
> C:\Python27\lib\site-packages\matplotlib\axes.pyc in plot(self, *args, 
> **kwargs)
>   3846 lines = []
>   3847
> -> 3848 for line in self._get_lines(*args, **kwargs):
>   3849 self.add_line(line)
>   3850 lines.append(line)
> 
> C:\Python27\lib\site-packages\matplotlib\axes.pyc in
> _grab_next_args(self, *args, **kwargs)
>321 return
>322 if len(remaining) <= 3:
> --> 323 for seg in self._plot_args(remaining, kwargs):
>324 yield seg
>325 return
> 
> C:\Python27\lib\site-packages\matplotlib\axes.pyc in _plot_args(self,
> tup, kwargs)
>298 x = np.arange(y.shape[0], dtype=float)
>299
> --> 300 x, y = self._xy_from_xy(x, y)
>301
>302 if self.command == 'plot':
> 
> C:\Python27\lib\site-packages\matplotlib\axes.pyc in _xy_from_xy(self, x, y)
>238 y = np.atleast_1d(y)
>239 if x.shape[0] != y.shape[0]:
> --> 240 raise ValueError("x and y must have same first dimension")
>241 if x.ndim > 2 or y.ndim > 2:
>242 raise ValueError("x and y can be no greater than 2-D")
> 
> ValueError: x and y must have same first dimension

Read the error message: *same (first) dimension*

Then search where in your program the exception occurred:
C:\Users\as\uy.py in ()
23 #plt.axhline(m)

24 #y=np.vectorize(y)

---> 25 plt.plot(x1,m)
26 #plt.show()

27 print m

So, x1 and m don't have the same first dimension.
Try printing x1 and m before this statement (not after, like you did for m); 
just as a debug line.
Then see if you can figure out why m isn't what probably you want it to be.

Good luck,

  Evert



> 
> 
> Question:
> then how to plot those specific component of E within the square well omly?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] repeat a sequence in range

2012-02-12 Thread Peter Otten
Michael Lewis wrote:

> I am trying to repeat a certain sequence in a range if a certain even
> occurs. Forgive me for not pasting my code; but I am not at the machine
> where it's saved.
> 
> Basically, I want to get user input and append that input to a list only
> if the input is not already in the list. I want to do this x amount of
> times, but if the user input is already in the list, then I want to repeat
> that step in range(x).
> 
> At the prompt, I want to ask the user for:
> 
> Name 1:
> Name 2:
> Name 3:
> etc
>  but if the user input for Name 3 was the same as the user input for Name
> 2, then I want to ask the user again for Name 3 instead of continuing to
> Name 4.
> 
> How can I do this?

Well, you are describing the algorithm pretty clearly: keep asking for a 
name until the user enters a name that is not in the list of names already.
Here's a straightforward implementation:

>>> names = []
>>> for i in range(1, 4):
... while True:
... name = raw_input("Name %d: " % i)
... if name not in names:
... names.append(name)
... break
... print "Name %r already taken" % name
...
Name 1: Jack
Name 2: Jack
Name 'Jack' already taken
Name 2: Jack
Name 'Jack' already taken
Name 2: Jim
Name 3: Jack
Name 'Jack' already taken
Name 3: Jim
Name 'Jim' already taken
Name 3: Joe
>>> names
['Jack', 'Jim', 'Joe']


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running Files with Command Lines

2012-02-12 Thread bob gailer

On 2/12/2012 2:21 AM, Yony Torres wrote:

Hello buddies


I'm trying to learn Python from a well known book, and i'm stuck with 
something that i know that might seem surprisingly easy for you and i 
would like to humbly request your help:


i created a script in a file named script1.py and i saved it in the 
folder named python journey located in this path 
c:\users\myusername\documents\varios 2\python journey\


i tested the script1.py file via the GUI and it works fine...BUT!...i 
have been trying to run it via the Python command line and the Windows 
CMD... UNSUCCESSFULLY :(


the instructions given in the book are these as follows:

"Once you've saved this text file, you can ask Python to run it by 
listing its full filename as the first argument to a python command, 
typed at the system shell prompt:"


% python script1.py

"Again, you can type such a system shell command in whatever your 
system provides for command-line entry---a Windows Command Prompt 
window, an xterm window,
or similar. Remember to replace "python" with a full directory path, 
as before, if your PATH setting is not configured."


what i did was this:

I typed all this options:

c:\users\myusername\documents\varios 2\python journey> script1.py

c:\users\myusername\documents\varios 2\python journey\ script1.py

c:\users\myusername\documents\varios 2\python journey\script1.py


what am i doing wrong? can somebody please help me?


Thanks for asking. When you ask for help please tell us which version of 
Python and what OS you are running, and what (if any) errors or 
unexpected results you are getting. The more info you give us the easier 
it is for us to help.


Also be sure to reply-all so a copy goes to the list.

The directions you were given are hard to follow, and a lot depends on 
how your system is configured. Once we see the error you are getting we 
can better help.


--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to stop a running program in python without closing python interface?

2012-02-12 Thread Debashish Saha
actually a i ran a progam in python which is sufficiently large . so i
want to stop the execution of the program now . how can i do this?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to stop a running program in python without closing python interface?

2012-02-12 Thread David Smith

On 12 Feb 2012, at 11:54, Debashish Saha wrote:

> actually a i ran a progam in python which is sufficiently large . so i
> want to stop the execution of the program now . how can i do this?

This will depend on your operating system.

On a Mac you press alt + command + esc and the choose the program you want to 
Force Quit. I have no idea what you do on Windows or Unix.

Dax

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to print a string in desired colour

2012-02-12 Thread Debashish Saha
suppose i want to print 'hello world' in color blue.so what to do?


i tried
print 'hello world','blue'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to stop a running program in python without closing python interface?

2012-02-12 Thread Evert Rol
>> actually a i ran a progam in python which is sufficiently large . so i
>> want to stop the execution of the program now . how can i do this?
> 
> This will depend on your operating system.
> 
> On a Mac you press alt + command + esc and the choose the program you want to 
> Force Quit. I have no idea what you do on Windows or Unix.

Going by the subject line, and assuming you're running the python shell (I 
don't know about Python IDEs etc), you may want to try control-C instead. That 
will stop the currently running function or similar, while leaving you in the 
shell:

>>> while True:
...   pass
... 
^CTraceback (most recent call last):
  File "", line 2, in 
KeyboardInterrupt
>>> 


  Evert

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to print a string in desired colour

2012-02-12 Thread Scott Nelson
On Sun, Feb 12, 2012 at 6:47 AM, Debashish Saha  wrote:

> suppose i want to print 'hello world' in color blue.so what to do?
>
>
There was a similar thread awhile ago.  Unfortunately the answer isn't an
easy one.  It depends on what operating system you use.  Here's a link to
the old thread: http://comments.gmane.org/gmane.comp.python.tutor/67743
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] string integers?

2012-02-12 Thread William Stewart
I am trying to get 2 string variables and 2 integer variables to be able to be 
multiplied
can anyone tell me what I did wrong
 
str1 = raw_input("Type in a String: ")
str2 = raw_input("Type in a String: ")
int1 = raw_input("Type in a integer variable: ")
int2 = raw_input("Type in a integer variable: ")
print str1 + str2 + int1 + int2
import math 
print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 * int 2
 
 

and it wont let me write int2
I know this may look stupid to most people  but I am just new at this so dont 
laugh  :)___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to print a string in desired colour

2012-02-12 Thread bob gailer

Please always tell us which OS you are using, which version of Python.

When something you try does not give you the result you want, please 
tell us what you got.


In a case like this also tell us where you want the colored text to 
appear. It may be obvious to you but it is not to us.


Given how long you've been on this list I'd think you'd know all that by 
now.


On 2/12/2012 7:47 AM, Debashish Saha wrote:

suppose i want to print 'hello world' in color blue.so what to do?

Ths stupid answer is "take a blue marker and write 'hello world'"


i tried
print 'hello world','blue'


Where did you get the idea that that would do what you want? Did you 
read the documentation regarding print? Please do not post stuff that is 
obviously wrong!


No one can tell you how to create colored output until we know where you 
are trying to get it to display.



--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string integers?

2012-02-12 Thread Hugo Arts
On Sun, Feb 12, 2012 at 2:25 PM, William Stewart  wrote:

> I am trying to get 2 string variables and 2 integer variables to be able
> to be multiplied
> can anyone tell me what I did wrong
>
> str1 = raw_input("Type in a String: ")
> str2 = raw_input("Type in a String: ")
> int1 = raw_input("Type in a integer variable: ")
> int2 = raw_input("Type in a integer variable: ")
> print str1 + str2 + int1 + int2
> import math
> print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 * int
> 2
>
>
>
> and it wont let me write int2
> I know this may look stupid to most people  but I am just new at this so
> dont laugh  :)
>
>
No worries, this list is aimed toward new people. No one asking questions
here looks stupid.

Now, when you post to places like this, make sure you mention
1) what you thought would happen or what you're trying to do (you've got
this one decently covered)
2) what happened instead (if an error message happened, paste it!!! It's
almost always useful to us).

Your first problem is the commas in the print statement. You have commas in
places where they shouldn't be, and no commas in places where they should.
I'm not gonna give everything away, but look real close at it again. If you
have to put a lot of variables inside a string, all these commas do end up
being fairly confusing. You might want to have a look at the format method,
I'll give you a sample:

print "{0} * {1} * {2} * {3} = {4}".format(str1, str2, int1, int2, str1 *
str2 * int1 * int2)

The curly brackets are replaced by an argument of the format call. The
number indicates which argument. This way, you don't get commas all over
the place and you keep things nice and organized. You should also know that
you really can't multiply strings. You'll get a TypeError if you try.

HTH,
Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string integers?

2012-02-12 Thread Brian van den Broek
On 12 Feb 2012 15:28, "William Stewart"  wrote:
>
> I am trying to get 2 string variables and 2 integer variables to be able
to be multiplied
> can anyone tell me what I did wrong
>
> str1 = raw_input("Type in a String: ")
> str2 = raw_input("Type in a String: ")
> int1 = raw_input("Type in a integer variable: ")
> int2 = raw_input("Type in a integer variable: ")
> print str1 + str2 + int1 + int2
> import math
> print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 *
int 2
>
>
>
> and it wont let me write int2
> I know this may look stupid to most people  but I am just new at this so
dont laugh  :)
>

Hi,

It is a bit unclear what you mean by "it wont let me write int2".

Try running this function and see if it helps:

def test():
data = raw_input("give me an integer")
print type(data)
print "a string" * "another"

Best,

Brian vdB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to print a string in desired colour

2012-02-12 Thread Devin Jeanpierre
On Sun, Feb 12, 2012 at 8:02 AM, Scott Nelson  wrote:
> On Sun, Feb 12, 2012 at 6:47 AM, Debashish Saha  wrote:
>>
>> suppose i want to print 'hello world' in color blue.so what to do?
>>
>
> There was a similar thread awhile ago.  Unfortunately the answer isn't an
> easy one.  It depends on what operating system you use.  Here's a link to
> the old thread: http://comments.gmane.org/gmane.comp.python.tutor/67743

Colorama works on every major desktop platform.

http://pypi.python.org/pypi/colorama

-- Devin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Same code has different result

2012-02-12 Thread daedae11
The code is:
from nntplib import NNTP
s = NNTP('news.gmane.org')
resp, count, first, last, name = s.group('gmane.comp.python.committers')
print 'Group', name, 'has', count, 'articles, range', first, 'to', last
resp, subs = s.xhdr('subject', first + '-' + last)
for id, sub in subs[-10:]:
print id, sub
s.quit()

When I write it into a script, it can execute normally. However, when I input 
it in interpreter line by line, I got the follow error when I execute the third 
sentence. What's the matter?

>>> s = NNTP('news.gmane.org')
>>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
Traceback (most recent call last):
  File "", line 1, in 
  File "D:\Python27\lib\nntplib.py", line 345, in group
resp = self.shortcmd('GROUP ' + name)
  File "D:\Python27\lib\nntplib.py", line 259, in shortcmd
return self.getresp()
  File "D:\Python27\lib\nntplib.py", line 214, in getresp
resp = self.getline()
  File "D:\Python27\lib\nntplib.py", line 206, in getline
if not line: raise EOFError
EOFError






daedae11___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise with classes 2nd attempt

2012-02-12 Thread Brian van den Broek
On 12 Feb 2012 05:23, "Tonu Mikk"  wrote:
>
> I am learning Python using the "Learn Python the Hard Way" book by Zed
Shaw.  I reached exercise 42 where we learn about Python classes.  The
exercise shows a game with one class that includes all the definitions for
playing the game.  For extra credit we are asked to create another version
of this game where we use two classes - one for the room definitions and
the other for the playing the game.
>
> May attempt at creating two classes is here http://codepad.org/963vUgSt .
 I get the following error which I have been un-able to resolve.  Any
suggestions are welcome.
>
> Traceback (most recent call last):
>   File "ex42_3.py", line 233, in 
> run()
>   File "ex42_3.py", line 231, in run
> room1.doIt()  # plays the game
>   File "ex42_3.py", line 32, in doIt
> self.obj.play() # use object
>   File "ex42_3.py", line 20, in play
> room = getattr(self, next)
> AttributeError: 'Engine' object has no attribute 'central_corridor'

Hi,

Your code is longer than I feel like reading carefully. (Not a complaint;
just cautioning you about how closely I looked.) Also, the line numbers of
your code sample do not agree with those of your traceback. (That is a mild
complaint; it makes it harder to help you.)

Notice that you defined central_corridor as a method of Room. The last line
of your traceback is (it seems) in Engine.play; the code there looks for
central_corridor in Engine and doesn't find it.

If that help, great. If not, try to trim down your code to a smaller
version that exhibits the problem and post again, this time making sure the
posted code and the code that generate the traceback are the same.

Best,

Brian vdB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string integers?

2012-02-12 Thread Dave Angel

On 02/12/2012 08:25 AM, William Stewart wrote:

I am trying to get 2 string variables and 2 integer variables to be able to be 
multiplied
can anyone tell me what I did wrong
  
str1 = raw_input("Type in a String: ")

str2 = raw_input("Type in a String: ")
int1 = raw_input("Type in a integer variable: ")
int2 = raw_input("Type in a integer variable: ")
print str1 + str2 + int1 + int2
import math
print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 * int 2
  
  


and it wont let me write int2
I know this may look stupid to most people  but I am just new at this so dont 
laugh  :)



That's who this list is targeted at, people who are just learning 
Python.  Are you new to programming, or just to Python?  Anyway, welcome 
to Python-Tutor list.


If these 7 lines are in a file, and you try to run them, you get a 
specific error, pointing to a specific line.  When asking questions 
about Python error messages, please post the actual message, as it 
generally contains lots of clues as to what's wrong.


davea@think:~/temppython$ python william.py
  File "william.py", line 7
print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * 
int1 * int 2

^
SyntaxError: invalid syntax

So now we both know the error is a syntax error, and it occurs on line 
7, which is conveniently redisplayed with a caret pointing at where the 
error was discovered (notice that in many email systems a proportional 
font may hide the correct column.  So trust what you saw on your own 
terminal window).  Sometimes the actual syntax error is a few characters 
earlier, but this is as fine-tuned as most compilers can manage.


That print line has 4 errors that I can immediately spot, and the 
compiler told you about the first one.  That error was in omitting the 
comma  before the first occurrence of the variable int2.  You butted a 
string literal right up to a variable name, with no operator between.


My usual advice when seeing a beginner with a complex line that gives a 
syntax error is to see if you can replace it by a few simpler lines.  
Then the error might be more obvious.  So use several print lines.  So 
what if the output isn't quite right;  you have some more debugging to 
do before you'll even see that output.



Now let me ask you, how is str1 any different from int1 ?  Python does 
not have typed variables, and it certainly pays no attention to the 
spelling of a name to guess what it's intended to hold.  The same name 
str1 can hold a string one time, an integer another time, a list yet 
another.  The only way you're going to get those 3rd and 4th lines to 
make integer objects is to convert the string that raw_input() returns 
into an integer.  So use

 int1 = int(raw_input("x"))

The next problem will probably be easier for you to spot, but if not, 
remember to post the full error message, not some summary of it.






--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Same code has different result

2012-02-12 Thread Andreas Perstinger
On Sun, 12 Feb 2012 21:31:57 +0800
daedae11  wrote:

> The code is:
> from nntplib import NNTP
> s = NNTP('news.gmane.org')
> resp, count, first, last, name = s.group
> ('gmane.comp.python.committers') print 'Group', name, 'has', count,
> 'articles, range', first, 'to', last resp, subs = s.xhdr('subject',
> first + '-' + last) for id, sub in subs[-10:]:
> print id, sub
> s.quit()
> 
> When I write it into a script, it can execute normally. However, when
> I input it in interpreter line by line, I got the follow error when I
> execute the third sentence. What's the matter?

It seems that the connection is closed if you need more than about 6-7 seconds 
(on my computer) to type the third line.

>From the source of "nntplib.py":

def getline(self):
"""Internal: return one line from the server, stripping CRLF.
Raise EOFError if the connection is closed."""

Bye, Andreas
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string integers?

2012-02-12 Thread Mark Lawrence

On 12/02/2012 14:51, Dave Angel wrote:

On 02/12/2012 08:25 AM, William Stewart wrote:


[snipped]


My usual advice when seeing a beginner with a complex line that gives a
syntax error is to see if you can replace it by a few simpler lines.
Then the error might be more obvious. So use several print lines. So
what if the output isn't quite right; you have some more debugging to do
before you'll even see that output.



For the OP.

print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 * 
int 2


To print all of this on one line you can use
print str1,
print "*",
etc

See http://docs.python.org/tutorial/inputoutput.html

--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running Files with Command Lines

2012-02-12 Thread Yony Torres

hello there :) ima try it one more time, ima pasting the previous email content 
i sent previously so here it goes ;P
I'm working in a windows vista home premium system
i installed python 3.2.2
I'm trying to learn Python from a well known book, and i'm stuck with 
> something that i know that might seem surprisingly easy for you and i 
> would like to humbly request your help: 
> 
> i created a script in a file named script1.py and i saved it in the 
> folder named python journey located in this path 
> c:\users\myusername\documents\varios2\pythonjourney\  (as you can see 
> i suppressed the spaces in the folder's names)
> 
> i tested the script1.py file via the GUI and it works fine...BUT!...i 
> have been trying to run it via the Python command line and the Windows 
> CMD... UNSUCCESSFULLY :( 
> 
> the instructions given in the book are these as follows: 
> 
> "Once you’ve saved this text file, you can ask Python to run it by 
> listing its full filename as the first argument to a python command, 
> typed at the system shell prompt:" 
> 
> % python script1.py 
> 
> "Again, you can type such a system shell command in whatever your 
> system provides for command-line entry—a Windows Command Prompt window, 
> an xterm window, 
> or similar. Remember to replace “python” with a full directory path, as 
> before, if your PATH setting is not configured." 
> 
> what i did was this: 

while i was working with the windows CMD
i made sure i was working on python by entering this "code"
cd c:\python32pyhton
---so, it successfully showed me this
c:\Python32>pythonPython 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 
bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more 
information.>>>
then, i tried this:
>>> c:\Users\myusername\Documents\varios2\pythonjourney\ script1.py

(please note that i put no indentation), i pressed enter and it returned 
this:

  File "", line 1    
c:\Users\myusername\Documents\varios2\pythonjourney\ script1.py     
^SyntaxError: invalid syntax
then i tried what's next:
>>> c:\Users\myusername\Documents\varios2\pythonjourney> script1.pyand 
>>> returned this:

  File "", line 1    c:\Users\Talmidim\Documents\varios2\pythonjourney> 
script1.py     ^SyntaxError: invalid syntax
my guess was that is something wrong with writing the the ":", so i tried:
>>> \Users\Talmidim\Documents\varios2\pythonjourney> script1.pyand it returned 
>>> this:

  File "", line 1    \Users\myusername\Documents\varios2\pythonjourney> 
script1.py                                                              
^SyntaxError: unexpected character after line continuation character


Can somebody please help me?

thanks in advance!!! ;)

 

> Date: Sun, 12 Feb 2012 06:06:22 -0500 
> From: bgai...@gmail.com 
> To: talmi...@live.com 
> CC: tutor@python.org 
> Subject: Re: [Tutor] Running Files with Command Lines 
>  
> On 2/12/2012 2:21 AM, Yony Torres wrote: 
> Hello buddies 
>  
>  
> I'm trying to learn Python from a well known book, and i'm stuck with  
> something that i know that might seem surprisingly easy for you and i  
> would like to humbly request your help: 
>  
> i created a script in a file named script1.py and i saved it in the  
> folder named python journey located in this path  
> c:\users\myusername\documents\varios 2\python journey\ 
>  
> i tested the script1.py file via the GUI and it works fine...BUT!...i  
> have been trying to run it via the Python command line and the Windows  
> CMD... UNSUCCESSFULLY :( 
>  
> the instructions given in the book are these as follows: 
>  
> "Once you’ve saved this text file, you can ask Python to run it by  
> listing its full filename as the first argument to a python command,  
> typed at the system shell prompt:" 
>  
> % python script1.py 
>  
> "Again, you can type such a system shell command in whatever your  
> system provides for command-line entry—a Windows Command Prompt window,  
> an xterm window, 
> or similar. Remember to replace “python” with a full directory path, as  
> before, if your PATH setting is not configured." 
>  
> what i did was this: 
>  
> I typed all this options: 
>  
> c:\users\myusername\documents\varios 2\python journey> script1.py 
>  
> c:\users\myusername\documents\varios 2\python journey\ script1.py 
>  
> c:\users\myusername\documents\varios 2\python journey\script1.py 
>  
>  
> what am i doing wrong? can somebody please help me? 
>  
> Thanks for asking. When you ask for help please tell us which version  
> of Python and what OS you are running, and what (if any) errors or  
> unexpected results you are getting. The more info you give us the  
> easier it is for us to help. 
>  
> Also be sure to reply-all so a copy goes to the list. 
>  
> The directions you were given are hard to follow, and a lot depends on  
> how your system is configured. Once we see the error you are getting we  
> can better help. 
>  
>  
> --  
> Bob Gailer 
> 91

Re: [Tutor] Running Files with Command Lines

2012-02-12 Thread Alan Gauld

On 12/02/12 15:50, Yony Torres wrote:


I'm trying to learn Python from a well known book,



Do0n;t make us guess, tell us the name of the book, well known or not. 
There is just a chance somebody else may have read it too!



while i was working with the windows CMD
c:\Python32>python


This is where you are going wrong.
You should not run python on its own, that gets you into the Python 
interpreter with its own >>> prompt. For this exercise you want to run 
your script directly from the OS prompt (on Windows usually ending in a 
single >)



Can somebody please help me?


See the other posted replies about typing the command.
It will likely look like this(but all on a single line)

C:\Python32> python 
c:\Users\myusername\Documents\varios2\pythonjourney\script1.py


Don't put spaces in your paths, that will confuse Windows...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running Files with Command Lines

2012-02-12 Thread Yony Torres

1. i tried this morning and it worked in the CMD check it out:

copied and pasted from the CMD
C:\Users\myusername>cd documents
C:\Users\myusername\Documents>cd varios2
C:\Users\myusername\Documents\varios2>cd pythonjourney
C:\Users\myusername\Documents\varios2\pythonjourney>script.py    <--- i did 
not write the name the file properly, my mistake!'script.py' is not recognized 
as an internal or external command,operable program or batch file.
C:\Users\myusername\Documents\varios2\pythonjourney>script1.py    <- now i 
did it 
properlywin321267650600228229401496703205376spam!spam!spam!spam!spam!spam!spam!spam!
--- i must say that previously i added C:\python32 to the environment path ;)
yay!
now im trying it from the python command line, with no success yet :(



> To: tutor@python.org
> From: alan.ga...@btinternet.com
> Date: Sun, 12 Feb 2012 16:16:47 +
> Subject: Re: [Tutor] Running Files with Command Lines
>
> On 12/02/12 15:50, Yony Torres wrote:
>
> > I'm trying to learn Python from a well known book,
>
>
> Do0n;t make us guess, tell us the name of the book, well known or not.
> There is just a chance somebody else may have read it too!
>
> > while i was working with the windows CMD
> > c:\Python32>python
>
> This is where you are going wrong.
> You should not run python on its own, that gets you into the Python
> interpreter with its own >>> prompt. For this exercise you want to run
> your script directly from the OS prompt (on Windows usually ending in a
> single >)
>
> > Can somebody please help me?
>
> See the other posted replies about typing the command.
> It will likely look like this(but all on a single line)
>
> C:\Python32> python
> c:\Users\myusername\Documents\varios2\pythonjourney\script1.py
>
> Don't put spaces in your paths, that will confuse Windows...
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Same code has different result

2012-02-12 Thread Andreas Perstinger
[You've forgot to include the list in your reply]

On Mon, 13 Feb 2012 00:04:54 +0800 daedae11  wrote:

> Sorry, I'm not sure I know your viewpoint. Could you give me a
> detailed explanation about "you need more than about 6-7 seconds (on
> my computer) to type the third line."? Thank you very much.

The comment in "nntplib.py" says that if the connection is closed, an 
"EOFError" will be raised (that's the error you get).

In the interpreter you type in first the line "s = NNTP('news.gmane.org')" 
which opens the connection. Then you type in the "s.group"-line which is rather 
long and you are probably not typing fast enough. Meanwhile the connection to 
the gmane-Server is closed and that's why you get the "EOFError".

In your script there is no problem because there is no delay between those two 
lines.

Try to copy the lines of your script into your interpreter shell and you 
shouldn't get the error (don't type them manually, use copy & paste!).

HTH, Andreas
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running Files with Command Lines

2012-02-12 Thread Yony Torres


oh i forgot this:
The book i bought and which im reading from is Learning Python - Mark Lutz - 
O'reilly


> From: talmi...@live.com
> To: alan.ga...@btinternet.com; tutor@python.org
> Date: Sun, 12 Feb 2012 11:57:57 -0500
> Subject: Re: [Tutor] Running Files with Command Lines
>
>
> 1. i tried this morning and it worked in the CMD check it out:
>
> copied and pasted from the CMD
> C:\Users\myusername>cd documents
> C:\Users\myusername\Documents>cd varios2
> C:\Users\myusername\Documents\varios2>cd pythonjourney
> C:\Users\myusername\Documents\varios2\pythonjourney>script.py<--- i 
> did not write the name the file properly, my mistake!'script.py' is not 
> recognized as an internal or external command,operable program or batch file.
> C:\Users\myusername\Documents\varios2\pythonjourney>script1.py<- now 
> i did it 
> properlywin321267650600228229401496703205376spam!spam!spam!spam!spam!spam!spam!spam!
> --- i must say that previously i added C:\python32 to the environment path ;)
> yay!
> now im trying it from the python command line, with no success yet :(
>
>
> 
> > To: tutor@python.org
> > From: alan.ga...@btinternet.com
> > Date: Sun, 12 Feb 2012 16:16:47 +
> > Subject: Re: [Tutor] Running Files with Command Lines
> >
> > On 12/02/12 15:50, Yony Torres wrote:
> >
> > > I'm trying to learn Python from a well known book,
> >
> >
> > Do0n;t make us guess, tell us the name of the book, well known or not.
> > There is just a chance somebody else may have read it too!
> >
> > > while i was working with the windows CMD
> > > c:\Python32>python
> >
> > This is where you are going wrong.
> > You should not run python on its own, that gets you into the Python
> > interpreter with its own >>> prompt. For this exercise you want to run
> > your script directly from the OS prompt (on Windows usually ending in a
> > single >)
> >
> > > Can somebody please help me?
> >
> > See the other posted replies about typing the command.
> > It will likely look like this(but all on a single line)
> >
> > C:\Python32> python
> > c:\Users\myusername\Documents\varios2\pythonjourney\script1.py
> >
> > Don't put spaces in your paths, that will confuse Windows...
> >
> > --
> > Alan G
> > Author of the Learn to Program web site
> > http://www.alan-g.me.uk/
> >
> > ___
> > Tutor maillist - Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
>
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Same code has different result

2012-02-12 Thread Dave Angel

On 02/12/2012 12:02 PM, Andreas Perstinger wrote:

[You've forgot to include the list in your reply]

On Mon, 13 Feb 2012 00:04:54 +0800 daedae11  wrote:


Sorry, I'm not sure I know your viewpoint. Could you give me a
detailed explanation about "you need more than about 6-7 seconds (on
my computer) to type the third line."? Thank you very much.

The comment in "nntplib.py" says that if the connection is closed, an 
"EOFError" will be raised (that's the error you get).

In the interpreter you type in first the line "s = NNTP('news.gmane.org')" which opens the 
connection. Then you type in the "s.group"-line which is rather long and you are probably not 
typing fast enough. Meanwhile the connection to the gmane-Server is closed and that's why you get the 
"EOFError".

In your script there is no problem because there is no delay between those two 
lines.

Try to copy the lines of your script into your interpreter shell and you shouldn't 
get the error (don't type them manually, use copy&  paste!).

HTH, Andreas




Or write them in a function, so it doesn't get run at all till you call it.




--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running Files with Command Lines

2012-02-12 Thread Dave Angel

On 02/12/2012 11:57 AM, Yony Torres wrote:

1. i tried this morning and it worked in the CMD check it out:

copied and pasted from the CMD
C:\Users\myusername>cd documents
C:\Users\myusername\Documents>cd varios2
C:\Users\myusername\Documents\varios2>cd pythonjourney
C:\Users\myusername\Documents\varios2\pythonjourney>script.py<--- i did not 
write the name the file properly, my mistake!'script.py' is not recognized as an 
internal or external command,operable program or batch file.
C:\Users\myusername\Documents\varios2\pythonjourney>script1.py<- now i did 
it 
properlywin321267650600228229401496703205376spam!spam!spam!spam!spam!spam!spam!spam!
--- i must say that previously i added C:\python32 to the environment path ;)
yay!
now im trying it from the python command line, with no success yet :(


Still some things you haven't caught onto yet.

1) please don't top-post

2) Please do use copy/paste, rather than laboriously (and inaccurately) 
retyping what you did & saw


3) Use more definite wording than

with no success yet


4) the syntax inside the python interpreter is different than in a 
Windows cmd prompt.  Once you get that prompt, you can type simple 
expressions like  3*4, or you can define and execute functions.  But if 
you want to "run" another script, that script needs to  be a valid module,

 and you import it with the   "import" statement.

So if you want to start python, and then run your script/module from the 
python prompt, you'd do something like:


davea@think:~/temppython$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myscript
Traceback (most recent call last):
  File "", line 1, in 
  File "myscript.py", line 16
assert w = -1
 ^
SyntaxError: invalid syntax
>>>

Note that a number of things are significant, and different than the 
command line:


1) the script must be in the current directory, or it must be in the 
sys.path string, which you probably haven't learned about yet.
2) you import the script by its module name, which does not include the 
.py   In fact, other extensions are possible and common.
3) when the module exits to the python prompt, any variables it defined 
are in the module's space, not the "global" space.  So if you defined a 
variable w, you'd reference it by myscript.w




--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Concatenating multiple lines into one

2012-02-12 Thread Spyros Charonis
Thanks for all the help, Peter's and Hugo's methods worked well in
concatenating multiple lines into a single data structure!

S

On Fri, Feb 10, 2012 at 5:30 PM, Mark Lawrence wrote:

> On 10/02/2012 17:08, Peter Otten wrote:
>
>> Spyros Charonis wrote:
>>
>>  Dear python community,
>>>
>>> I have a file where I store sequences that each have a header. The
>>> structure of the file is as such:
>>>
>>>  sp|(some code) =>1st header

>>> AGGCGG
>>> MNKPLOI
>>> .
>>> .
>>>
>>>  sp|(some code) =>  2nd header

>>> AA
>>>  ...
>>> .
>>>
>>> ..
>>>
>>> I am looking to implement a logical structure that would allow me to
>>> group
>>> each of the sequences (spread on multiple lines) into a single string. So
>>> instead of having the letters spread on multiple lines I would be able to
>>> have 'AGGCGGMNKP' as a single string that could be indexed.
>>>
>>> This snipped is good for isolating the sequences (=stripping headers and
>>> skipping blank lines) but how could I concatenate each sequence in order
>>> to get one string per sequence?
>>>
>>>  for line in align_file:
>>
> ... if line.startswith('>sp'):
>>> ... continue
>>> ... elif not line.strip():
>>> ... continue
>>> ... else:
>>> ... print line
>>>
>>> (... is just OS X terminal notation, nothing programmatic)
>>>
>>> Many thanks in advance.
>>>
>>
>> Instead of printing the line directly collect it in a list (without
>> trailing
>> "\n"). When you encounter a line starting with">sp" check if that list is
>> non-empty, and if so print "".join(parts), assuming the list is called
>> parts, and start with a fresh list. Don't forget to print any leftover
>> data
>> in the list once the for loop has terminated.
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>>
> The advice from Peter is sound if the strings could grow very large but
> you can simply concatenate the parts if they are not.  For the indexing
> simply store your data in a dict.
>
> --
> Cheers.
>
> Mark Lawrence.
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to stop a running program in python without closing python interface?

2012-02-12 Thread Steven D'Aprano

Debashish Saha wrote:

actually a i ran a progam in python which is sufficiently large . so i
want to stop the execution of the program now . how can i do this?


Control-C should work on every operating system. Obviously you have send the 
Ctrl-C to the Python process, not some other application. E.g. if you are 
running your program in a terminal window, bring the terminal window to the 
front so it captures keystrokes and type Ctrl-C.


If you have to kill the Python process from outside, use your operating 
system's standard method for killing processes. E.g. on Windows you would type 
ctrl-alt-del and then select which process to kill from the dialog box. On 
Linux you would use the ps and kill commands.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string integers?

2012-02-12 Thread Brian van den Broek
On 12 February 2012 21:27, William Stewart  wrote:
>
> thanks i tried the code and it doesnt make anydiffference what I need is it 
> to multiply now I fixed the error message but how do I get the 2 numbers that 
> the person enters to multiply
>
> --- On Sun, 2/12/12, Brian van den Broek  
> wrote:
>
>
> From: Brian van den Broek 
> Subject: Re: [Tutor] string integers?
> To: "William Stewart" 
> Cc: tutor@python.org
> Date: Sunday, February 12, 2012, 8:53 AM
>
>
>
> On 12 Feb 2012 15:28, "William Stewart"  wrote:
> >
> > I am trying to get 2 string variables and 2 integer variables to be able to 
> > be multiplied
> > can anyone tell me what I did wrong
> >
> > str1 = raw_input("Type in a String: ")
> > str2 = raw_input("Type in a String: ")
> > int1 = raw_input("Type in a integer variable: ")
> > int2 = raw_input("Type in a integer variable: ")
> > print str1 + str2 + int1 + int2
> > import math
> > print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 * int 2
> >
> >
> >
> > and it wont let me write int2
> > I know this may look stupid to most people  but I am just new at this so 
> > dont laugh  :)
> >
> Hi,
> It is a bit unclear what you mean by "it wont let me write int2".
> Try running this function and see if it helps:
> def test():
>     data = raw_input("give me an integer")
>     print type(data)
>     print "a string" * "another"
> Best,
> Brian vdB


Hi William and list,

To the list:

I have only recently reappeared here on the Tutor list, but years
back, I learned a great deal from various patient people, some of whom
are still here. (Grateful waves to those folk!) I feel fairly
confident that the message below is still in the spirit and cultural
norms of the list. However, if I am wrong in that, I would welcome any
Tutor veterans calling me out, in public (preferred) or in private.

To William:

I have a few observations that, if you take them to heart, will help
you make better use of the Tutor mailing list. They may seem picky,
but I assure you that there are reasons behind each thing I say and
following these observations will give you a much more rewarding
experience with the list.

1) Please don't top post. It bothers geeks and as you want geeks to
help you, even if that preference seems silly (it isn't), grit your
teeth and do as those who you are asking to help you would prefer.
('Geek' is, of course, a term of praise.)

2) Please be sure to hit Reply-to-All in your mail client. If it lacks
such a button, be sure to add a to:tutor@python.org. If you don't your
response will go only to the person to whom you are replying. This is
what happened to your response to me. If I'd lost the time, interest,
or ability to reply to you, your message to me would never get you any
further help. Sent to both me and to the list, you can get help from
others even if help from me is not forthcoming for whatever reason.

3) Please put more effort in to asking your question in a clear
manner. In all honesty, I have no idea what it is you hope "thanks i
tried the code and it doesnt make anydiffference what I need is it to
multiply now I fixed the error message but how do I get the 2 numbers
that the person enters to multiply" to produce by way of further help.
What code? What difference were you expecting? Different from what?
What error message? I am quite sure I spent longer typing up my first
message to you than you did typing your reply. You will generally find
that people here will respond positively to effort you expend to make
your question clear as it makes it easy for them to help you. If you
are not willing to spend much effort, in general, people are not
likely to spend more effort than you are.

To help see the importance of including your code, your output or
backtrace, and a clear statement of your expectations and intentions,
consider what happened with your first message. I said "It is a bit
unclear what you mean by "it wont let me write int2"." I noticed a
problem with your code and, as you'd not been clear about what problem
you were having, I said something about that. I didn't read carefully
enough to see the problems that others pointed out to you. (If you
didn't care to clearly state your problem, I didn't care to work it
out for you.) While your code did have multiple issues (that's fine;
we were all beginners once), the way you asked it made it hard for me
to focus on the issue you were having at the time.

Another benefit of taking the time to compose a clear email with a
clear statement of your problem (including a *copy and paste* of the
smallest chunk of code that exhibits the problem, a description of the
expected output, and a *copy and paste* of the output or generate
traceback) is that very often, the process of generating such a
message will help you solve your own problem. I cannot begin to guess
how many times I've started writing a question to this or some other
technical mailinglist or newsgroup only to find that the process of
asking well forced me to

Re: [Tutor] string integers?

2012-02-12 Thread William Stewart
this is the code I have
 
str1 = raw_input("Type in a String: ")
str2 = raw_input("Type in a String: ")
int1 = raw_input("Type in a integer variable: ")
int2 = raw_input("Type in a integer variable: ")
print str1 + str2 + int1 + int2
import math
int1 = int(raw_input(""))
print str1,
print str2,
print int1, "*", int2
print "="

and it does not give me an error message when I run it, the program runs fine 
except its did not multiply the 2 integer variables i entered
 
it looks like this
 
Type in a String: hello
Type in a String: hi
Type in a integer variable: 4
Type in a integer variable: 5
hellohi45
 
This part is exactly what I want it to look like
except I want it to multply the 2 numbers I inputed (4 & 5 in this example)
 
thanks again for your time and sorry if I seem rude I just dont have alot of 
time these days
I know you all dont have alot of time either and thats why I am extremely 
appreciative of everyones help
 

 


--- On Sun, 2/12/12, Dave Angel  wrote:


From: Dave Angel 
Subject: Re: [Tutor] string integers?
To: "William Stewart" 
Date: Sunday, February 12, 2012, 5:07 PM


On 02/12/2012 02:41 PM, William Stewart wrote:
> hello thank you for your reply

That was a post to the list;  you replied privately,  but I'm going to 
just elaborate my earlier points.  Try again, but use reply-all to one 
of the messages in the thread.

You've got a new version of stuff, but no indication what the code now 
looks like, or what is "not working".

Be explicit when requesting help.  I and others pointed out a few things 
wrong;  there are others. So don't make us guess what state you're in. 
Hopefully you're here to learn, and that happens best when you make 
clear questions, and get good responses.

Show your code, show the error,and use cut&paste for the error you get.
"It's not working" is not an error message.

You either get an error message:  quote the entire traceback
Or it doesn't do what you expect:   tell what you expected, and show 
what you got instead.


> I fixed the error the only problem now is how do i get the 2 spereate 
> integers to multiply? but I still need the 2 strings to print
>
> I tried
>
> print str1,
> print str2,
> print int1, "*", int2
> print "="
>
> I think I am missing something
> I know the * is to multiply but its not working
> thank you
>
>
>
> --- On Sun, 2/12/12, Dave Angel  wrote:
>
>
> From: Dave Angel
> Subject: Re: [Tutor] string integers?
> To: "William Stewart"
> Cc: tutor@python.org
> Date: Sunday, February 12, 2012, 9:51 AM
>
>
> On 02/12/2012 08:25 AM, William Stewart wrote:
>> I am trying to get 2 string variables and 2 integer variables to be able to 
>> be multiplied
>> can anyone tell me what I did wrong
>>     str1 = raw_input("Type in a String: ")
>> str2 = raw_input("Type in a String: ")
>> int1 = raw_input("Type in a integer variable: ")
>> int2 = raw_input("Type in a integer variable: ")
>> print str1 + str2 + int1 + int2
>> import math
>> print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 * int 2
>>
>> and it wont let me write int2
>> I know this may look stupid to most people  but I am just new at this so 
>> dont laugh  :)
>>
>
> That's who this list is targeted at, people who are just learning Python.  
> Are you new to programming, or just to Python?  Anyway, welcome to 
> Python-Tutor list.
>
> If these 7 lines are in a file, and you try to run them, you get a specific 
> error, pointing to a specific line.  When asking questions about Python error 
> messages, please post the actual message, as it generally contains lots of 
> clues as to what's wrong.
>
> davea@think:~/temppython$ python william.py
>    File "william.py", line 7
>      print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 * 
>int 2
>                                              ^
> SyntaxError: invalid syntax
>
> So now we both know the error is a syntax error, and it occurs on line 7, 
> which is conveniently redisplayed with a caret pointing at where the error 
> was discovered (notice that in many email systems a proportional font may 
> hide the correct column.  So trust what you saw on your own terminal 
> window).  Sometimes the actual syntax error is a few characters earlier, but 
> this is as fine-tuned as most compilers can manage.
>
> That print line has 4 errors that I can immediately spot, and the compiler 
> told you about the first one.  That error was in omitting the comma  before 
> the first occurrence of the variable int2.  You butted a string literal right 
> up to a variable name, with no operator between.
>
> My usual advice when seeing a beginner with a complex line that gives a 
> syntax error is to see if you can replace it by a few simpler lines.  Then 
> the error might be more obvious.  So use several print lines.  So what if the 
> output isn't quite right;  you have some more debugging to do before you'll 
> even see that output.
>
>
> Now let me ask you, how is str1 any different from int1 ?  Python does not 
>

Re: [Tutor] string integers?

2012-02-12 Thread Steven D'Aprano

William Stewart wrote:

this is the code I have
 
str1 = raw_input("Type in a String: ")

str2 = raw_input("Type in a String: ")
int1 = raw_input("Type in a integer variable: ")
int2 = raw_input("Type in a integer variable: ")
print str1 + str2 + int1 + int2
import math
int1 = int(raw_input(""))
print str1,
print str2,
print int1, "*", int2
print "="

and it does not give me an error message when I run it, the program runs fine
 except its did not multiply the 2 integer variables i entered



But you haven't asked it to multiply anything. You asked it to PRINT the two 
variables with an asterisk between them.


And further more, you don't have two integers. You have two strings that 
merely happen to contain digits. Before you can do maths on them, you have to 
tell Python to treat them as integers, not strings. You use the int() function 
(int being short for integer, in case it isn't obvious) for that:



py> x = raw_input("Enter a number: ")
Enter a number: 42
py> type(x)

py> x * 10
'42424242424242424242'
py>
py> x + 1
Traceback (most recent call last):
  File "", line 1, in 
TypeError: cannot concatenate 'str' and 'int' objects


Before you can do maths on x, you have to turn it into a number:

py> x = int(x)
py> x * 10
420
py> x + 1
43


P.S. Does your computer have a Delete or Backspace key? Please trim your 
replies, there is no need to quote page after page after page of old 
conversation that you don't directly address.





--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string integers?

2012-02-12 Thread amt
Hello William and welcome to the Python list. I'm a beginner  but I'll
give it a shot.

Problem is, you use raw_input and it returns a string, not an int.
Try this code:

str1 = raw_input("Type in a String: ")
str2 = raw_input("Type in a String: ")
int1 = int(raw_input("Type in a integer variable: "))
int2 = int(raw_input("Type in a integer variable: "))
print "{0}{1}{2}".format(str1, str2, int1*int2)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string integers?

2012-02-12 Thread Brian van den Broek
On 13 February 2012 01:34, William Stewart  wrote:
>
> this is the code I have
>
> str1 = raw_input("Type in a String: ")
> str2 = raw_input("Type in a String: ")
> int1 = raw_input("Type in a integer variable: ")
> int2 = raw_input("Type in a integer variable: ")
> print str1 + str2 + int1 + int2
> import math
> int1 = int(raw_input(""))
> print str1,
> print str2,
> print int1, "*", int2
> print "="
> and it does not give me an error message when I run it, the program runs fine 
> except its did not multiply the 2 integer variables i entered
>
> it looks like this
>
> Type in a String: hello
> Type in a String: hi
> Type in a integer variable: 4
> Type in a integer variable: 5
> hellohi45
>
> This part is exactly what I want it to look like
> except I want it to multply the 2 numbers I inputed (4 & 5 in this example)
>


Hi William,

That is a much better starting point from which to get help. It looks
to me as though you took the responses concerning form that I and
other gave you seriously; I'm glad. (I sympathize about the difficulty
to find time to ask well. It does, however, take less time in the long
run than asking several rounds of quick to compose questions.)

Steven D'Aprano has given you enough that you should be able to make
progress and ask again if needed. I did, however, want to point out
that in my first message to you, when I suggested a function for you
to run, it was with an eye to helping you to discover the problem.
Here's the function and the results of running it in idle:

IDLE 2.6.6
>>> def test():
data = raw_input("give me an integer")
print type(data)
print "a string" * "another"


>>> test()
give me an integer42


Traceback (most recent call last):
  File "", line 1, in 
test()
  File "", line 4, in test
print "a string" * "another"
TypeError: can't multiply sequence by non-int of type 'str'
>>>

(Here, 42 is my input.) You can see that the type of data (the value
returned by the raw_input call) is str---a string. The TypeError is
telling you that the code I gave tries to multiply by a string and
that caused a TypeError as multiplication isn't an operation defined
for strings as the right-hand multiplier. Steven's email shows you how
to surmount that problem; you must use int() to turn the returned
value of raw_input into an integer. Compare:

>>> def test2():
data = int(raw_input("give me an integer"))
print type(data)
print data * data


>>> test2()
give me an integer4

16

of course, there are still things that can go wrong:

>>> test2()
give me an integer3.1415

Traceback (most recent call last):
  File "", line 1, in 
test2()
  File "", line 2, in test2
data = int(raw_input("give me an integer"))
ValueError: invalid literal for int() with base 10: '3.1415'
>>> test2()
give me an integerFourtyTwo

Traceback (most recent call last):
  File "", line 1, in 
test2()
  File "", line 2, in test2
data = int(raw_input("give me an integer"))
ValueError: invalid literal for int() with base 10: 'FourtyTwo'
>>>

In both cases, I entered some string that int() cannot turn into an integer.

If you get the basic idea working for the case where the user enters
sane values, we can talk about how to deal with such cases if need be.

Best,

Brian vdB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string integers?

2012-02-12 Thread William Stewart
Thank you for the reply It worked fine !

--- On Sun, 2/12/12, Steven D'Aprano  wrote:


From: Steven D'Aprano 
Subject: Re: [Tutor] string integers?
To: tutor@python.org
Date: Sunday, February 12, 2012, 6:50 PM


William Stewart wrote:
> this is the code I have
>  str1 = raw_input("Type in a String: ")
> str2 = raw_input("Type in a String: ")
> int1 = raw_input("Type in a integer variable: ")
> int2 = raw_input("Type in a integer variable: ")
> print str1 + str2 + int1 + int2
> import math
> int1 = int(raw_input(""))
> print str1,
> print str2,
> print int1, "*", int2
> print "="
> 
> and it does not give me an error message when I run it, the program runs fine
>  except its did not multiply the 2 integer variables i entered


But you haven't asked it to multiply anything. You asked it to PRINT the two 
variables with an asterisk between them.

And further more, you don't have two integers. You have two strings that merely 
happen to contain digits. Before you can do maths on them, you have to tell 
Python to treat them as integers, not strings. You use the int() function (int 
being short for integer, in case it isn't obvious) for that:


py> x = raw_input("Enter a number: ")
Enter a number: 42
py> type(x)

py> x * 10
'42424242424242424242'
py>
py> x + 1
Traceback (most recent call last):
  File "", line 1, in 
TypeError: cannot concatenate 'str' and 'int' objects


Before you can do maths on x, you have to turn it into a number:

py> x = int(x)
py> x * 10
420
py> x + 1
43


P.S. Does your computer have a Delete or Backspace key? Please trim your 
replies, there is no need to quote page after page after page of old 
conversation that you don't directly address.




-- Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] tabbed output

2012-02-12 Thread Michael Lewis
I am having a weird issue. I have a print statement that will give me
multiple outputs separated by a tab; however, sometimes there is a tab
between the output and sometimes there is not. It seems sort of sporadic.
My code is below and two sample outputs are below that (one that works how
I expect, and the other showing the issue (tab separation not occurring
between all pieces of the output) What is going on?

def CheckNames():
names = []
for loop in range(1,4):
while True:
name = raw_input('''Name #%s: ''' %(loop))
if name not in names:
names.append(name)
break
print '%s is already in the data. Try again.' %(name)
sorted_names = sorted(names)
for element in list(sorted_names):
print 'Hurray for %s!\t' %(element),

Sample Output1:

Name #1: mike
Name #2: bret
Name #3: adam
Hurray for adam!Hurray for bret!   Hurray for mike!

Sample Output2(there is a tab between output 2 & 3, but not 1 & 2):

Name #1: abe
Name #2: alan
Name #3: adam
Hurray for abe! Hurray for adam!  Hurray for alan!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to stop a running program in python without closing python interface?

2012-02-12 Thread Mark Lawrence

On 12/02/2012 22:19, Steven D'Aprano wrote:

Debashish Saha wrote:

actually a i ran a progam in python which is sufficiently large . so i
want to stop the execution of the program now . how can i do this?


Control-C should work on every operating system. Obviously you have send
the Ctrl-C to the Python process, not some other application. E.g. if
you are running your program in a terminal window, bring the terminal
window to the front so it captures keystrokes and type Ctrl-C.

If you have to kill the Python process from outside, use your operating
system's standard method for killing processes. E.g. on Windows you
would type ctrl-alt-del and then select which process to kill from the
dialog box. On Linux you would use the ps and kill commands.




At least on Windows Vista ctrl-alt-del brings up a list of options, one 
of which lets you bring up the Task Manager to kill the process and/or 
application.  ctrl-shift-esc brings up the Task Manager directly.


--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tabbed output

2012-02-12 Thread Hugo Arts
On Mon, Feb 13, 2012 at 1:52 AM, Michael Lewis  wrote:
> I am having a weird issue. I have a print statement that will give me
> multiple outputs separated by a tab; however, sometimes there is a tab
> between the output and sometimes there is not. It seems sort of sporadic. My
> code is below and two sample outputs are below that (one that works how I
> expect, and the other showing the issue (tab separation not occurring
> between all pieces of the output) What is going on?
>
> def CheckNames():
>     names = []
>     for loop in range(1,4):
>         while True:
>             name = raw_input('''Name #%s: ''' %(loop))
>             if name not in names:
>                 names.append(name)
>                 break
>             print '%s is already in the data. Try again.' %(name)
>     sorted_names = sorted(names)
>     for element in list(sorted_names):
>         print 'Hurray for %s!\t' %(element),
>
> Sample Output1:
>
> Name #1: mike
> Name #2: bret
> Name #3: adam
> Hurray for adam!            Hurray for bret!   Hurray for mike!
>
> Sample Output2(there is a tab between output 2 & 3, but not 1 & 2):
>

Yes there is, and I'll show you by modifying your method just a little:

>>> def CheckNames():
names = []
for loop in range(1,4):
while True:
name = raw_input('''Name #%s: ''' %(loop))
if name not in names:
names.append(name)
break
print '%s is already in the data. Try again.' %(name)
sorted_names = sorted(names)
lst = []
for element in list(sorted_names):
lst.append('Hurray for %s!\t' %(element))
return ''.join(lst)

The juicy bit is at the end. Instead of printing, we make a list,
append everything we were going to print to it, join it into one big
string, and return it. This way we can look at the string better. Now
for the demonstration:

>>> CheckNames()
Name #1: abe
Name #2: alan
Name #3: adam
'Hurray for abe!\tHurray for adam!\tHurray for alan!\t'
>>> print _   # btw, _ is short for last value in the interpreter
Hurray for abe! Hurray for adam!Hurray for alan!
>>>

You see that? in the string returned, there is most definitely a tab.
And in fact, that little space between abe and adam, that is also a
tab. You see, if you insert a tab, the cursor is moved up to the next
tab stop. Choosing a short name #1, like abe, means that the next tab
stop is right after the exclamation mark. If you use a slightly longer
name though, like adam, the exclamation mark will be past that tab
stop, and the tab character afterward will put name #2 all the way at
the next tabstop.

tab characters are lame like that. They are generally only used to
make sure output lines up at a tabstop, it's not a reliable way to put
a certain amount of space between two pieces of text.

HTH,
Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tabbed output

2012-02-12 Thread Modulok
On 2/12/12, Michael Lewis  wrote:
> I am having a weird issue. I have a print statement that will give me
> multiple outputs separated by a tab; however, sometimes there is a tab
> between the output and sometimes there is not. It seems sort of sporadic.
> My code is below and two sample outputs are below that (one that works how
> I expect, and the other showing the issue (tab separation not occurring
> between all pieces of the output) What is going on?
>
> def CheckNames():
> names = []
> for loop in range(1,4):
> while True:
> name = raw_input('''Name #%s: ''' %(loop))
> if name not in names:
> names.append(name)
> break
> print '%s is already in the data. Try again.' %(name)
> sorted_names = sorted(names)
> for element in list(sorted_names):
> print 'Hurray for %s!\t' %(element),
>
> Sample Output1:
>
> Name #1: mike
> Name #2: bret
> Name #3: adam
> Hurray for adam!Hurray for bret!   Hurray for mike!
>
> Sample Output2(there is a tab between output 2 & 3, but not 1 & 2):
>
> Name #1: abe
> Name #2: alan
> Name #3: adam
> Hurray for abe! Hurray for adam!  Hurray for alan!
>

You should use spaces, not tabs. Tabs only align with tab stops, which will
depend on the length of the words (names). To make it easy to use spaces
instead, use the 'format()' method available on string objects. A one-line
modification of your could would look like this::

def CheckNames():
names = []
for loop in range(1,4):
while True:
name = raw_input('''Name #%s: ''' %(loop))
if name not in names:
names.append(name)
break
print '%s is already in the data. Try again.' %(name)
sorted_names = sorted(names)
for element in list(sorted_names):
print "{value:<30}".format(value="Hurray for %s!" % element),
# The line above is all that was changed.


The result, is that 'value' will be output as left-aligned '<', and a minimum
of 30 characters wide '30'. The 'value' is specified as a keyword argument to
the format() method. In the example above, 'value' is also making use of
python's older string formatting method. Using a monospaced font, your output
will always be lined up, as long as the 'value' string never exceeds 30
characters wide.

You can optionally *not* specify the 'value' variable and instead use a
positional argument to the format method like this, but it makes it less clear
what you're doing::

print "{0:<30}".format("Hurray for %s!" % element),

As a final note, if 'CheckNames' is a function and not a method, it should be
all lowercase, or use_underscores rather than camelCase. This is not enforced
by python, but is kind of the de-facto standard.

Read more about the format method here:

http://docs.python.org/library/string.html#formatspec
http://docs.python.org/library/string.html#formatstrings


-Modulok-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor