个人项目代码有问题(替换字符)

2024-10-02 22:25:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一组朋友设置一个个人项目,出现以下错误:

回溯(最近一次呼叫): 文件“C:\Users[my pc name]\Downloads\Python code\meme文档.py,第11行,在 直线=生产线.re(字符,') AttributeError:“tuple”对象没有属性“re”

我在网上查了一下,找不到任何与我的问题有关的东西。这是我的密码:

import re
name = input("What is the name of this meme?")
age = input("How Old is this meme?")
purpose = str(input("please state the purpose of this meme: (if there is none, reply with 'N/A'"))

wholeAssDocumentation = ("This meme is known as the" , name , "and it is" , age , "years old. Its purpose was:" , purpose)
print = (wholeAssDocumentation)

line = (wholeAssDocumentation)  
for char in "( ?.!/;:)'":  
    line = line.re(char,'')

预期:它将从字符串中完全删除列出的字符(如我将其写入的文本文件中所示)

Actual: Traceback (most recent call last):
  File "C:\Users\[my pc name]\Downloads\Python  code\meme documentation.py", line 11, in <module>
    line = line.re(char,'')
AttributeError: 'tuple' object has no attribute 're'

Tags: thenamereinputismydownloadsline
2条回答
  1. wholeAssDocumentation是一个元组,您知道它是您创建的。你知道吗
  2. 您将其分配给line,因此line也是。你知道吗
  3. 您试图执行reline方法/函数,正如错误明确指出的那样,该方法/函数不存在。你知道吗

你从哪里知道元组有一个re方法?你知道吗

在python中,元组是用tup = (val1,val2,...)定义的。这就是定义wholeAssDocumentation时要做的事情。因为元组没有re方法,所以会出现错误。你知道吗

除此之外,我建议您阅读regex文档,因为这也不是在字符串上使用regex的方式。你知道吗

另外,由于某种原因,您正在覆盖print函数

相关问题 更多 >