2

Though I use input() at the end of my code, the console stil immediately closes after entering the name in the window in Windows 10 (outside of IDLE), and I cannot see the result. What can I do to stop the console from closing?

#! python 3
name = input('Enter your name: ')
print('Hello ' + name)
input('press Enter to exit: ')
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Mohammadreza
  • 79
  • 2
  • 9
  • 2
    Your shebang should be `#!python3`, without a space between "python" and "3", or preferably `#!/usr/bin/python3` -- a virtual Unix path for cross-platform compatibility . If you have .py files associated with the py.exe launcher, it uses the shebang and even supports command line options. So you could have the interpreter exit into the REPL by changing the shebang to `#!/usr/bin/python3 -i`. – Eryk Sun Jul 27 '19 at 09:54
  • 1
    Generally a script that prints output that should persist after exiting should be run from a console shell such as CMD or PowerShell. Python will inherit the console window from the shell, and after Python exits the shell will resume and the console remains open. – Eryk Sun Jul 27 '19 at 09:57
  • I modified the script as you suggested, but it doesn't work. May there be something wrong with windows settings? – Mohammadreza Jul 27 '19 at 15:40
  • If you fixed the shebang and have Python 3 installed properly, with py.exe handling .py files, then I don't know what the problem could be. However, my suspicion is that you're running Python 2, in which `input` evaluates the input text as an expression and will thus raise an exception such as `NameError`. Add `import sys; print(sys.version_info)` prior to calling `input`. If it's Python 2, try fixing your .py file association. Use the "open with" dialog to select and always use the "Python" application that has a rocket in its icon. Do not browse for py.exe; that creates a broken association. – Eryk Sun Jul 28 '19 at 03:04

0 Answers0