繁体   English   中英

从 Python 3 中的变量中减去值时遇到问题

[英]Having problems with subtracting a value from a variable in Python 3

在你生我的气之前,我是 python 的新手。 我正在尝试制作基于文本的游戏。 我拥有的卫生系统无法正常工作。 我试图修复它,但我认为我知道的不足以修复它。 我遇到的问题是当我想降低健康时。 当我降低它并没有降低我给出的值而是降低了

这是工作的部分代码,因为我没有尝试减去任何值。 代码生成 object,然后代码将添加到 Total_health 变量以使 Total health 为 100。

Total_health = 0

class Rover:
  def __init__(self, broken_camera, dirty_solar_panels, system_failure, broken_solar_panels, broken_wheels, signal_interference, battery, bearing_failure, arm, xray, motor_failure):
    self.broken_camera = broken_camera
    self.dirty_solar_panels = dirty_solar_panels
    self.system_failure = system_failure
    self.broken_solar_panels = broken_solar_panels
    self.broken_wheels = broken_wheels
    self.signal_interference = signal_interference
    self.battery = battery
    self.arm = arm
    self.xray = xray
    self.motor_failure = motor_failure

Rover = Rover(False, False, False, False, False, False, False, 100, False, False, False)
Rover.battery = 100
def lines():
  print("_____________________________________________")


def health():
  global Total_health
  if Rover.broken_camera == True:
    print("Broken Camera") 
  if Rover.dirty_solar_panels == True:
    print("Dirty Solar Panels")
  if Rover.broken_solar_panels == True:
    print("Broken Solar Panels")
  if Rover.broken_wheels == True:
    print("Broken Wheels")    
  if Rover.battery < 21:
    print("Low Battery 20%")
  if Rover.arm == True:
    print("Arm Damage")
  if Rover.xray == True:
    print("Broken XRAY")
  if Rover.motor_failure == True:
    print("Moror Faliure") 
  print("Total Health:", Total_health)
  lines()


if Rover.broken_camera == True:
  Total_health -= 10
if Rover.broken_camera == False:
  Total_health += 10

if Rover.dirty_solar_panels == True:
  Total_health -= 5

if Rover.dirty_solar_panels == False:
  Total_health += 5

if Rover.broken_solar_panels == True:
  Total_health -= 20  
if Rover.broken_solar_panels == False:
  Total_health += 20  

if Rover.broken_wheels == True:
  Total_health -= 10 
if Rover.broken_wheels == False:
  Total_health += 10    

if Rover.battery < 21: 
  Total_health -= 5 
if Rover.battery > 20:
  Total_health += 5 

if Rover.arm == True:
  Total_health -= 20
if Rover.arm == False:
  Total_health += 20

if Rover.xray == True:
  Total_health -= 10
if Rover.xray == False:
  Total_health += 10

if Rover.motor_failure == True:
  Total_health -= 20
if Rover.motor_failure == False:
  Total_health += 20
  
health()

这是代码不起作用,因为我试图减去一个值。

Total_health = 0

class Rover:
  def __init__(self, broken_camera, dirty_solar_panels, system_failure, broken_solar_panels, broken_wheels, signal_interference, battery, bearing_failure, arm, xray, motor_failure):
    self.broken_camera = broken_camera
    self.dirty_solar_panels = dirty_solar_panels
    self.system_failure = system_failure
    self.broken_solar_panels = broken_solar_panels
    self.broken_wheels = broken_wheels
    self.signal_interference = signal_interference
    self.battery = battery
    self.arm = arm
    self.xray = xray
    self.motor_failure = motor_failure



Rover = Rover(False, False, False, False, False, False, False, 100, False, False, False)
Rover.battery = 100

def lines():
  print("_____________________________________________")

# here this code wont work
Rover.broken_camera = True
# it subtracts 20 insted of 10


def health():
  global Total_health
  if Rover.broken_camera == True:
    print("Broken Camera") 
  if Rover.dirty_solar_panels == True:
    print("Dirty Solar Panels")
  if Rover.broken_solar_panels == True:
    print("Broken Solar Panels")
  if Rover.broken_wheels == True:
    print("Broken Wheels")    
  if Rover.battery < 21:
    print("Low Battery 20%")
  if Rover.arm == True:
    print("Arm Damage")
  if Rover.xray == True:
    print("Broken XRAY")
  if Rover.motor_failure == True:
    print("Moror Faliure") 
  print("Total Health:", Total_health)
  lines()


if Rover.broken_camera == True:
  Total_health -= 10
if Rover.broken_camera == False:
  Total_health += 10

if Rover.dirty_solar_panels == True:
  Total_health -= 5

if Rover.dirty_solar_panels == False:
  Total_health += 5

if Rover.broken_solar_panels == True:
  Total_health -= 20  
if Rover.broken_solar_panels == False:
  Total_health += 20  

if Rover.broken_wheels == True:
  Total_health -= 10 
if Rover.broken_wheels == False:
  Total_health += 10    

if Rover.battery < 21: 
  Total_health -= 5 
if Rover.battery > 20:
  Total_health += 5 

if Rover.arm == True:
  Total_health -= 20
if Rover.arm == False:
  Total_health += 20

if Rover.xray == True:
  Total_health -= 10
if Rover.xray == False:
  Total_health += 10

if Rover.motor_failure == True:
  Total_health -= 20
if Rover.motor_failure == False:
  Total_health += 20
  
health()

我知道代码很乱,但有没有简单的方法来解决这个问题?

您已创建与 class 同名的流动站 object

Rover = Rover(False, False, False, False, False, False, False, 100, False, False, False)

这是一个大错误,因为您将无法创建任何新对象,请将此行替换为

Rover = Rover(False, False, False, False, False, False, False, 100, False, False, False)

然后更改下面所有行中的引用,例如

rover.battery = 100
rover.battery = 100

现在,由于您将流动站初始化为 False,根据您的代码,该值不会减少而是增加。

if Rover.broken_camera == True:
  Total_health -= 10
if Rover.broken_camera == False:
  Total_health += 10

为了测试您的健康状况是否降低,您已将流动站电池更改为 0

rover.battery = 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM