python - 我怎样才能从一个def跳转到另一个def
问题描述
当我输入4时,它显示Living_room2未定义,我怎样才能让它先跳转到另一个def表单。代码如下,如果你能提供帮助,不胜感激。
def Living_room():
print("\nLiving room contents: a pot of soil, stairs going up, a dark entranceway, a ball of a string")
print("1).Viewing the pot of soil")
print("2). Stairs going up")
print("3). Dark entranceway")
print("4). Pick Ball of string")
choice = input(">");
if "1" in choice:
print(" it looks dry")
Living_room()
elif "2" in choice:
room_attic()
elif "3" in choice:
room_bedroom()
elif "4" in choice:
print("picked up")
Living_room2()
Living_room()
def Living_room2():
print("\nLiving room dcontents: a pot of soil, stairs going up, a dark entranceway")
print("1).Viewing the pot of soil")
print("2). Stairs going up")
print("3). Dark entranceway")
choice = input(">");
if "1" in choice:
print(" it looks dry")
Living_room2()
elif "2" in choice:
room_attic()
elif "3" in choice:
room_bedroom2()
Living_room2()
解决方案
Python 从上到下逐行运行。因此,在您的代码中,python 将其视为...
我需要创建一个名为Living_room
. 伟大的。但是它Living_room()
在读取该行之前就命中了def Living_room2()
。所以现在 python 在Living_room()
没有设置的情况下停止并运行该调用Living_room2
。
将您的代码更改为此...
def Living_room():
# Living_room code here
elif "4" in choice:
print("picked up")
Living_room2()
def Living_room2():
# Living_room2 code here
Living_room()
推荐阅读
- ios - SwiftUI - 计算 ScrollView 中长 GeometryReader 的高度
- apache-spark - 在 pyspark 中读取、转换和输出结果的有效方法
- apache-nifi - Nifi Custom Processor - 未指定传输关系
- debugging - remix ethereum.org 中的调试器将函数返回值显示为“对象”
- bash - 不断变化的变量
- python - 在 k 个子集中拆分列表而不改变顺序
- c# - 如何指定可观察序列之间的依赖关系?
- angular - 如何使用 Angular 将动画设置为 Nebular Stepper,就像 Material 一样
- javascript - 在javascript中单击正文或窗口中的相同元素删除元素
- tensorflow - MLflow 服务于 tensorflow.keras.wrappers.scikit_learn KerasClassifier 模型