首页 > 编程问答 >成功退出但没有输出

成功退出但没有输出

时间:2024-07-30 15:06:25浏览次数:7  
标签:python

它不提供输出,我不知道该怎么做,这非常令人沮丧。

它尝试了一个简单的代码并点击运行,但它没有给出任何输出,而是显示进程已结束,退出代码为零。 请帮我。这真的很困扰我。

class car:
    def getSpeed(self):
        print("155 mph")
    
bmw = car()
ford = car()
car.getSpeed(bmw)
car.getSpeed(ford)
bmw.getSpeed()
ford.getSpeed()

该代码没有产生任何输出,因为它没有在任何地方调用 print() 函数。 getSpeed() 方法正在打印一个值,而不是返回它。要解决此问题,应该从 getSpeed() 方法返回一个值,然后打印该值。

这是更新后的代码:

class car:
    def getSpeed(self):
        return "155 mph"

bmw = car()
ford = car()
print(car.getSpeed(bmw))
print(car.getSpeed(ford))
print(bmw.getSpeed())
print(ford.getSpeed())

此代码现在将为 getSpeed() 方法的每次调用打印 "155 mph"

标签:python
From: 78810185

相关文章

  • 如何使用 python 在 influxdb 中创建组织和存储桶
    如何使用python在influxdb中创建组织和存储桶?我有一个python脚本,用于在influxdb中创建组织和存储桶,但它无法工作并返回未经授权的响应任何人可以使用influxdbapi帮助我解决这个问题吗?HTTPresponsebody:{"code":"unauthorized","message":"write:org......
  • Python - File opening modes and buffering
    'r'-readmode(default)'w'-writemode'a'-appendmode'x'-exclusivecreationWeknowthatthemode'r'opensanexistingfileforreadingonly;thefileshouldalreadyexist.Ifyouopenafilein......
  • 如何使用 Python 对图像中的掩模部分进行聚类?
    我需要以这样的方式拆分蒙版:如果蒙版内存在不一致,则会将其分开。例如,如果我在一只猫上画一个面具,我希望宽的部分(身体)是一个面具,窄的部分(尾巴)是另一个面具。目前,我有一个连续的面具,其中包括两者猫的身体和尾巴。我想将其分成两个不同的面具。如何使用Python实现此目的?原......
  • 如何在 python 中为具有不同类型作为值的字典添加类型声明
    我有一个字典如下my_dict={"key_1":"value_1","key_2":{"key_1":True,"key_2":1200}"key_3":True,}并且在我的类中@dataclassclassTestClass:my_dict:typing......
  • Python TypedDict:继承另一个TypedDict时的函数语法
    给定这种类型:classTPerson(TypedDict):name:straddress:str我想要另一个TypedDict继承前一个,例如:classTDealer(TPerson):db-id:intpolice_record:strarrested_now:boolclassTConsumer(TPerson):db-id:intpreferred_product:......
  • 如何让 python 类型在需要父类时将子类识别为有效类型?
    这是我需要做的一个最小示例:fromtypingimportCallable,AnyclassData:passclassSpecificData(Data):passclassEvent:passclassSpecificEvent(Event):passdefdetect_specific_event(data:SpecificData,other_info:str)->Specif......
  • 使用 Python + Beautiful Soup 抓取任何包含 5 个数字的字符串
    我住在德国,那里的邮政编码在大多数情况下都是5位数字。53525。我真的很想使用beautifulSoup从网站中提取该信息。我是Python/BeautifulSoup的新手,我不知道如何将“查找连续的每5个数字+“空格””翻译成Python语言。importrequestsimporturllib.re......
  • 如何测试 python 类型协议是另一个协议的子类?
    该问题的明显解决方案是使用issubclass,但这会引发TypeError(使用Python3.6.7),例如>>>fromtyping_extensionsimportProtocol>>>classProtoSubclass(Protocol):...pass...>>>issubclass(ProtoSubclass,Protocol)Traceback(mos......
  • Python:指定与继承一起使用的类方法的返回类型
    我一直在尝试了解如何在Python中指定类方法的返回类型,以便即使对于子类也能正确解释它(例如在我的Sphinx文档中)。假设我有:classParent:@classmethoddefa_class_method(cls)->'Parent':returncls()classChild(Parent):pass什么如......
  • python使用SMTP功能发送邮件
    网页格式发送for_email.html<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><style>h1{color:brown;}p{margin:5px;color:purple......