仅当变量 ==1 时才执行 Python 定义

我写了几个定义,我希望用户选择要运行的定义,但定义总是打印如何打开和定义?如果用户选择 2,则不应打印。我错过了什么?


Senerio = 0

while Senerio not in (1, 2, 3, 4):

    Senerio = int(input("choose a commission/Aggregation senerio number \n"

                        "1. put in car \n"                     

                        "2. put in plane \n"

                        "3. put on boat \n"

                        "4. put on fire "))


if Senerio == 1:

Car = 1

Plane = 0

boat = 0

fire = 0


def incar():

    print('Im putting this in a car')

incar()


if car == 1:

    incar


慕桂英3389331
浏览 67回答 2
2回答

慕尼黑8549860

这应该可以解决它Senerio = 0while Senerio not in (1, 2, 3, 4):    Senerio = int(input("choose a commission/Aggregation senerio number \n"                        "1. put in car \n"                                             "2. put in plane \n"                        "3. put on boat \n"                        "4. put on fire "))if Senerio == 1:    car = 1    plane = 0    boat = 0    fire = 0def incar():    print('Im putting this in a car')if car == 1:    incar()

MM们

我发现将我的if语句包裹在定义周围给了我想要的东西,就像这样:if car == 1:    def incar():        print('Im putting this in a car')    incar()    incar
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python