SyntaxError: Invalid syntax on Ubuntu Python 3 Terminal [duplicate] - python

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 4 years ago.
I'm a novice at coding, just come back to it after being in the dark for so long, currently trying to complete an assignment, advice appreciated! Line 13 syntax error
import os
#Imports operating system modules
user_file_search = raw_input('Type Millienium2000 ')
#Prompt the user to enter password
encoded = user_file_search.encode('hex')
#Decodes files based on hex
for root, dirs, files in os.walk ('/Desktop/POP/PoP_Coursework_Assignment/Svr1/documents$'):
for data in files :
pass_file = open(os.path.join(root,data)).read()
if(encoded in pass_file):
print'This could be the pass : {}'.format(os.path.join(root,data))
print 'Located data: {}'.format(pass_file)
#Prints Data retrieved
I've been told that originally it was a indent problem via terminal, I spent some time correcting it I believe and I am now met with
File "oswalk.py", line 13
print'This could be the pass : {}'.format(os.path.join(root,data))
SyntaxError: invalid syntax ^
Pointing towards # {}'
Coding on Ubuntu Python 3
Any help would be appreciated!
Thanks

Welcome to Python 3+, print is not a keyword but a function now, it needs brackets when called:
print ('This could be the pass : {}'.format(os.path.join(root,data)))
print ('Located data: {}'.format(pass_file))

Related

Print function does not run [duplicate]

This question already has answers here:
Python, writing multi line code in IDLE
(7 answers)
Copy-paste into Python interactive interpreter and indentation
(9 answers)
Copying and pasting code into the Python interpreter
(11 answers)
Closed 2 years ago.
Just started learning.
Put the code:
username = input("Enter username:")
print("Username is: " + username)
into IDLE and after it asks for input, I expect it to print something. It doesn't. It just asks for input and that's it. What am I missing?
Just to clarify to everyone- I had entered the code on separate lines and it says
Enter Username:
I enter 'John'
Nothing happened after that.
Edit: I think the answer is that you cannot do multi-line code in the shell. I had no idea what a 'shell' is as opposed to a file where you can do multi-line.
Python is a top-down language, meaning that it will only continue to the next line of code until the current line of code has finished running
The input() function requires that you provide input into the console, to continue to the next line of code. So, you have to input something and only then will it actually print anything.

Python2 And Python3 [duplicate]

This question already has answers here:
"print" throws an invalid syntax error in Python 3
(4 answers)
Closed 2 years ago.
This is for Python 2:
import re
print sum([int(i) for i in re.findall('[0-9]+',open(raw_input('What is the file you want to analyze?\n'),'r').read())])
But why do I get a syntax error with Python 3?
Python3
import re
print sum([int(i) for i in re.findall('[0-9]+',open(input('What is the file you want to analyze?\n')).read())])
That is because in Python3 you should use brackets around the argument for the print function.
print()
so your code will work as soon as you write
print(sum([int(i) for i in re.findall('[0-9]+',open(input('What is the file you want to analyze?\n')).read())]))

Strange syntax and error when executing lambda function with multiple arguments [duplicate]

This question already has answers here:
f-strings giving SyntaxError?
(7 answers)
Closed 3 years ago.
I'm using this tutorial to learn about lambda functions:
How to Use Python lambda Functions
There is an example involving this line:
full_name = lambda first, last: f'Full name: {first.title()} {last.title()}'
I have 2 questions:
There is an "f" in front of "Full name". What does this "f" do?
When I run this line, I immediately get this error:
File "<stdin>", line 1
full_name = lambda first, last: f'Full name: {first.title()} {last.title()}'
^
SyntaxError: invalid syntax
Why does this happen? Why did the tutorial show a properly executed function, but I get an error?
So firstly this f is a new, elegant way to format a string with variables. I invite you to read https://realpython.com/python-f-strings/ that tells the whole story.
However, this exists only since Python 3.6. Can you confirm your version?
python --version
Kind regards -

How to run python in windows 7 [duplicate]

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 7 years ago.
i am new to python. i am try to run my first hello world program but i got an error msg.
please find below the screen shot
Error Msg look like this:
>>> print "hello"
File "" line1
print "hello"
^
SyntaxError: Missing parentheses in call to 'print'
You are using Python 3, In this version print is not a keyword it is a function.
Try this :
print("hello")
See changes in Python3 from Python 2

How to use this code in python 3 version? [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 8 years ago.
I want to run this code in the python 3 but i can't.Whenever i try to run the code,i get the invalid syntax error.
age = 20
name = 'Swaroop'
print '{} was {} years old when he wrote this book'.format(name, age)
print 'Why is {} playing with that python?'.format(name)
Please help me.
Thank you.
Put parentheses around the print function calls.
print('{} was {} years old when he wrote this book'.format(name, age))
print('Why is {} playing with that python?'.format(name))
In Python2, print is a statement, and does not require parenthesis.
In Python3, print is a function, so it requires parentheses around its arguments.

Categories