当前位置:首页 > 教育培训 >

python代码没错但运行不出来(python写完代码怎么运行)

来源:原点资讯(www.yd166.com)时间:2023-06-04 22:41:24作者:YD166手机阅读>>

#父与子的编程之旅

#与小卡特一起学Python

#第十四课一

#终于到了对象这一章节,集前面学习的大成,尝试着一点一点的理解每行代码的意义,在大脑中模拟运算整个代码可能出现的结果。

#然后打开电脑,按照教材的代码清单,逐个字母的敲击进去。

#不积跬步无以至千里,你要默默努力,悄悄生长,然后惊艳所有人。

#加油,你是最棒的。

#代码1—创建一个对象

class Ball:

def bounce(self):

if self.direction == "down":

self.direction = "up"

myBall = Ball()

myBall.direction = "down"

myBall.color = "red"

myBall.size = "small"

print ("I just created a ball.")

print ("My ball is ",myBall.size)

print ("My ball is ",myBall.color)

print ("My ball`s direction is ", myBall.direction)

print ("Now i`m going to bounce the ball.")

print()

myBall.bounce()

print ("Now the ball`s direction is ",myBall.direction)

print()

#代码2—初始化一个对象,让对象在使用之前处于预设的状态

class Ball:

def __init__(self,color,size,direction):

self.color=color

self.size=size

self.direction=direction

def bounce(self):

if self.direction=="down":

self.direction="up"

myBall=Ball("red","small","down")

print("I just created a ball.")

print ("My ball is ",myBall.size)

print("My ball is", myBall.color)

print("My ball`s direction is ", myBall.direction)

print("Now I`m going to bounce the ball")

print()

myBall.bounce()

print ("Now the ball`s direction is", myBall.direction)

print(myBall)

print()

#代码3,—使用str()方法来改变打印对象的方式

class Ball:

def __init__(self, color, size, direction):

self.color = color

self.size = size

self.direction = direction

def __str__(self):

msg = "Hi, I`m a " self.size " " self.color " " " ball!"

return msg

myBall = Ball("red","small","down")

print (myBall)

#代码4—加热热狗,并添加配料

#学习了这么久之后,第一次编写一个有头有尾的程序,从定义类别开始,到完成配料添加

#对于变成的理解,和程序员的思想,也有了进一步的体会

#看着教材的内容,自己演练演练

#好记性不如烂笔头,看的再仔细,不如自己在键盘上多敲敲

class HotDog:

def __init__(self):

self.cooked_level = 0

self.cooked_srting = "Raw"

self.condiments = []

def __str__(self):

msg= "hot dog"

if len(self.condiments) >0:

msg= msg " with"

for i in self.condiments:

msg= msg i ", "

msg = msg.strip(", ")

msg = self.cooked_string " " msg "."

return msg

def cook(self, time):

self.cooked_level = self.cooked_level time

if self.cooked_level > 8:

self.cooked_string = "Charcoal"

elif self.cooked_level > 5:

self.cooked_string = "Well-done"

elif self.cooked_level > 3:

self.cooked_string = "Medium"

else:

self.cooked_string = "Raw"

def addCondiment (self, condiment):

self.condiments.append (condiment)

myDog = HotDog()

print(myDog)

print ("Cooking hot dog for 4 minutes...")

myDog.cook(4)

print (myDog)

print ("Cookding hot dog for 3 more minutes...")

myDog.cook(3)

print (myDog)

print ("What happens if I cook it for 10 more minutes?")

myDog.cook(10)

print (myDog)

print ("Now, I`m going to add some stuff on my hot dog.")

myDog.addCondiment ("ketchup")

myDog.addCondiment ("mustard")

print (myDog)

print ()

最后的这个代码,无法运行,提示错误,但是我和标准答案比较之后没有错误,谁能发现错在什么地方了吗?

栏目热文

python运行不出结果(python运行不出结果也不报错)

python运行不出结果(python运行不出结果也不报错)

1.异常python发生错误时,会创建一个异常对象,如果编写了处理该异常的代码,程序将继续运行,如果没有编写,程序将停止...

2023-06-04 23:21:02查看全文 >>

python代码不能持续运行(python代码没错但无法运行)

python代码不能持续运行(python代码没错但无法运行)

文 | xybaby 出处 | cnblogs服务器程序员最怕的就是程序crash,不过有时候程序没有crash,但是“...

2023-06-04 23:12:08查看全文 >>

python代码正确但是不运行

python代码正确但是不运行

这节课我们讲要了解一个新的知识:异常处理。假设我们写一个程序,计算两个数相除的商,程序运行后除数输入0。再假设程序中要求...

2023-06-04 23:11:12查看全文 >>

python点击运行没反应(python的idle点了之后没反应)

python点击运行没反应(python的idle点了之后没反应)

安装Python后,它自带一个编辑器IDLE,但是使用几次之后出现启动不了的情况,可做如下操作。Windows操作系统下...

2023-06-04 23:23:02查看全文 >>

python复制的代码不能运行(复制的python代码无法运行)

python复制的代码不能运行(复制的python代码无法运行)

全文共3733字,预计学习时长7分钟python最近火了,大红大紫那种。PYPL(编程语言受欢迎程度) 四月官方榜单宣布...

2023-06-04 22:47:28查看全文 >>

python代码点了运行没用(python代码运行后不动怎么办)

python代码点了运行没用(python代码运行后不动怎么办)

本文只适合python初学者,老鸟可以先飞走。turtle是python标准库之一,可以用来简单地实现画图功能。在使用t...

2023-06-04 22:52:25查看全文 >>

python代码总是无法运行

python代码总是无法运行

本章节我们来讲述一下Python的编码规范,通过详细对代码编写规则以及命名规范等进行介绍。1.编写规则Python采用P...

2023-06-04 23:17:00查看全文 >>

python无法运行(python程序运行不了怎么办)

python无法运行(python程序运行不了怎么办)

在Python中导致程序无法运行的有2种情况。1;语法错误,这是致命错误,需要重新查看代码进行修改,比如:缩进问题,比如...

2023-06-04 22:44:18查看全文 >>

python打完代码后运行不提示(python代码写好了怎么运行不了)

python打完代码后运行不提示(python代码写好了怎么运行不了)

输出用print()在括号中加上字符串,就可以向屏幕上输出指定的文字。比如输出'hello, world',用代码实现如...

2023-06-04 22:53:26查看全文 >>

手动挡空挡起步和停车步骤(手动挡起步和停车步骤)

手动挡空挡起步和停车步骤(手动挡起步和停车步骤)

每天一分钟,懂车好轻松。抽奖周周送,好礼等你来。敬各位英雄豪杰,请关注。手动挡车不同于自动挡,离合器、刹车、油门要配合好...

2023-06-04 23:04:51查看全文 >>

文档排行