簡體   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