Reputation: 504
I have been having some problems creating a character printer for an assignment in school, and I do not know how to solve it despite looking everywhere on StackOverflow. I am trying to enable the user to enter the symbols they want for the first two top and bottom lines, and have their two texts centered with forward and backslashes based on word length. However, this has been proven to be a problem.
I have tried to change variables as well as create variables that encapsulate the length of the text into an integer, this has proven to be unsuccessful.
Repeats = input("How many times should the symbol repeat (1-30 CHARACTERS)?:")
insertText = input("Please enter text:")
insertText = str(insertText)
insertMoreText =input("Please enter text (again):")
insertMoreText= str(insertMoreText)
Repeats = int(Repeats)
#Determining text lengths/stored variables:
text1length = len(insertText)
text2length = len(insertMoreText)
forwardSlash = str("/")
backSlash = str("\\")
symbolsbeforeText = "*"
if(Repeats <= 30):
print(firstSymbol * Repeats)
print(secondSymbol * Repeats)
print(symbolsbeforeText * Repeats)
print (forwardSlash * text1length) & (insertText.upper()) & (forwardSlash
* text1length)
print (backSlash * text2length) + (insertMoreText.lower()) + (backSlash *
text2length)
print(symbolsbeforeText * Repeats)
print(secondSymbol * Repeats)
print(firstSymbol * Repeats)
else:
print("You have reached the repetition threshold, please try again.")
The error:
Traceback (most recent call last):
File "C:\Users\colby\trainingTime.py", line 28, in <module>
print (forwardSlash * text1length) & (insertText.upper()) &
(forwardSlash * text1length)
TypeError: unsupported operand type(s) for &: 'NoneType' and 'str'
Process terminated with an exit code of 1
Upvotes: 1
Views: 1038
Reputation: 69903
Your problem is that in Python 3+, print is a function that returns None
, so:
print(forwardSlash * text1length)
returns None
, and
insertText.upper()
returns a str
. Therefore, you cannot perform a bit-wise &
operation on None
and str
.
A very good way to solve this is using an f-string which is a Python feature available since Python 3.6:
print(f"{backSlash * text2length} {insertMoreText.lower()} {backSlash * text2length}")
This will make your code more readable. See that you don't need to concatenate strings using +
which probably lead to your confusion.
Upvotes: 1
Reputation: 106901
The print
function always returns None
, and you're performing bitwise-and operation with the returning value of print
and (insertText.upper())
, which is a string, which causes the said error.
You should call print
with the entire expression enclosed in parentheses as an argument instead:
print((forwardSlash * text1length) & (insertText.upper()) & (forwardSlash * text1length))
print((backSlash * text2length) + (insertMoreText.lower()) + (backSlash * text2length))
Upvotes: 2