python定义不了类_在python类定义中找不到小部分

news/2024/7/5 23:35:55

我的代码快完成了,但我还是搞不懂一件事。问题是:Write a class named Car that has the following data attributes:__year_model

__make

__speed

The Car class should have an __init__ method that accepted the car's year model and make as arguments. These values should be assigned to the object's __year_model and __make data attributes. It should also assign 0 to the __speed data attribute. The class should also have the following methods:accelerate: The accelerate method should add 5 to the speed data attribute each time it is called.

brake: the brake method should subtract 5 from the speed data attribute each time it is called.

get_speed: The get_speed method should return the current speed

Next, design a program that creates a Car object and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times and do the same. However, when the car is braked and speed >0 it should display an error message and reset speed back to 0.

最后一行是我有困难的部分。它正确地加5和减5,但当它小于0时不显示错误消息。这是我的类定义代码和测试它的代码。谢谢你的帮助。在class Car:

def __init__(self, year_model, make):

self.__year_model = year_model

self.__make = make

self.__speed = 0

############# year_model################

def setYear_model(self, year_model):

self.__year_model = year_model

def getYear_model(self):

return self.__year_model

############# Make################

def setMake(self, make):

self.__make = make

def getMake(self):

return self.make

############# speed################

def setSpeed(self, speed):

if speed < 0:

print("Speed cannot be negative")

else:

self.__speed = speed

def getSpeed(self):

return self.__speed

def accelerate(self):

self.__speed += 5

return self.__speed

def brake(self):

self.__speed -= 5

return self.__speed

############# str ############

def __str__(self):

return "Make : " + self.__make + ", Model Year :" + \

self.__year_model + ", speed = " + str(self.__speed)

import ag_CarDefinition

def main():

# Create an instance of Car

my_car = ag_CarDefinition.Car("2008", "Honda Accord")

print(my_car)

print("my_car after instantiating:\n", my_car)

my_car.setSpeed(2)

print("my_car after my_car.setSpeed(2):\n", my_car)

# Accelerate 5 times

print ("car is accelerating: ")

for i in range(5):

my_car.accelerate()

print ("Current speed: ", my_car.getSpeed())

print()

# Brake 7 times

print ("car is braking: ")

for i in range(7):

my_car.brake()

print ("Current speed: ", my_car.getSpeed())

print("my_car values at program end:\n", my_car)

main()


http://www.niftyadmin.cn/n/1930037.html

相关文章

linux centos7中使用service iptables stop 显示not loaded

centos从7开始默认用的是firewalld&#xff0c;这个是基于iptables的&#xff0c;虽然有iptables的核心&#xff0c;但是iptables的服务是没安装的。所以你只要停止firewalld服务即可&#xff1a; sudo systemctl stop firewalld.service && sudo systemctl disable fi…

python corr()用的是什么方法_用Python处理Args的3种方法

欢迎关注 “小白玩转Python”&#xff0c;发现更多 “有趣”1. sys 模块Python 中的 sys 模块具有 argv 功能。当通过终端触发 main.py 的执行时&#xff0c;此功能将返回提供给 main.py 的所有命令行参数的列表。除了其他参数之外&#xff0c;返回列表中的第一个元素是 main.p…

mac 项目部署

#!/bin/bash cd /workspace/GW/mybitauto-operation #替换环镜变量配置信息 src/main/resources/application.properties sed -i s/active.*/active: prod/g src/main/resources/application.properties# git pullmvn clean package/usr/bin/expect <<-EOF #参数设置 se…

mac mysql编码问题吗_Mac环境mysql5.7.21 utf8编码问题及解决方案

1. 目标&#xff1a;将 mysql 的 character_set_server 的值由 latin1 更改为 utf8暂时性&#xff1a;SET character_set_serverutf8 即可&#xff0c;一次性。永久性&#xff1a;需要更改配置文件&#xff0c;见第2步骤。2. mysql 5.7.21 的 support-files里没有配置文件。新建…

23种设计模式 访问者设计模式

https://github.com/yzmaodeng/java-keypointknowledge/commit/4745c8012aebd0022ea779fcef25ce2bc10be1b6访问者模式把数据结构和作用于结构上的操作解耦合&#xff0c;使得操作集合可相对自由地演化。访问者模式适用于数据结构相对稳定算法又易变化的系统。因为访问者模式使得…

小强升职记思维导图_《小强升职记》学习笔记(思维导图)

原图是在XMind中制作的&#xff0c;比较大&#xff0c;所以分解成几张图片来进行梳理。主要框架主要框架分为四个主要部分&#xff1a;认知、方法和工具、问题、实践&#xff0c;进行要点整理。认知1 认知1.1 时间黑洞1.1.1 产生的原因1.1.2 时间的特性1.2 人生目标1.2.1 人生规…

linux上面是否有安装redis,redis启动

edis 支持 32 位和 64 位。这个需要根据你系统平台的实际情况选择&#xff0c;这里我们下载 Redis-x64-xxx.zip压缩包到 C 盘&#xff0c;解压后&#xff0c;将文件夹重新命名为 redis。打开一个 cmd 窗口 使用cd命令切换目录到 C:\redis 运行 redis-server.exe redis.windows.…

MySQL 创建注册页面_需要flask+mysql实现用户注册和登陆效果页面如何实现

首先&#xff0c;请不要用任何ORM(当然SQLAlchemy&#xff0c;Flask-SQLAlchemy也不要用)初学flask&#xff0c;于是参照http://dormousehole.readthedocs.org/en/latest/tutorial/introduction.ht...实际用到的关键代码在这里&#xff1a;https://github.com/mitsuhiko/flask/…