【发布时间】:2017-10-27 02:52:59
【问题描述】:
我是编码初学者,我正在制作一个简单的 rpg。我目前正在制作只有 4 个房间的地板。我希望游戏无限循环。我还希望游戏在玩家输入无效命令时提示玩家输入有效命令,但 while 循环会无限重复。有人可以给我一个初学者可以理解的修复程序,也许还有一些指示? while 循环是while foo(move) == False:。
def foo(m):
"""
"""
if m.lower == 'l' or m.lower == 'r' or m.lower == 'help' or m.lower == 'pokemon':
return True
else:
return False
print() #Put storyline here
print("You may input 'help' to display the commands.")
print()
gamePlay = True
floor4 = ['floor 4 room 4', 'floor 4 room 3', 'floor 4 room 2', 'floor 4 room 1']
floor4feature = ['nothing here', 'nothing here', 'stairs going down', 'a Squirtle']
currentRoom = 0
pokemonGot = ['Bulbasaur']
while gamePlay == True:
print("You are on " + floor4[currentRoom] + ". You find " + floor4feature[currentRoom] + ".")
move = input("What would you like to do? ")
while foo(move) == False:
move = input("There's a time and place for everything, but not now! What would you like to do? ")
if move.lower() == 'l':
if currentRoom > 0:
currentRoom = currentRoom - 1
print("Moved to " + floor4[currentRoom] + ".")
else:
print("*Bumping noise* Looks like you can't go that way...")
elif move.lower() == 'r':
if currentRoom < len(floor4) - 1:
currentRoom = currentRoom + 1
print("Moved to " + floor4[currentRoom] + ".")
else:
print("*Bumping noise* Looks like you can't go that way...")
elif move.lower() == 'help':
print("Input 'r' to move right. Input 'l' to move left. Input 'pokemon' to see what Pokemon are on your team. Input 'help' to see the commands again.")
elif move.lower() == 'pokemon':
print("The Pokemon on your team are: " + str(pokemonGot) + ".")
print()
【问题讨论】:
-
您必须像这样实际调用
m.lower方法:m.lower()。 python 中的函数和方法本身就是对象,您试图直接将其与字符串进行比较 -
foo 中的 m.lower 应该是 m.lower() 吗?
标签: python-3.x while-loop