【发布时间】:2020-06-25 10:05:16
【问题描述】:
我想将某个函数链接到一个特定函数,该函数只有在前面的函数在 python 中成功执行时才会运行,并且链接的函数不应该在其他地方访问。
示例:我希望仅在名称函数成功运行时才运行问候函数。
def name():
name = str(input("please enter your name: "))
while name not in (""," "):
return name
def greeting():
message = "hello {}, welcome".format(name)
print(message)
return message
def happy():
mood = str(input("hello {},this is to check if you are happy, kindly answer 'yes' or 'no': ".format(name))
if mood == "yes":
reply = "that's good"
else:
reply = "oh dear, cheer up"
print(reply)
else:
break
info = name()
print(info)
这段代码应该只在name函数执行成功的情况下运行greeting函数并调用happy函数,并且如果没有name函数也不应该调用greeting或happy函数。
但它没有按预期执行。它只返回名称。
提前致谢
【问题讨论】:
标签: python function while-loop