会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132440个问题

老师看下附件代码哪错了

错.gif

goods = [
    {"name": "面包", "mount": 7, "price": 5},
    {"name": "牛奶", "mount": 3, "price": 10},
    {"name": "香蕉", "mount": 1, "price": 12},
    {"name": "大米", "mount": 2, "price": 98}
    ]
def showGoods(x):
    print("--- 商品信息 ---")
    count = 0
    a = 0
    money = 0
    for i in x:
        print(f"{count}",i["name"],i["mount"],i["price"])
        count += 1
        start = (i[a]["mount"])*(i[a]["price"])
        a += 1
        money += start
    return money
    
    
showGoods(goods)
print(f"本次购物总价为{showGoods(goods)}元")


Python 全系列/第一阶段:Python入门/Python入门(动画版) 3511楼
Python 全系列/第一阶段:Python入门/编程基本概念 3514楼
Python 全系列/第一阶段:Python入门/控制语句 3517楼
Python 全系列/第一阶段:Python入门/序列 3518楼
Python 全系列/第一阶段:Python入门/编程基本概念 3519楼
Python 全系列/第一阶段:Python入门/函数和内存分析 3524楼

# 设计一个名为 MyRectangle 的矩形类来表示矩形。
'''这个类包含
(1) 左上角顶点的坐标:x,y
(2) 宽度和高度:width、height
(3) 构造方法:传入 x,y,width,height。如果(x,y)不传则默认是 0,如果 width和 height 不传,则默认是 100.
(4) 定义一个 getArea() 计算面积的方法
(5) 定义一个 getPerimeter(),计算周长的方法
(6) 定义一个 draw()方法,使用海龟绘图绘制出这个'''
import turtle


class MyRectangle:

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        if x == "" or y == "":
            x == 0 and y == 0
        elif width == "" or height == "":
            width == 100 and height == 100
        return "左上角顶点的坐标:{0},{1},矩形的宽:{2},矩形的高:{3}".format(self.x, self.y, self.width, self.height)

    def getArea(self):
        s = self.width * self.height
        print("面积是:", s)

    def getPerimeter(self):
        c = (self.width + self.height) * 2
        print("周长是:", c)

    def draw(self):
        turtle.penup()
        turtle.goto(self.x, self.y)  # 开始起点
        turtle.pendown()  # 下笔
        turtle.goto(self.x, self.y - self.height)
        turtle.goto(self.x + self.width, self.y - (self.height))
        turtle.goto(self.x + self.width, self.y)
        turtle.goto(self.x, self.y)
        turtle.done()  # 保持绘画窗口不消失

s = MyRectangle(x="", y="", width="", height="")
s.getArea()
s.getPerimeter()
s.draw()

老师:这断代码老是报错,找了半天也不知道错在哪里?

Traceback (most recent call last):

  File "C:/Users/Administrator/PycharmProjects/mypro_exception/test.py", line 43, in <module>

    s = MyRectangle(x="", y="", width="", height="")

TypeError: __init__() should return None, not 'str'


Process finished with exit code 1


Python 全系列/第一阶段:Python入门/面向对象 3525楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637