Jump to content

python help?


dragonstar57

Recommended Posts

"python myfirstprogram.py" is the correct code, but you need to run it in the plain command prompt, not in the Python REPL.

 

In other words, don't go:

 

$ python
Python 2.5.1 (r251:54863, May  2 2007, 16:56:35) 
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 
Type "help", "copyright", "credits" or "license" for more information.
>>> $python myfirstprograp.py

 

Do this:

 

$ python myfirstprograp.py

 

And the $ indicates the beginning of the command prompt line, not something you type in.

Link to comment
Share on other sites

To make matters short (and not explaining what you did wrong - it only really matters if you are not using Windows, which seems rather unlikely). I'm not a python expert, but you could try

 

import myfirstprograp.py

or

execfile("myfirstprograp.py")

 

@Capn: My suspicion is that dragonstar might simply not have a console.

Edited by timo
Link to comment
Share on other sites

python myfirstprogram.py

SyntaxError invalid syntax

python myfirstprogram.py

^

import myfirstprogram.py

traceback <most recent call last?>:

file"<stdin>", line 1, in <module>

ImporteError: no module named myfirstprogram.py

 

ececfile("myfirstprogram.py")

SyntaxError: EOL while scanning string literal

 

this is not working...:( :(

Windows has Command Prompt, but execfile() should work too.

To make matters short (and not explaining what you did wrong - it only really matters if you are not using Windows, which seems rather unlikely). I'm not a python expert, but you could try

 

import myfirstprograp.py

or

execfile("myfirstprograp.py")

 

@Capn: My suspicion is that dragonstar might simply not have a console.

i'm using windows xp

and the "program" that i am typing my code into is called

python.exe

Edited by dragonstar57
Link to comment
Share on other sites

Go to Start->All Programs->Accessories. Fire up Command Prompt. Save your Python program directly in C:/. In Command Prompt, type:

 

"python.exe nameofyourpythonprogram.py"

 

ececfile("myfirstprogram.py")

SyntaxError: EOL while scanning string literal

This just means it couldn't find the file.

Link to comment
Share on other sites

Or add the line

raw_input()

as the last line of your program (it will wait for you to press <enter> before finishing the program), and try to run your program by double-clicking it. It you're lucky, files ending on ".py" are automatically opened with python. While Cap'n is right that Windows has a console, he's wrong in assuming that Windows users use it :P.

Edited by timo
Link to comment
Share on other sites

Go to Start->All Programs->Accessories. Fire up Command Prompt. Save your Python program directly in C:/. In Command Prompt, type:

 

"python.exe nameofyourpythonprogram.py"

 

 

This just means it couldn't find the file.

in quotes?

Or add the line

raw_input()

as the last line of your program (it will wait for you to press <enter> before finishing the program), and try to run your program by double-clicking it. It you're lucky, files ending on ".py" are automatically opened with python. While Cap'n is right that Windows has a console, he's wrong in assuming that Windows users use it :P.

(still not working)

when i click them it opens and closes in under a second

where does the rawinput command go?

Edited by dragonstar57
Link to comment
Share on other sites

You'll have to be more specific, because I'm not psychic. What did you do, and what happened?

python.exe myfirstprogram.py

file '<stdin>",line 1

python.exe myfirstprogram.py

_____________________^

syntaxerror: invalid syntax

Edited by dragonstar57
Link to comment
Share on other sites

when i click them it opens and closes in under a second

where does the rawinput command go?

The raw_input() is supposed to stop exactly this immediate closing of your program. It should be the last command/line in the program file itself, i.e. a test program (stored in a file ending on ".py") could be

print "Hello Windows ", 3+4
raw_input("Hit <enter> to ->exit")

If that doesn't do the trick, then you'll either have to play around with some Windows settings (there might be something like "close window after program finishes") or learn how to use the Windows console.

Link to comment
Share on other sites

replace the execfile( "c:/Users/PrettyFlower/Desktop/myfirstprogram.py" )

 

with

 

exec( open( "c:/Users/PrettyFlower/Desktop/myfirstprogram.py" ).read() )

 

and

 

raw_input()

 

with

 

input()

 

as you are using 3.1 and not 2 something :/

 

google errors as you will have more errors than you will be able to post eg. google => execfile nameerror name ....

 

This is assuming that you are running in All Programs => Python 3.1 => Python (command line)

 

myfirstprogram.py

print( 1 + 1 )
input()

 

I could probably expand on this and say that exec is acting on a 'stream' object. The file itself is not a 'stream object it is a file. So first we open the file with the open() function inputing the file name as parameter. This function loads the contents of the file as a 'storage' object. The 'storage' object is returned by reference whereupon the 'stream' object is created by appending the .read() to the reference returned by the open( fn ) function call.

 

The changes themselves from release to release reflect optimizations done and the result closely mirrors the underlying function calls which are made via the python interpreter down to the c language.

 

If you wish to run the program as the tutorial you linked suggested you would have to open

 

All Programs => Accessories => command prompt

 

and then go to the python directory so ...

 

C:\python\

 

and run something like

 

C:\python\python.exe myfirstprogram.py

Edited by Xittenn
Link to comment
Share on other sites

I wouldn't worry too much about the ramp up if it is something you really want to do. I've been doing this stuff since I was five and I still suck, but I do it anyway .... We don't all have to be Mark Zuckerberg. He's funny looking anyway :)

 

/me shakes fist

Link to comment
Share on other sites

is the command prompt the same thing as a (command line)

 

 

Or add the line

raw_input()

as the last line of your program (it will wait for you to press <enter> before finishing the program), and try to run your program by double-clicking it. It you're lucky, files ending on ".py" are automatically opened with python. While Cap'n is right that Windows has a console, he's wrong in assuming that Windows users use it :P.

when i do that they close before i can see the output

 

Or add the line

raw_input()

as the last line of your program (it will wait for you to press <enter> before finishing the program), and try to run your program by double-clicking it. It you're lucky, files ending on ".py" are automatically opened with python. While Cap'n is right that Windows has a console, he's wrong in assuming that Windows users use it :P.

its still closing in before i can see if it works

 

"python.exe nameofyourpythonprogram.py"

was this meant to be in quotes?in the actual code?

 

The raw_input() is supposed to stop exactly this immediate closing of your program. It should be the last command/line in the program file itself, i.e. a test program (stored in a file ending on ".py") could be

print "Hello Windows ", 3+4
raw_input("Hit <enter> to ->exit")

If that doesn't do the trick, then you'll either have to play around with some Windows settings (there might be something like "close window after program finishes") or learn how to use the Windows console.

now the program runs when clicked

 

replace the execfile( "c:/Users/PrettyFlower/Desktop/myfirstprogram.py" )

 

with

 

exec( open( "c:/Users/PrettyFlower/Desktop/myfirstprogram.py" ).read() )

 

and

 

raw_input()

 

with

 

input()

 

as you are using 3.1 and not 2 something :/

 

google errors as you will have more errors than you will be able to post eg. google => execfile nameerror name ....

 

This is assuming that you are running in All Programs => Python 3.1 => Python (command line)

 

myfirstprogram.py

print( 1 + 1 )
input()

 

I could probably expand on this and say that exec is acting on a 'stream' object. The file itself is not a 'stream object it is a file. So first we open the file with the open() function inputing the file name as parameter. This function loads the contents of the file as a 'storage' object. The 'storage' object is returned by reference whereupon the 'stream' object is created by appending the .read() to the reference returned by the open( fn ) function call.

 

The changes themselves from release to release reflect optimizations done and the result closely mirrors the underlying function calls which are made via the python interpreter down to the c language.

 

If you wish to run the program as the tutorial you linked suggested you would have to open

 

All Programs => Accessories => command prompt

 

and then go to the python directory so ...

 

C:\python\

 

and run something like

 

C:\python\python.exe myfirstprogram.py

 

C:\python\python.exe myfirstprogram.py

file "<stdin>', line 1

 

C:\python\python.exe myfirstprogram.py

^

SyntaxError: invalid syntax

Edited by dragonstar57
Link to comment
Share on other sites

Okay, tell me exactly what you're doing before you type in those commands. What program are you typing them into? How do you start it?

 

Your problem seems to be that you're given the commands to the wrong thing, but you're not giving me enough detail to tell what you need to do.

Link to comment
Share on other sites

Okay, tell me exactly what you're doing before you type in those commands. What program are you typing them into? How do you start it?

 

Your problem seems to be that you're given the commands to the wrong thing, but you're not giving me enough detail to tell what you need to do.

sometimes i click the start menu and click on python(command line)

that opens a window called python(command line)

and sometimes i go to the C drive to click on the program and that oppens

C:\python22\python.exe

they both open identical windows except for the name(a small black window with white text in what appears to be python).

i have the windows version

http://wiki.python.org/moin/BeginnersGuide/Download

Edited by dragonstar57
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.