0

When I double click on the file to run it, it just pops up for a few seconds and disappears. I've been told it is a problem with my indentation but what exactly. I'm new to program, so a solution on how to fix my indentation would be helpful.

import random
print('I am thinking of a number between 1 and 50')

number = random.randint(1, 50)
guess = int(input('Can you guess what it is: '))

while guess != number:
    if guess > number:
        print('Lower...')
    else:
        print('higher...')

guess = int(input('Can you guess what it is: '))


print('Correct!')

input('\n\nEnter.')
oneabdi
  • 57
  • 1
  • 1
  • 4

4 Answers4

2

You shouldn't need to click on the filename.py file. Just run

ipython filename.py

in terminal. I tried this and saw a logic error with your guess statement.

Or just

python filename.py

if you don't have ipython installed.

Update:

It looks like you may be having issues with your path and running python.

Windows: resource1
Mac: resource2

Pippin
  • 1,066
  • 7
  • 15
  • 2
    Not everyone is using ipython – Jimmy Kane Dec 01 '13 at 19:13
  • @JimmyKane I've added python for more diverse answer. Thank you. – Pippin Dec 01 '13 at 19:15
  • It says 'python' is not recognized as an internal or external command. – oneabdi Dec 01 '13 at 19:34
  • That means it isn't installed on your path. – rlms Dec 01 '13 at 19:36
  • 1
    @Pippin It won't output anything if it isn't recognized as a command. – rlms Dec 01 '13 at 19:36
  • @sweeneyrod is right. Sounds like it could be a path issue. Have you been able to run python files before? I've added a resource for windows and OSX to my answer that goes over fixing paths. – Pippin Dec 01 '13 at 19:47
  • 1
    If it's Python 3.3 on Windows, the command may be `py -3` (Python 3.3 uses `py.exe` and takes the version as a command line parameter to better accommodate multiple versions) – Free Monica Cellio Dec 01 '13 at 19:54
  • Thanks but once I get to enviromental variables what do I do then? – oneabdi Dec 01 '13 at 19:58
  • I've been able to run all my other python files perfectly by saving them on to my desktop then double clicking on them. – oneabdi Dec 01 '13 at 19:59
  • @user3052934 Have you seen this [page](http://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-7). It links to [this](http://patheditor2.codeplex.com/) path editor. – Pippin Dec 01 '13 at 22:47
0

Run it though terminal. Check @pipins answer.

This way you'll see the output and any errors about indentation etc and where they where made.

As I see there is no problem with your identation

Jimmy Kane
  • 16,223
  • 11
  • 86
  • 117
0

You should take a close look at the line guess = int(input('Can you guess what it is: ')) and think about the intended logic of the while loop...

(the indentation seems to be formally correct, but of yourse your program does behave as you expect...)

The tipp "indentation" is correct, but misleading as well. Since your program seems to do something, your python settings should be ok (but it would not hurt to add python to your system path). The reason your programm closes nearly instantly is due to a wrong indentation which results in a wrong execution logic.

It would be best to follow your program execution in a debugger or to insert several print statements.

In fact, you don't have to change much ;).

OBu
  • 4,977
  • 3
  • 29
  • 45
  • Extended my answer a bit - several discussion threads here are quite misleading... – OBu Dec 01 '13 at 20:16
0

This does not require adding python to your path

open console and type:

/path/to/python.exe /path/to/<file>.py

As an example:

C:\Python27\python.exe C:\Users\user\Desktop\my_program.py

Explaination:

If you double click the .py file, the console opens and should wait for user input.

However, if there are errors it will close immediately and you won't be able to view those errors.

Running your program from the console directly ensures the console remains open and will allow you to see the errors.

bcorso
  • 45,608
  • 10
  • 63
  • 75