简体   繁体   English

TypeError:&:不支持的操作数类型:“ NoneType”和“ str”

[英]TypeError: unsupported operand type(s) for &: 'NoneType' and 'str'

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. 我在学校为作业分配字符打印机时遇到了一些问题,尽管在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

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. print函数始终返回None ,并且您正在使用print(insertText.upper())的返回值执行位与运算,这是一个字符串,这会导致上述错误。

You should call print with the entire expression enclosed in parentheses as an argument instead: 您应该使用括号将整个表达式括起来作为参数来调用print

print((forwardSlash * text1length) & (insertText.upper()) & (forwardSlash * text1length))
print((backSlash * text2length) + (insertMoreText.lower()) + (backSlash * text2length))

Your problem is that in Python 3+, print is a function that returns None , so: 您的问题是在Python 3+中,print是一个返回None的函数,因此:

print(forwardSlash * text1length)

returns None , and 返回None ,并且

insertText.upper()

returns a str . 返回一个str Therefore, you cannot perform a bit-wise & operation on None and str . 因此,您无法对Nonestr进行按位&运算。

A very good way to solve this is using an f-string which is a Python feature available since Python 3.6: 解决此问题的一种非常好的方法是使用f字符串,这是Python 3.6以来提供的Python功能:

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. 看到您不需要使用+连接字符串,这可能会引起混乱。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:+= 不支持的操作数类型:&#39;NoneType&#39; 和 &#39;str&#39; - TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str' TypeError:%支持的操作数类型:&#39;NoneType&#39;和&#39;str&#39; - TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' 类型错误:+ 不支持的操作数类型:“NoneType”和“str” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' TypeError:+不支持的操作数类型:“ NoneType”和“ str” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' CSV阅读器:TypeError:+不支持的操作数类型:“ NoneType”和“ str” - CSV Reader: TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' TypeError:+:使用GITPYTHON的&#39;NoneType&#39;和&#39;str&#39;不受支持的操作数类型 - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' using GITPYTHON Python TypeError:+不支持的操作数类型:&#39;NoneType&#39;和&#39;str&#39; - Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' python2 :: TypeError:+:&#39;NoneType&#39;和&#39;str&#39;不受支持的操作数类型 - python2::TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' 如何解决:TypeError:+不支持的操作数类型:“ NoneType”和“ str” - How to fix: TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' Python TypeError:+不支持的操作数类型:&#39;NoneType&#39;和&#39;str&#39; - Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM