Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie help with input
#7
(Aug-29-2019, 03:34 PM)perfringo Wrote: You should get some basics sorted out and then you are good to go.

By referring to 'terminal' you actually use Python interactive interpreter. It interprets code row by row - you enter row, it interprets it (takes some action) and only after that it interprets next line.

If you paste two rows into interactive interpreter following happens: it reads firs row v = input("Enter value: ". Based on interpreting this line it displays input prompt and waits for action. After that it takes second line and it will become the action interpreter waited (i.e. entered value).

Interpreter don't execute row immediately only if row ends with :, it indicates that there will be code block which must be interpreted together (loops, functions, classes etc).

So some advices:

- don't paste your code into interpreter, type it (especially if you are a beginner)
- interpreter is for checking stuff (built-in help) and playing with ideas
- for code to 'run' you create file (with .py extension) and run it. For that you should use decent editor (my personal suggestion is VS Code).
- you can use interactive mode while running file.

Example:

Write following code into file (and save it as sample.py).

v = input('Enter the value: ')
print(v)
Then head to terminal and cd yourself into directory where your file resides. There enter python -i sample.py and your code runs as expected. However, after finishing (printing out value of v) it will remain active so that you can further test and play with your code in interactive interpreter. If you don't need interactivity you just omit -i.

OK, I will try running a file. But I hqve a script of 64 lines that run just fine pasting them into the Terminal. But the interpreter does not wait for input.

(Aug-29-2019, 05:38 PM)LouK Wrote: Then head to terminal and cd yourself into directory where your file resides. There enter python -i sample.py and your code runs as expected. However, after finishing (printing out value of v) it will remain active so that you can further test and play with your code in interactive interpreter. If you don't need interactivity you just omit -i.

I saved it as a file, but I'm getting an unexpected token error trying to run it from the Terminal. Looks like I've forgotten the syntax to do this.

(Aug-29-2019, 05:38 PM)LouK Wrote: Then head to terminal and cd yourself into directory where your file resides. There enter python -i sample.py and your code runs as expected. However, after finishing (printing out value of v) it will remain active so that you can further test and play with your code in interactive interpreter. If you don't need interactivity you just omit -i.

I've tried to cd/ relative to user folder as well as cd/full path from the HD and I get nothing but "No such file"

Ex: cd/MyInternalDrive/Users/LK/Desktop/sample.py

It's been a while since I did this, what am I forgetting?

(Aug-29-2019, 05:38 PM)LouK Wrote: Then head to terminal and cd yourself into directory where your file resides. There enter python -i sample.py and your code runs as expected. However, after finishing (printing out value of v) it will remain active so that you can further test and play with your code in interactive interpreter. If you don't need interactivity you just omit -i.

Sorry, drifted out of Python. Now I'm in Python and i get

>>> cd/MyInternalDrive/Users/LK/Desktop/sample.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'cd' is not defined
>>>
Reply


Messages In This Thread
Newbie help with input - by LouK - Aug-28-2019, 09:10 PM
RE: Newbie help with input - by jefsummers - Aug-28-2019, 10:16 PM
RE: Newbie help with input - by Yoriz - Aug-28-2019, 10:21 PM
RE: Newbie help with input - by Malt - Aug-29-2019, 05:59 AM
RE: Newbie help with input - by LouK - Aug-29-2019, 02:00 PM
RE: Newbie help with input - by perfringo - Aug-29-2019, 03:34 PM
RE: Newbie help with input - by LouK - Aug-29-2019, 05:38 PM
RE: Newbie help with input - by LouK - Aug-29-2019, 06:50 PM
RE: Newbie help with input - by LouK - Aug-29-2019, 08:41 PM
RE: Newbie help with input - by onlydibs - Dec-22-2019, 04:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Big Grin Newbie to Python - input and branching issue Sherine 3 2,341 Jul-31-2021, 01:09 AM
Last Post: Pedroski55
  Newbie ? Python 3 Input() jlgrunr 1 2,537 Feb-17-2018, 10:26 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020