You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python ValueError错误求助:sign必须为0或1的整数

Fixing ValueError: sign must be an integer with the value 0 or 1 in your Python code

Let's break down what's causing this error and how to fix it quickly.

The Root Cause

Your error stems from a misplaced order of operations in the line calculating data["balance"]:

data["balance"] = "{}".format(int(data["balance"]) + int(items["1"]["deploy_cost"]) * int(soldier) * random.choice(Decimal(random_percent_j)))

Here, you’re passing the entire list random_percent_j directly to Decimal(), then trying to use that Decimal object as input for random.choice()—that’s backwards!

  • random.choice() needs an iterable (like your list of percentage strings) to pick a single element from.
  • Decimal() requires a single numeric value or string (not a whole list) to convert into a precise decimal type.

When you feed a list to Decimal(), it tries to parse the list as a numeric input, which triggers that confusing "sign must be 0 or 1" error—it’s just Decimal’s way of saying "I don’t recognize this input format."

The Fix

Rearrange the order: first pick a random percentage string from your list, then convert it to a Decimal. Also, using Decimal consistently for all calculations will prevent precision issues (instead of mixing ints and Decimals):

# Step 1: Pick a random percentage string from the list
selected_percent = Decimal(random.choice(random_percent_j))

# Step 2: Calculate the new balance using Decimal for all values to avoid precision loss
current_balance = Decimal(data["balance"])
deploy_cost = Decimal(items["1"]["deploy_cost"])
soldier_count = Decimal(soldier)

new_balance = current_balance + deploy_cost * soldier_count * selected_percent

# Step 3: Convert back to string to store in JSON (or keep as Decimal if your JSON handling supports it)
data["balance"] = str(new_balance)

Bonus: Clean Up Repetitive Code

You have a lot of repeated lines for updating unit counts. Simplify this with a loop to make your code cleaner and less error-prone:

# Map unit names to their respective count variables
unit_mappings = [
    ("soldier", soldier),
    ("sniper", sniper),
    ("demolitionist", demolit),
    ("spy", spy),
    ("armored_vehicle", armored_vehicle),
    ("tank", tank)
]

for unit_name, unit_count in unit_mappings:
    # Get current count, subtract deployed units, update the data
    current_count = int(data[f"{unit_name}#"])
    data[f"{unit_name}#"] = str(current_count - int(unit_count))

Quick Note on JSON Data

Storing all values as strings in your JSON leads to constant type conversion headaches. If possible, store numeric values as integers/floats in the JSON file—this way you won’t need to wrap everything in int() or Decimal() every time you read/write data.

内容的提问来源于stack exchange,提问作者Novial

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:52:44