【问题标题】:How to check an input for class attributes [closed]如何检查类属性的输入[关闭]
【发布时间】:2022-11-20 02:09:40
【问题描述】:

我想编写一个代码来检查输入中写入的内容是否等于类中某个对象的属性“名称”。

这就是我所做的

class place(object):
    def __init__(self,name)
        Self.name = str(name)

room = place('room')

inp = str(input(''))
if inp == place.name:
    print('1')
else print('2')

我知道这是错误的,但我想 知道我需要做什么才能把它做好。

【问题讨论】:

  • 您的意思是:if inp == room.name:
  • 欢迎来到堆栈溢出!请收下tour,阅读How to Ask。看起来 quamrana 发现了问题,但如果没有,您需要创建一个minimal reproducible example。这段代码从语法到语义都有各种不同的问题,所以你并不完全清楚哪一部分需要帮助。

标签: python


【解决方案1】:

您应该为类的实例调用属性名称。 在你的代码中它应该是:

if inp == room.name:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2016-04-03
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    相关资源
    最近更新 更多