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

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.

Related

Python character name [duplicate]

This question already has answers here:
print variable and a string in python
(6 answers)
Closed 10 months ago.
First I assigned a variable
character_name = "Patrice"
On the print function, I wrote
print( " Patrice does a wonderful job at his workplace ")
Instead of writing Patrice on the print function, I wanted to declare the variable that would print Patrice, How would I do it?
There's various ways to achive this, you might want to look at Pythons f-strings:
print(f'{character_name} does a wonderful job at his workplace')

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

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))

Why semi-colon in python after end of print and variable working? [duplicate]

This question already has answers here:
What does a semicolon do?
(5 answers)
Closed 5 years ago.
I am new to python and currently working in it.
I have write one simple program, as i read somewhere that semicolon not allowed or required in python to end statement. but i have use that and till its working fine! anyone explain me
why its possible?
here is code.
a = 10;
if a == 10:
print "value of a is %s"%(a);
else:
print "value of a is not %s"%(a);
semicolon is allowed as statement separator
>>> a=1;b=2;c=3
>>> print(a,b,c)
1 2 3
It is not required if you write each statement in a new line
Python does not require semi-colons to terminate statements. Semi-colons can be used to delimit statements if you wish to put multiple statements on the same line.

Simple Countdown Python [duplicate]

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 5 years ago.
I wrote this code, which I found in the book 'Python for dummies'
countdown = 10
while countdown:
print countdown,
countdown -= 1
print "Blastoff!"
It should print 10 9 8 7 6 5 4 3 2 1 Blastoff!
When I run the program, I get an error 'Missing parentheses in call to print'
On youtube I found a code similar to this, which counts from 0 to 10000000. This one works just fine.
def count(x):
while (x <= 10000000):
print (x)
x+=1
count(0)
print ("I hate my job. I quit!")
How comes they look so different? What kind of basic knowledge do I need to understand this? Is this a question of different python versions? Is 'Python for dummies' a bad book?
This is a Python 2 vs 3 gotcha. print was a keyword in 2, now it's a function in 3. Evidently that code was written for 2, but you're using 3.
Just add parentheses and treat print as you would any other function.
For Python 3.0 and after () are required for `print̀€ .
Change to print ("Blastoff!") you won't have any error
The book you are reading is written for python version 2, where if and while statements do not require parenthesis. However, you seem to be using python version 3, which requires you to put paranthesis in if and while statements. You can solve this issue by adding paranthesis to the code in the right places, or download python version 2 (2.7 is the most common version) so you can use the code without modifying it.

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

Categories