gpt4 book ai didi

python - 类型错误 : unsupported operand type(s) for &: 'NoneType' and 'str'

转载 作者:太空宇宙 更新时间:2023-11-04 11:12:04 25 4
gpt4 key购买 nike

我在为学校作业创建字符打印机时遇到了一些问题,尽管在 StackOverflow 上到处查看,但我不知道如何解决它。我正在尝试使用户能够为前两个顶行和底行输入他们想要的符号,并根据字长将他们的两个文本以正斜杠和反斜杠居中。然而,这已被证明是一个问题。

我尝试过更改变量以及创建将文本长度封装为整数的变量,这被证明是不成功的。

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

错误:

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

最佳答案

print 函数始终返回 None,并且您正在对 print 的返回值执行按位与运算(insertText.upper()),这是一个字符串,导致上述错误。

您应该调用 print 并将整个表达式括在括号中作为参数:

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

关于python - 类型错误 : unsupported operand type(s) for &: 'NoneType' and 'str' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57965421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com