Newbie help with Python?

Is this OK to post here? Code Exchange doens’t have “AppleScript” in the name so I thought it might be OK. 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 tutorial site into the Terminal:

val = input("Enter your value: ")
print(val)

and the result is:

val = input("Enter your value: ")
Enter your value: print(val) my input
Traceback (most recent call last):
File “”, line 1, in
File “”, 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.

Hi LouK.

Code Exhange is for offering code or ideas you think others might find useful. We don’t have a forum specifically for Python, but since it’s a language you can use in a shell script, the AppleScript | Mac OS X forum is probably the best place we have for a query about it. I’ll ask Mark to move this topic there.

I read that Perl, Python 2.7, and Ruby are being deprecated in macOS 10.15 Catalina and that they’ll cease be provided in a future release.

The included copies are, but people can still install their own. This has the added advantage of being able to know exactly which version is being used.

Thanks, Shane.

@LouK:
Looking around this morning, I think the problem may be that you’re pasting the entire script into Terminal at once. Each line is being executed as soon as it’s entered and “print(val)” is being taken as your response to the prompt "Enter your value: ". If you type the first line and then enter a value before typing “print(val)”, everything works as expected.

I don’t know anything about Python (apart from what I’ve learned from your post), but I imagine a complete script would be need to be saved as an executable text file and run as a command from the Terminal.

See Need help with using Applescript to fire a Python script for related problems.
Since you get a syntax error, pay the most attention to the correct use of tabs and spaces in the Python code.

Fix tabs and spaces problem in the source Python code (which you copied from some site) with xCode, and save fixed source code as Python file (.py) somewhere on the disk. Then you can copy the source code to Terminal and run.