Python 7 Levels, L5, Unit 44, Advanced Functions

Powered By EmbedPress

Exercise 1:

def meat(name,*d):
    str1 = ""#create an empty string
    if len(d) != 0:
        for i in d:
            str1 += i
        return "Meat Kebab:"+name + ",Ingredients:"+str1
    else:
        return "Meat Kebab:"+name

name = input("Please input types of meat kebab:(Pork Kebab,Beef Kebab,Lamb Kebab)")
p = input("Do you need spicy?(yes/no)")
s = input("Do you need sauce?(yes/no)")
if p == "yes" and s == "yes":
    print(meat(name,"Spicy","Sauce"))
elif p == "yes" and s == "no":
    print(meat(name,"Spicy"))
elif p == "no" and s == "yes":
    print(meat(name,"Sauce"))
else:
    print(meat(name))

Exercise 2:

a = 100


def outer():
    b = 10

    def inner():
        nonlocal b
        b += 1
        print("inner:", b)

        global a
        a += 1

    inner()
    print("outer b:", b)


outer()
print("a:",a)

Homework:

import turtle as t
t.penup()#pen up
t.goto(-200,0)#Initial position, default pointing to x axis positive
t.pendown()#pen down
t.speed(50)#drawing speed
t.color("blue")#pen color
def star(size):#a function drawing star
    if size<10:
        return#invalid if it's less than 10
    else:
        for i in range(5):
            t.forward(size)
            t.left(144)
            star(size/3)
star(360)#call function
t.done()#end

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts