Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie help with input
#1
I just started into Python today and got a lot working in a few hours. But this has me stumped.

I copy and paste (no typos) these two lines from a Python help site into the Terminal:

val = input("Enter your value: ") 
print(val) 
and the result is:

Error:
>>> val = input("Enter your value: ") Enter your value: print(val) my input Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 print(val) my input ^ SyntaxError: invalid syntax
The print(val) line runs into the input line. I typed "My input". It all just runs together. Thanks in advance to anyone who can tell me what's wrong here.

Thanks.
Reply
#2
The code is correct, but somehow you have a problem with the end of line of the first line. I cut and pasted the code and got an error
Error:
SyntaxError: multiple statements found while compiling a single statement
with a big red line going out from the first line.
Don't know what you did, but somehow in your editing you messed the line ending up.
Reply
#3
Paste the lines into the command line, one line at a time:
  1. paste val = input("Enter your value: ") & press enter
  2. enter your input at the prompt
  3. paste print(val) & press enter

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> val = input("Enter your value: ")
Enter your value: Yoriz
>>> print(val)
Yoriz
>>>
Reply
#4
(Aug-28-2019, 09:10 PM)LouK Wrote: I just started into Python today and got a lot working in a few hours. But this has me stumped. I copy and paste (no typos) these two lines from a Python help site into the Terminal:
val = input("Enter your value: ") print(val) 
and the result is:
Error:
>>> val = input("Enter your value: ") Enter your value: print(val) my input Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 print(val) my input ^ SyntaxError: invalid syntax
The print(val) line runs into the input line. I typed "My input". It all just runs together. Thanks in advance to anyone who can tell me what's wrong here. Thanks.

I'm just curious to know which Python version you are using
Reply
#5
I'm running 3.7.4

Type in
v=input("Enter value: ")
print (v)

result:

>>> v=input("Enter value: ")
Enter value: print (v)

It isn't waiting for input.

Per Yoriz, I paste in "v=input("Enter value: ")", type in input, then paste "v=input("Enter value: ")"

Yes, it prints v correctly. It gets the input.

But I'm not clear how this helps me write a script in which I need user input. It doesn't wait. It just moves on to the print command.

Does anyone have something that works for you, that I can paste in to test?

Correction:

Per Yoriz I paste "v=input("Enter value: ")", input, and then "print (v)".

It gets the input, but in a script it doesn't wait, it just moves on to the next command.

I'm brand new to Python. I may have some major misconception about input.

1. It displays the print(v) command but doesn't execute it.
2. When I manually type in the print command, I see it has received the value.

I don't understand. Why doesn't it wait for input and then executes the print command?

>>> v=input("Enter value: ")
Enter value: print (v)hi there
>>> print (v)
print (v)hi there
>>>

(Aug-28-2019, 10:16 PM)jefsummers Wrote: The code is correct, but somehow you have a problem with the end of line of the first line. I cut and pasted the code and got an error
Error:
SyntaxError: multiple statements found while compiling a single statement
with a big red line going out from the first line.
Don't know what you did, but somehow in your editing you messed the line ending up.

I hit Return like every other line of my script, and it all runs. Just not the input command.

What do I do to correct the EOL?


Thanks for reply.
Reply
#6
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#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
#8
Now it's a syntax error.

My file reads:

v=input("Enter a value: ")
print (v)

Run it through Python Launcher and the reult is :

Last login: Thu Aug 29 12:44:21 on ttys000
Lous-MacBook-Pro:~ lk$ cd '/Users/lk/Desktop/' && '/usr/bin/pythonw' '/Users/lk/Desktop/sample.py' && echo Exit status: $? && exit 1
Enter a value: I type a value
Traceback (most recent call last):
File "/Users/lk/Desktop/sample.py", line 1, in <module>
v=input("Enter a value: ")
File "<string>", line 1
I type a value
^
SyntaxError: invalid syntax
Lous-MacBook-Pro:Desktop lk$

Q: How do you guys get input in your script's? I've hit a brick wall.

But if I type in "ertwert" it turns into a name error.

Last login: Thu Aug 29 13:02:07 on ttys005
Lous-MacBook-Pro:~ lk$ cd '/Users/lk/Desktop/' && '/usr/bin/pythonw' '/Users/lk/Desktop/sample.py' && echo Exit status: $? && exit 1
Enter a value: ertwert
Traceback (most recent call last):
File "/Users/lk/Desktop/sample.py", line 1, in <module>
v=input("Enter a value: ")
File "<string>", line 1, in <module>
NameError: name 'ertwert' is not defined
Lous-MacBook-Pro:Desktop lk$

Different errors depending upon what is typed in???
Reply
#9
I finally got this to run with input() in IDLE as Run Module.

I paste it into Terminal under the Pyton interpreter, the input fails.

If someone could explain this I think it'd help my concept of Python quite a bit.


Thanks!


# CONVERT DECIMAL FEET TO FEET/INCHES
decInput=input("Enter decimal feet: ")
feetDec=float(decInput)

# INTEGERS:
feetInt=int(feetDec)
feetDecFrac= feetDec-feetInt
inchesDec=feetDecFrac*12
inchesInt=int(inchesDec)
feetIntDisp = "%8.0f" % feetInt
inchesIntDisp="%3.0f" % inchesInt
if inchesDec<100:
inchesIntDisp ="%2.0f" % inchesDec
#if inchesDec<10:
# inchesIntDisp ="%1.0f" % inchesDec

# INCH FRACTION:
decA =[0.00000,0.03125,0.06250,0.09375,0.12500,0.15625,0.18750,0.21875,0.25000,0.28125,0.31250,0.34375,0.37500,0.40625,0.43750,0.46875,0.50000,0.53125,0.56250,0.59375,0.62500,0.65625,0.68750,0.71875,0.75000,0.78125,0.81250,0.84375,0.87500,0.90625,0.93750,0.96875,1.00000]
fracA=["0","1/32","1/16","3/32","⅛","5/32","3/16","7/32","¼","9/32","5/16","11/32","⅜","13/32","7/16","15/32","½","17/32","9/16","19/32","⅝","21/32","11/16","23/32","¾","25/32","13/16","27/32","⅞","29/32","15/16","31/32","1"]

k=-1
minDiff=99
inchesDecFrac=inchesDec-inchesInt
while (k<32): # First array position is 0
k=k+1
diff=abs(decA[k]-inchesDecFrac)
if diff<minDiff:
minDiff=diff
closestFrac=fracA[k]

print (feetIntDisp + "' " + inchesIntDisp + "-" + closestFrac + "\"")

My question now is, will it run in Pythonista?
Reply
#10
Parsing Big XML Files
Need help to parse very large xml files(>10GB). Could you please help how to avoid memory issues. We can't read the entire file once. Any code example or conceptual explanation please. Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Big Grin Newbie to Python - input and branching issue Sherine 3 2,242 Jul-31-2021, 01:09 AM
Last Post: Pedroski55
  Newbie ? Python 3 Input() jlgrunr 1 2,461 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