我真的不知道如何准确地解释这一点。但我正在为学习基础知识进行文字冒险。现在我想制作一个黄金和货币系统,我正在使用 def... 不同级别的东西等等。但是,如果用户键入黄金,我必须在每个提示中输入,或者 inv 它显示库存,然后返回到它所在的 def.. 我每次都觉得这样做很烦人。我有时也会忘记它。我想在提示中默认这样做。我有一个 def prompt(): 就是这个简单的代码:
def prompt():
x = input('Type a command: ')
return x
如果我把它放在那里,它只会结束代码。我必须在每个提示中执行此操作:
def AlleenThuis():
command = prompt()
if command == '1':
print()
elif command == '2':
print()
elif command == '3':
print()
elif command == '4':
print()
elif command == 'geld': #Actions start here
print('\n\tYou have ' + str(gold) + ' euro. RICH BOY BRO!.\n')
print()
return AlleenThuis()
elif command == 'inv':
if not inv:
print("\n\tYou don't have any items..\n")
return AlleenThuis()
else: #The else has to stay in this place because it's part of the 'if not inv:' guys.
print('\n\t' + str(inv) + '\n')
return AlleenThuis()
#Actions end here
因此,如果有任何方法可以实现它,这样我就不必每次都重新放入它,那就太棒了!谢谢。
编辑:看起来你们不明白我在说什么,所以我有 2 张图片。所以.. http://i.imgur.com/GLArsyu.png(我还不能发布图片=[)
正如你在这张图片中看到的,我已经包括了黄金和投资。但是在http://i.imgur.com/V3ZhJ36.png我也这样做了,所以我再次在代码中编码,这就是我不想要的!
我只想在代码 1 中使用它,并在玩家为他们输入命令时让金币和库存一直显示!