根据用户输入,将列表项中的项仅移动一个空格



下面的代码是字母"a"只能向右移动和字母"b"可以向左移动之间的游戏的实现

list1=[‘a’,’a’,’a’, ‘ ‘,’b’,’b’,’b’]
fr=int(input("Start Position"))
to=int(input("End Position"))
if list1[fr-1]=='a' and list1[to-1]==' ':
if list1[fr-1]=='b'and list[to-1]==' '
#swap code
list1[fr-1],list1[to-1]=list1[to-1],list1[fr-1]

游戏规则

  • 不能交换多次空间

一个空格就是我的意思:原始列表:list1=[‘a’,’a’,’a’, ‘ ‘,’b’,’b’,’b’]

如果用户输入from:1to:4转换看起来是这样的,但是不应该执行,并且错误消息应该出现在

[‘ ‘,’a’,’a’,’a’,’b’,’b’,’b’]->#这个代码不应该发生,因为它不是一次空间

这就是可能发生的情况

所以它可以采用以下格式:

[‘a’,’a’,’a’, ‘ ‘,’b’,’b’,’b’]#原单

用户输入

from:3

to:4

到此

[‘a’,’a’,’ ‘,‘a’,’b’,’b’,’b’]

同样的规则也适用于蛙式"b">

另一种可能发生的情况是[‘a’,’a’,’a’, ‘ ‘,’b’,’b’,’b’]#原始列表

用户输入from:6to:4

[‘a’,’a’,’a’, ‘b‘,’b’,’ ’,’b’]

所以有人能帮助如何编码吗

我在您提出的上一个问题中给出了答案。你没有做出任何努力,也没有按照你提出的问题的答案去做。这可能是你的家庭作业或家庭作业的一部分,你只是在等待解决方案。我希望你能分享你的作品,然后提出问题,这对你的进步会更好。

我再次分享我的答案,因为也许它可以帮助其他人,并且之前的问题被删除了。我希望这个答案能奏效。

请在下次提问之前分享你的工作,并遵循你的问题和他们的答案,否则你可能无法解决你的问题。

puzzle =["a","a","a"," ","b","b","b"]
## dont forget your input will start 0
## because you will pick an index
def move(puzzle):
move_index = int(input("enter an index for move right"))
for i in puzzle:
if move_index < len(puzzle):
if puzzle[move_index + 1] == " " and puzzle[move_index] == "a":
puzzle[move_index],puzzle[move_index+1] = puzzle[move_index +1 ],puzzle[move_index] ## if right index is free,move
return puzzle
if puzzle[move_index - 1] == " " and puzzle[move_index] == "b" :
puzzle[move_index], puzzle[move_index - 1] = puzzle[move_index - 1], puzzle[move_index]
return puzzle
if puzzle[move_index - 2] == " " and puzzle[move_index-1] == "a" and puzzle[move_index] == "b":
puzzle[move_index], puzzle[move_index - 2] = puzzle[move_index - 2], puzzle[move_index]
return puzzle
if puzzle[move_index + 2] == " " and puzzle[move_index + 1 ] == "b" and puzzle[move_index] == "a" :
puzzle[move_index], puzzle[move_index + 2] = puzzle[move_index + 2], puzzle[move_index]
return puzzle
if move_index == len(puzzle): ## you can move last element only for your conditons
puzzle.append(puzzle.pop(move_index-1)) ## switch for last and first
puzzle.insert(0, puzzle.pop())
return puzzle ## updated puzzle
else:
return puzzle
def game():
is_game_continue = int(input("Do you want continue ? (1) Yes (2) No")) ## for enter new moves
return is_game_continue
while game() == 1: ##  if user want continue
## you can add other options like return value != 1
current_puzzle = move(puzzle)
print(current_puzzle)

你对规则有所有的想法,所以我认为你可以自己完成游戏。这个答案可以作为你整理想法的参考。

start_puzzle = ['a', 'a', 'a', ' ', 'b', 'b', 'b']
end_puzzle = ['b', 'b', 'b', ' ', 'a', 'a', 'a']
print(start_puzzle)
while start_puzzle != end_puzzle:
# -1 to get index
from_idx = int(input("Move from position: ")) - 1
to_idx = int(input("Move to position: ")) - 1
# positive step from left to right and negative step from right to left
step = to_idx - from_idx
if 0 <= from_idx < len(start_puzzle) and 0 <= to_idx < len(start_puzzle):
if start_puzzle[from_idx] == "a" and step < 0 or start_puzzle[from_idx] == "b" and step > 0:
print(f"'{start_puzzle[from_idx]}' cannot move backwards.")
elif start_puzzle[to_idx] != ' ':
print("Can only move to empty space.")
elif step > 2 or step < -2:
print("Cannot swap more than one space.")
else:
start_puzzle[from_idx], start_puzzle[to_idx] = start_puzzle[to_idx], start_puzzle[from_idx]
else:
print("Position out of range.")
print(start_puzzle)
if start_puzzle == end_puzzle:
print("You Won!!")

测试结果:

['a', 'a', 'a', ' ', 'b', 'b', 'b']
Move from position: 1
Move to position: 8
Position out of range.
['a', 'a', 'a', ' ', 'b', 'b', 'b']
Move from position: 1
Move to position: 4
Cannot swap more than one space.
['a', 'a', 'a', ' ', 'b', 'b', 'b']
Move from position: 3
Move to position: 5
Can only move to empty space.
['a', 'a', 'a', ' ', 'b', 'b', 'b']
Move from position: 3
Move to position: 4
['a', 'a', ' ', 'a', 'b', 'b', 'b']
Move from position: 4
Move to position: 3
'a' cannot move backwards.
['a', 'a', ' ', 'a', 'b', 'b', 'b']
Move from position: 5
Move to position: 3
['a', 'a', 'b', 'a', ' ', 'b', 'b']
Move from position: 3
Move to position: 5
'b' cannot move backwards.
.
.
.
['b', 'b', 'b', 'a', ' ', 'a', 'a']
Move from position: 4
Move to position: 5
['b', 'b', 'b', ' ', 'a', 'a', 'a']
You Won!!

您不应该使用from作为变量,因为它是python中的保留关键字。

也许这就是造成你问题的原因。

最新更新