无限循环 - 为真 [Python]
Infinite Loop - While True [Python]
运行陷入无限循环的老问题...
编写一个文字冒险游戏并降低游戏机制;地图和周围的运动。 运行 当我尝试实现其他功能时出现无限循环问题 - 获取物品等
下面是 2 个代码片段,包括初始化变量、移动代码和下一阶段的内容。 “……”下面是问题所在。
所有缩进都对齐。
print("Welcome to this most intrepid" + game['name']
+ " your goal; should you dare to continue... is to" + game['goal'])
star_loc = int(game['start'])
curr_loc = star_loc
character = {'inventory': [], 'location':curr_loc}
x_loc = 1
y_loc = 2
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
while True:
command_parts = command.split()
cmd = command_parts[0]
print(cmd)
obj = command_parts[-1]
print(obj)
#move around map
if cmd == 'move' or cmd == 'go' or 'hiddenpath':
#handles movement on the X plane - East, West
elif obj == 'east':
if x_loc == int(game['xsize']): # use the x size the game dict instead, compute the x and y
curr_loc = curr_loc - 2
x_loc = 1
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
else:
curr_loc = curr_loc + 1
x_loc = x_loc + 1
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
elif obj == 'west':
if x_loc == int(game['xsize']) - 2:
curr_loc = curr_loc + 2
x_loc = x_loc = 3
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
else:
curr_loc = curr_loc - 1
x_loc = x_loc - 1
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
.................
if cmd == 'exit':
print('Goodbye')
break
if cmd == 'inv':
print('You are carrying')
for item in character['inventory']:
print('', item)
continue
if cmd == 'goal':
print(game['goal'])
else:
continue
if cmd == 'take':
if obj in map[curr_loc]['obj']:
print("You take the " + str(map[curr_loc]['obj'][0]))
map[curr_loc]['obj'].remove(obj)
character['inventory'].append(obj)
continue
大拖...└[∵┌]└[∵]┘[┐∵]┘
在线
if cmd == 'move' or cmd == 'go' or 'hiddenpath':
改为
if cmd == 'move' or cmd == 'go' or cmd == 'hiddenpath':
我仔细阅读了您的代码并提出了以下建议:
确保您的 sscce 可以运行。具体来说,你没有告诉我们游戏是什么样的。
我会在 ['move'、'go'、'hiddenpath' 中使用“cmd”,而不是多个相等条件,当你得到更多的子句时会变得非常麻烦]".
日志记录模块是您的朋友,而不是在您的代码中到处放置打印语句。
y_loc = y_loc
是空操作。摆脱它。
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
不需要在每个子句中,在 if/elif 语句链之后。
运行陷入无限循环的老问题...
编写一个文字冒险游戏并降低游戏机制;地图和周围的运动。 运行 当我尝试实现其他功能时出现无限循环问题 - 获取物品等
下面是 2 个代码片段,包括初始化变量、移动代码和下一阶段的内容。 “……”下面是问题所在。
所有缩进都对齐。
print("Welcome to this most intrepid" + game['name']
+ " your goal; should you dare to continue... is to" + game['goal'])
star_loc = int(game['start'])
curr_loc = star_loc
character = {'inventory': [], 'location':curr_loc}
x_loc = 1
y_loc = 2
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
while True:
command_parts = command.split()
cmd = command_parts[0]
print(cmd)
obj = command_parts[-1]
print(obj)
#move around map
if cmd == 'move' or cmd == 'go' or 'hiddenpath':
#handles movement on the X plane - East, West
elif obj == 'east':
if x_loc == int(game['xsize']): # use the x size the game dict instead, compute the x and y
curr_loc = curr_loc - 2
x_loc = 1
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
else:
curr_loc = curr_loc + 1
x_loc = x_loc + 1
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
elif obj == 'west':
if x_loc == int(game['xsize']) - 2:
curr_loc = curr_loc + 2
x_loc = x_loc = 3
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
else:
curr_loc = curr_loc - 1
x_loc = x_loc - 1
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
.................
if cmd == 'exit':
print('Goodbye')
break
if cmd == 'inv':
print('You are carrying')
for item in character['inventory']:
print('', item)
continue
if cmd == 'goal':
print(game['goal'])
else:
continue
if cmd == 'take':
if obj in map[curr_loc]['obj']:
print("You take the " + str(map[curr_loc]['obj'][0]))
map[curr_loc]['obj'].remove(obj)
character['inventory'].append(obj)
continue
大拖...└[∵┌]└[∵]┘[┐∵]┘
在线
if cmd == 'move' or cmd == 'go' or 'hiddenpath':
改为
if cmd == 'move' or cmd == 'go' or cmd == 'hiddenpath':
我仔细阅读了您的代码并提出了以下建议:
确保您的 sscce 可以运行。具体来说,你没有告诉我们游戏是什么样的。
我会在 ['move'、'go'、'hiddenpath' 中使用“cmd”,而不是多个相等条件,当你得到更多的子句时会变得非常麻烦]".
日志记录模块是您的朋友,而不是在您的代码中到处放置打印语句。
y_loc = y_loc
是空操作。摆脱它。command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
不需要在每个子句中,在 if/elif 语句链之后。