为什么术语适当时输出值不显示

问题描述 投票:0回答:1

我希望它在mario.location值等于python中的coin.location值时打印一条消息并更改一些值。为什么不这样做呢?它只是打印常规输入。这只是打印位置值。

class mario:
  x_location = 0 
  y_location = 0 
  z_location = 0 
  health = 3


class coin:
  x = 1
  y = 0 
  z = 1 

rules = [mario.x_location == coin.x, 
  mario.y_location == coin.y, 
  mario.z_location == coin.z]

start = input('say yes: ').lower() 
while start == 'yes':
  command = input('').lower()
  if command == 'w':
    mario.x_location += 1 
    print(mario.x_location, mario.y_location, mario.z_location)

  elif command == 's':
    mario.x_location -= 1
    print(mario.x_location, mario.y_location, mario.z_location)

  elif command == 'a':
    mario.z_location -= 1 
    print(mario.x_location, mario.y_location, mario.z_location) 

  elif command == 'd':
    mario.z_location += 1 
    print(mario.x_location, mario.y_location, mario.z_location)

  elif all(rules):
    if mario.health == 3:
      print('you collected a coin')
      print('health: ', mario.health)
    elif mario.health < 3:
      print('You collected a coin and healed')
      mario.health += 1
      print('Health: ', mario.health) 
python input location
1个回答
0
投票

甚至在启动while循环之前,您只比较了一次值。选择命令后,应该在每次迭代时都比较这些值。

while start == 'yes':
  command = input('').lower()
  if command == 'w':
    mario.x_location += 1 
    print(mario.x_location, mario.y_location, mario.z_location)

  elif command == 's':
    mario.x_location -= 1
    print(mario.x_location, mario.y_location, mario.z_location)

  elif command == 'a':
    mario.z_location -= 1 
    print(mario.x_location, mario.y_location, mario.z_location) 

  elif command == 'd':
    mario.z_location += 1 
    print(mario.x_location, mario.y_location, mario.z_location)

  rules = [mario.x_location == coin.x, 
  mario.y_location == coin.y, 
  mario.z_location == coin.z]

  if all(rules):
    if mario.health == 3:
      print('you collected a coin')
      print('health: ', mario.health)
    elif mario.health < 3:
      print('You collected a coin and healed')
      mario.health += 1
      print('Health: ', mario.health)

样品运行:

say yes: yes
w
1 0 0
a
1 0 -1
d
1 0 0
d
1 0 1
you collected a coin
health:  3
© www.soinside.com 2019 - 2024. All rights reserved.