• Python Question
    18 replies, posted
I decided to try learning Python, and I had a question. My program keeps closing when I try to launch the .py file. I see a CMD box open for a second and close, and I can't read anything on it. Here is my code [code] x = int(raw_input("Please enter your age: ")) if x > 20: print 'You are legally old enough to drink alcoholic beverages! ' elif x<21: print 'You are not legally old enough to drink alcoholic beverages!' [/code] When I run the .py file, a command prompt window pops up and then closes really fast. Any help?
It usually means you're getting an error. (In this instance it would be your casting of raw_input to an int. Just raw_input() will suffice) The best way to run a script, and get feedback is either directly from the commandline (i.e. python myfile.py) or you could write a batch file that says python myfile.py pause OR you could run the script via the IDLE editor that is built into python. You get nearly instantaneous feedback there.
[QUOTE=Chandler;20783721]It usually means you're getting an error. (In this instance it would be your casting of raw_input to an int. Just raw_input() will suffice)[/QUOTE] He's casting the [I]return value[/I] of raw_input to int, which is actually preferred over relying on the automatic coercion. In Python 3, it wouldn't work without the explicit conversion. The error is probably because of the space before elif.
[QUOTE=jA_cOp;20783959]He's casting the [I]return value[/I] of raw_input to int, which is actually preferred over relying on the automatic coercion. In Python 3, it wouldn't work without the explicit conversion. The error is probably because of the space before elif.[/QUOTE] woops, my bad
I commented everything out except the first part, so this is now my code: [code] x = int(raw_input("Please enter your age: ")) #if x > 20: #print "hello" #elif x < 20 #print "Good Bye" [/code] And it runs, I get the window prompting me to "enter my age". When I press enter or enter any text it closes like it should, but I don't get any result when I do anything else. If I uncomment anything, it just does the closing issue.
[QUOTE=Hax102;20787613]-snip- And it runs, I get the window prompting me to "enter my age". When I press enter or enter any text it closes like it should, but I don't get any result when I do anything else. If I uncomment anything, it just does the closing issue.[/QUOTE] Just run the damn thing from the command line already :v: (I'd do it but I only have Python 3 installed, which is quite different in various ways)
I'm trying to learn python, and this was one of my first ideas and one of the most basic things :v:
[QUOTE=Hax102;20787909]I'm trying to learn python, and this was one of my first ideas and one of the most basic things :v:[/QUOTE] You won't be learning much Python not being able to see parse errors and unhandled exceptions (runtime errors) thrown from your code :P Even when your program runs without error, you would probably like to see its output. As it is now, you wouldn't be able to see the output after inputting your age without running the program from the command line or similar, as the program ends when the interpreter reaches the end of your code.
How can I make this run without closing though? I'm sorry if I sound noobish, but I'm new to python.
[QUOTE=Hax102;20788169]How can I make this run without closing though? I'm sorry if I sound noobish, but I'm new to python.[/QUOTE] It's not really a Python-specific problem, but it's one you really oughta figure out. I assume you are on Windows and have the .py extension registered with the Python interpreter (quite possibly named "python.exe"). This will cause Python programs to be executed when double-clicked. This is nice and Windows-ey and everything, until you realize it's completely useless for console apps because it doesn't show you when stuff went wrong. The proper way to run a Python console application is thus from the command line (cmd.exe being the typical Windows prompt). The ideal way of installing Python is to add the path to the Python interpreter to your Windows PATH environment variable (google for instructions) so you can run the interpreter from anywhere on your system by just typing "python". Another way is to specify the full path to the Python interpreter every time you want to run a script, like: [code]C:/python/python.exe hello.py[/code] versus having Python in path: [code]python hello.py[/code] You could also put all your Python scripts in the Python directory and just invoke "python hello.py" from there, although that's messy for obvious reasons.
I'm not too good at python, but this might work. [code] x = int(raw_input("Please enter your age: ")) if x > 20: print 'Ok' y = int(raw_input("Please enter your lovers age: ")) if y < x: print 'Your lover is younger than you!' elif y > x: print 'You are younger than your lover!' z = int(raw_input("Do you want to close the program now? ")) [/code] It's kind of "cheap" though. No matter what you press at the end, it will close. Quick question: How can I make a standalone python program?
[QUOTE=Hax102;20783110][code] x = int(raw_input("Please enter your age: ")) if x > 20: print 'You are legally old enough to drink alcoholic beverages! ' elif x<21: print 'You are not legally old enough to drink alcoholic beverages!' [/code][/QUOTE] elif is indented. There's your problem.
[QUOTE=Chad Mobile;20792486]I'm not too good at python, but this might work. [code] x = int(raw_input("Please enter your age: ")) if x > 20: print 'Ok' y = int(raw_input("Please enter your lovers age: ")) if y < x: print 'Your lover is younger than you!' elif y > x: print 'You are younger than your lover!' z = int(raw_input("Do you want to close the program now? ")) [/code] It's kind of "cheap" though. No matter what you press at the end, it will close. Quick question: How can I make a standalone python program?[/QUOTE] py2exe or py2app if you're on a mac. Do not cast the return to raw_input at the end to int, it will lead to an error if it returns something that's not an int. You don't even need to bother assigning it to a variable. Further, That will only close if you press enter, which is a reasonable solution to the problem, but will not stop a program that has thrown an error closing immediately, unless you surround the whole program with a try/finally block, which is unbelievably crude. If you are programming python on windows, I would recommend using a proper IDE, especially if starting out. There is nothing particularly wrong with IDLE, which comes with python: it would produce better results than what you are trying to do, and makes it easy to run code. I also used Stani's Python Editor for a while. I've now switched to vim, but I tend not to develop on windows anyway: I use my mac, and just run things from the command prompt.
How would I use py2exe? I couldn't really find a decent tutorial and it's confusing :frown: [B]Edit:[/B]I'm on a PC running Windows XP by the way.
[url]http://www.py2exe.org/index.cgi/Tutorial[/url] Seems good enough to me. If you can't understand, learn more python.
If you are beginner you should use an IDE, it's a lot easier to learn the language that way. Wing IDE is great.
I wanted to try Python a long time ago, but it confused me and it seemed like alot of work to make little things. I was using the "IDLE" thing.
[QUOTE=Chad Mobile;20801951]I wanted to try Python a long time ago, but it confused me and it seemed like alot of work to make little things. I was using the "IDLE" thing.[/QUOTE] Python just does GUIs differently, which is a sign of its roots as a cross platform scripting language. I have written (small versions, obviously) of games, windowed applications and websites (including my own little blogging engine!) in python, and have found it very little work, relatively. If you want to learn, I would recommend O'Reilly's Learning Python.
I got confused with the command prompt stuff lol
Sorry, you need to Log In to post a reply to this thread.