Cannot display output in PyCharm


#1

While I was learning python on codecademy, the method I was tough to display variable on output was
print "SomeText: %s" % (variable)

When i try to execute same code on PyCharm it gives me an error:

Code:
from random import randint
output = randint(1, 6)
print('Output: %d') % output

Output:
Output: %d
Traceback (most recent call last):
File "C:/Users/CH325/Desktop/Python/Dice/Dice.py", line 8, in <module>
print('Output: %d') % output
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

Are there any other ways to display the variables? Or is my code wrong?


#2

your last line should look like:

print('Output: %d' % output)

IMHO a better way to print it is (but its Python3-style):

print("Output: {}".format(output))

#3

What does format stands for?


#4

Its a method to format strings in Python3.
Take a look at Format String Syntax for further informations.

Happy coding :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.