简体   繁体   English

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

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

Before you get mad at me, I am new to python.在你生我的气之前,我是 python 的新手。 I am trying to make a text-based game.我正在尝试制作基于文本的游戏。 The health system that I had was not working.我拥有的卫生系统无法正常工作。 I have tried to fix it but I don't think I know enough to fix it.我试图修复它,但我认为我知道的不足以修复它。 The problem that I have is when I want to lower the health.我遇到的问题是当我想降低健康时。 When I lower it doesn't lower by the value I give instead it lowered当我降低它并没有降低我给出的值而是降低了

This is the part code working because I did not try to subtract any value.这是工作的部分代码,因为我没有尝试减去任何值。 The code makes an object and then the code will add to the Total_health variable to make Total health 100.代码生成 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()

This is the code not working because I tried to subtract a value.这是代码不起作用,因为我试图减去一个值。

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()

I know the code is messy but is there an easy way to fix this?我知道代码很乱,但有没有简单的方法来解决这个问题?

You have created the rover object with the same name as the class您已创建与 class 同名的流动站 object

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

This is a big error, as you will not be able to create any new objects, replace the line with this这是一个大错误,因为您将无法创建任何新对象,请将此行替换为

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

Then change the reference in all the lines below for example然后更改下面所有行中的引用,例如

rover.battery = 100
rover.battery = 100

Now, since you initialized your rover with all aspects as False, according to your code, the value will not be reduced but increased.现在,由于您将流动站初始化为 False,根据您的代码,该值不会减少而是增加。

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

To test if you health reduces, you have change the rover battery to 0为了测试您的健康状况是否降低,您已将流动站电池更改为 0

rover.battery = 0

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

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