【问题标题】:I am trying to call a function inside of an if statement but it's not working. I am also using turtle in the function我试图在 if 语句中调用一个函数,但它不起作用。我也在函数中使用乌龟
【发布时间】:2020-09-27 19:56:44
【问题描述】:

所以我正在开一家太阳镜店。 这是我的代码:

# Sun_Glasses_Shop             

import turtle
import random
from random import randint

def build_glasses() :
    crim = turtle.Turtle()
    colors = ['red', 'blue', 'green', 'yellow', 'plum', 'sienna']

crim.color(random.choice(colors), random.choice(colors))
randpos = randint(-100, 100)
crim.width(2)

crim.penup()
crim.setposition(randpos, randpos)
crim.pendown()

# Making First Box 

crim.begin_fill()
for x in range(4):
    crim.forward(50)
    crim.right(90)

crim.end_fill()
crim.forward(50)
crim.right(90)
crim.forward(5)
crim.left(90)
crim.forward(50)
crim.left(90)
crim.forward(5)
crim.right(90)
crim.begin_fill()

# Making Second Box 

for x in range(4):
    crim.forward(50)
    crim.right(90)
crim.end_fill()

print("Welcome to the glasses shop, would you like some?")
A1 = str(input())
if A1 == "yes" or A1 == "Yes" :
    A1 = str(input())    print("Okay, coming right up!")
    build_glasses()
else :
    print("I dont understand try asking me later, bye!")

但是当我运行代码时,它会在模块中打印出来:

Welcome to the glasses shop, would you like some?
yes

Okay, coming right up!

海龟图形不弹出。

【问题讨论】:

    标签: python if-statement turtle-graphics


    【解决方案1】:

    这里是固定代码(你有一个缩进错误):

    import turtle
    import random
    from random import randint
    
    def build_glasses() :
        crim = turtle.Turtle()
        colors = ['red', 'blue', 'green', 'yellow', 'plum', 'sienna']
    
        crim.color(random.choice(colors), random.choice(colors))
        randpos = randint(-100, 100)
        crim.width(2)
    
        crim.penup()
        crim.setposition(randpos, randpos)
        crim.pendown()
    
        # Making First Box 
    
        crim.begin_fill()
        for x in range(4):
            crim.forward(50)
            crim.right(90)
    
        crim.end_fill()
        crim.forward(50)
        crim.right(90)
        crim.forward(5)
        crim.left(90)
        crim.forward(50)
        crim.left(90)
        crim.forward(5)
        crim.right(90)
        crim.begin_fill()
    
        # Making Second Box 
    
        for x in range(4):
            crim.forward(50)
            crim.right(90)
        crim.end_fill()
    
    print("Welcome to the glasses shop, would you like some?")
    A1 = str(input())
    if A1 == "yes" or A1 == "Yes" :
        print("Okay, coming right up!")
        build_glasses()
    else :
        print("I dont understand try asking me later, bye!")
    

    输出:

    Welcome to the glasses shop, would you like some?
    yes
    Okay, coming right up!
    >>>
    

    【讨论】:

      猜你喜欢
      • 2021-05-27
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2023-01-23
      相关资源
      最近更新 更多