Ok, literally my first day of learning Python. I am working my own way through Python Programming for the Absolute Beginner and am encountering my first glitch. I downloaded 3.3.1 and am just starting to play around.

When I run my program in the Python Shell it works as it is supposed to. But the book tells me to try saving these snippets occasionally and running them through the console window. When I do that the console closes after I type my name and hit enter, no "Hi etc etc"

name=input("What's your name?")
print(name)
print("Hi, ",name)
input("\n\nPress the enter key to exit.")

Am I doing something wrong or do I just not understand what I'm doing yet and this is perfectly normal?

Recommended Answers

All 12 Replies

How are you starting the console?

I'm saving the code in the python shell, then double-clicking the saved file. The console pops up automatically and starts by asking for my name, but when I type it in and hit enter, the window closes.

That happens because the window is owned by the executing program. When the program ends, the window is destroyed. This is normal and expected behavior that doesn't happen when using the Python shell because the Python shell is owned at a higher level than the running program.

If you start the console separately, then navigate to your program and run it that way, the console window won't close because it's owned by its own process and not dependent on the Python program's process.

Alternatively, you can add a request for input at the end of your program to force the process from terminating until input is provided.

Alternatively, you can add a request for input at the end of your program to force the process from terminating until input is provided.

But he already did that:

input("\n\nPress the enter key to exit.")

Exactly, theoretically I should be able to type my name and hit enter, it would then say Hello etc etc and print the line about pressing enter to exit. I haven't read this far, but I would assume there is a way to change the default key needed to exit, such as 'hit the X key to exit."

And how do I open a console window independently? I know how to run a cmd window, but have never needed a seperate console window.

But he already did that:

I'm not hip on Python, but I would assume that input() has a similar problem as you'd see with scanf() in C. Something gets left in the input stream that a subsequent call to input() will terminate successfully on without blocking for the user. The behavior clearly suggests something like this, and my first suggested fix should be preferred anyway because it's more flexible. ;)

I know how to run a cmd window, but have never needed a seperate console window.

Since it sounds like you're using Windows as your OS, go to your programs list and open a command prompt. It should open up in your user home folder. For example, my command prompt in Windows 7 would open under C:\Users\deceptikon. For XP it might be C:\Documents and Settings\deceptikon. Then you cd to the directory where the program is and run it by typing its name.

Yes, Windows XP. Ok, didn't realize that the command prompt was the same as the console window. Got it.

Ok, now I get...

d7ab5ac68f0420c0848030d753c58067

Still not sure if this is an error or just my lack of understanding. I still at least see it as an error because it's not getting to the "Hi, Christopher" part of the code.

I would assume that input() has a similar problem as you'd see with scanf() in C. Something gets left in the input stream that a subsequent call to input() will terminate successfully on without blocking for the user.

input reads a whole line. Nothing can get left in the input stream.

Ok, now I get... [an error about 'Christopher' not being defined]

According to that error message, you must be using Python 2, not Python 3. In Python 2 input will try to evaluate the input as a Python expression, so if you enter Christian, it thinks that that's a variable and complains about it not being defined. Use raw_input to read a string without evaluating it. In Python 3, input acts like Python 2's raw_input, so this wouldn't happen in Python 3.

Bingo!

Ok, I had downloaded both 2 & 3 versions, because I had looked at Codeacademy (which teaches two.something I think) and when I picked up my programming book they said 3.1. I uninstalled the 2.7 version I had, retried and it worked. Windows must have been defaulting to the earlier version because I installed that first.

Thank you! I really didn't want to go any further in the book if I couldn't even get a tiny little snip of code to work right.

input reads a whole line.

Including the newline character?

Including the newline character?

Yes, input consumes the newline character at the end of the line. If it didn't, you'd have to call some other function to discard the newline, every time after you use input. That would be absolutely stupid. I've never seen any "read the whole line" function that didn't consume the newline (that is either return it as part of the string or just read it and throw it away -- the latter being what input does).

The problem that you're describing generally only occurs if you mix input methods that don't discard leading whitespace (like C's getchar, fgets or scanf("%c",...), C++'s getline or Python's input) with ones that do (like C's scanf("anything else") or C++'s istream.operator>>). So if someone only ever uses input, there will be no problem.

# Python2 uses raw_input() for strings
# Python3 uses input() for strings

# ----------------------------------------------
# add these few lines near the start of you code
import sys
# make string input work with Python2 or Python3
if sys.version_info[0] < 3:
    input = raw_input
# ----------------------------------------------

# test ...
# now you can avoid using raw_input()
name = input("Enter your name: ")
# if you expect float input, use float() instead of int()
age = int(input("Enter your age: "))
print("%s you are %d old" % (name, age))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.