python-3.x - 依次调用函数内部的函数
问题描述
这周我开始学习 python,它是我的第一语言,这是我的第一个问题。我一直在努力自学,这是我目前正在研究的项目之一。
结果应该是:
当您使用其中一个箭头键(让我们打电话,做出选择)时,它应该打印一条消息并调用一个导致做出其他选择的函数,依此类推。
游戏的目的是在房子周围走动,这意味着您可以按顺序进入房间,甚至可以回到上一个房间。
如果我没有说清楚,请提前道歉。
我只发布了代码的一部分(这就像一个基本部分),所以我可以知道它是如何完成的,并让其他部分工作。
当我运行我的代码时,我只能从函数 on_press_outside 获得输出,当我输入时它不会跳转到下一个。所以我看起来像我被困在那个功能中。例如,当我按下向上箭头键时,在“on_press_outside”上它应该引导我到“on_press_hall”,在“on_press_hall”中,我现在应该能够按下右箭头键,并且会引导我“on_press_living”等等。在简历中,在这段代码中,我被困在“on_press_outside”,我无法跳转到下一个函数,如果输入重复,打印甚至会重复。
谢谢你!
from pynput.keyboard import Key, Listener
print("Welcome!")
print("Here, you will walk through my house")
print("\nNow, You'll find yourself outside")
print("To return to your previous room, press the Down arrow key")
print("Are you going to the entrance hall(up), or the garage(right)?")
def on_press_outside(key):
while True:
if key == Key.up:
print("You are now at the entrance hall, do you want to go to the kitchen(left) or to the living room(right)")#print the message that appears when select the Hall
on_press_hall(key) #means you entered the Hall and can now
break #choose the possibilities of the function on_press_hall
elif key == Key.right:
print("You are now at the garage, do you want to go the kitchen(right) or to the bathroom(up)")
#on_press_garage(key) --> I have not created this one yet
break
elif key == Key.down:
print("You are oustide, are you going to the entrance hall(up), or the garage(right)?")
on_press_outside(key) #go back to the starting function, so the choice can be made again
break
else:
print("Please, use the correct arrow keys on your keyboard!")
break
def on_press_hall(key):
while True:
if key == Key.right:
print("You are now at the living room, do you want to stay and watch some TV(Up, This ends the game), or do you want to go back to the Hall(down)")
#on_press_living(key) --> I have not created this function yet
break
elif key == Key.left:
print("You are at the kitchen, are you going to the garden(up) or the laundry room(right)?")
#on_press_kitchen(key) --> I have not created this function yet
break
elif key == Key.down:
print("You are oustide, are you going to the entrance hall(up), or the garage(right)?")
on_press_outside(key) #go back to the starting function, so the choice can be made again
break
else:
print("Please, use the correct arrow keys on your keyboard!")
break
with Listener(
on_press1=on_press_hall,
on_press=on_press_outside) as listener:
listener.join()
解决方案
推荐阅读
- javascript - 反应本机 FlatList/VirtualizedList 从顶部删除项目而无需滚动位置更改和无限加载
- swift - 悬停时显示标题栏分隔符?(苹果系统)
- java - Android:有没有办法检测状态栏上的触摸?即使使用 FLAG_LAYOUT_NO_LIMITS?
- android - Codemagic颤振无法构建android
- angular - 具有折叠内容的可调整大小的角材料抽屉
- express - express windows-service 停止运行
- flutter - 如何在我存储在 sqlite 数据库中的颤振中显示图像
- python-3.x - 在命名空间实例化 flask_restx 上添加 doc 装饰器
- java - 按另一个列表对列表进行排序
- python - Cloudera 应用程序上的 Django 托管:服务器未启动