大白兔奶糖12138 发表于 2021-8-2 10:49:28

为什么我输入了函数报错了

def watchMovie(name="小甲鱼",cigaertte=True,beer=True,girlfirend=True):
        sentence = name+"带着"
        if cigarette:
                sentence = sentence+"香烟"
        if beer:
                sentence = sentence+"啤酒"
        if girlfriend:
                if cigarette or beer:
                        sentence=sentence+"和女朋友"
                else:
                        sentence = sentence+"女朋友"
        sentence = sentence+"去看电影"
        return sentence

>>>
>>> watchMovie()
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
    watchMovie()
File "<pyshell#15>", line 3, in watchMovie
    if cigarette:
NameError: name 'cigarette' is not defined

青出于蓝 发表于 2021-8-2 10:50:57

cigaertte=True改成cigarette==True

青出于蓝 发表于 2021-8-2 10:51:31

def watchMovie(name="小甲鱼",cigarette=True,beer=True,girlfirend=True):
      sentence = name+"带着"
      if cigarette:
                sentence = sentence+"香烟"
      if beer:
                sentence = sentence+"啤酒"
      if girlfriend:
                if cigarette or beer:
                        sentence=sentence+"和女朋友"
                else:
                        sentence = sentence+"女朋友"
      sentence = sentence+"去看电影"
      return sentence
页: [1]
查看完整版本: 为什么我输入了函数报错了