【问题标题】:Why doesn't the output value show when the terms are appropiate为什么在条件合适时输出值不显示
【发布时间】:2020-03-21 00:54:26
【问题描述】:

我希望它在 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) 

【问题讨论】:

  • 顺便说一句,类名应该遵循CamelCase 样式。

标签: python input location


【解决方案1】:

在开始 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 2023-02-03
    • 1970-01-01
    • 2018-02-27
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多