[Tutor] Basic program question

Steven D'Aprano steve at pearwood.info
Mon Jul 25 01:23:48 CEST 2011


Alexander Quest wrote:
> Hello- I am running Python v 3.1.1. As an exercise, I wrote a simple coin
> flipper program, where the computer flips a coin 100 times and then prints
> out the number of heads and tails. My program crashes immediately if I run
> it normally through the command line, but if I go to "Run- Run Module," it
> seems to work just fine. I can't seem to figure out why. I've pasted the
> relevant code below- any help will be greatly appreciated. Thanks!


What do you mean, "crashes"? Do you get a segmentation error? A Python 
traceback? Computer hangs and you have to restart to recover? Something 
else?

What do you mean, "run it normally through the command line"?

What operating system are you using? What command are you using on the 
command line?

Since I love guessing games, I'm going to take a wild stab in the dark 
that you're using Linux, and you're done something like this:

[steve at sylar ~]$ echo "print('spam')" > spam.py  # make a Python script
[steve at sylar ~]$ cat spam.py
print('spam')
[steve at sylar ~]$ chmod u+x spam.py  # make it executable
[steve at sylar ~]$ ./spam.py
./spam.py: line 1: syntax error near unexpected token `'spam''
./spam.py: line 1: `print('spam')'

That's because you're trying to run it as a shell script. You need to 
add a hash-bang line to the file, or run it with Python:

[steve at sylar ~]$ python3 spam.py
spam


If that's not what you are doing, then we'll need more information to 
solve the problem.



-- 
Steven


More information about the Tutor mailing list