Почему выходное значение не отображается, когда условия соответствуют - PullRequest
0 голосов
/ 21 марта 2020

Я хочу напечатать сообщение и изменить некоторые значения, когда значения mario.location равны значения coin.location в python. Почему он этого не делает? Это просто печать обычного ввода. Который просто печатает значения местоположения.

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) 

1 Ответ

0 голосов
/ 21 марта 2020

Вы сравниваете значения только один раз перед тем, как запустить l oop. Вы должны сравнивать значения на каждой итерации после выбора команды.

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
...