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

Python新手求助:修复TypeError: int与tuple相加不支持错误

Fixing Your Python Code: TypeError and Other Common Issues

Hey there! Let's sort out your code issues one by one—you're close, just a few syntax and logic tweaks needed.

First, the Root Cause of Your TypeError

The error TypeError: unsupported operand type(s) for +: 'int' and 'tuple' happens because of how you wrote decimal numbers. In Brazilian Portuguese we use commas for decimals, but Python only recognizes dots (.) as decimal separators.

When you wrote IP = 0,3*PC, Python interprets 0,3 as a tuple ((0, 3)), not a float. So IP ends up being a tuple instead of a number, and you can't add an integer (PC) to a tuple. That's exactly where the error comes from.

Other Issues to Fix

  1. Incorrect Indentation: Your if-else blocks aren't properly indented, which breaks the logic. For example, if CC is invalid (not 10/20/30), the code still tries to run the CE checks—this would cause a NameError because PC isn't defined.
  2. Unnecessary Semicolons: Python doesn't require semicolons at the end of lines (unless you're putting multiple statements on one line). They're harmless here but make the code harder to read.
  3. Redundant Commas in print(): The trailing commas in your print statements add extra spaces; we can clean those up.

Fixed Code

Here's the corrected version with comments explaining the changes:

# Get input values (removed unnecessary semicolons)
CC = int(input("Informe o código de carga do caminhão: "))
PT = int(input("Informe o peso do caminhão em toneladas: "))
CE = int(input("Informe o código de Estado referente ao estado de origem do caminhão: "))

P = PT * 1000
print("O peso em quilos do caminhão é de", P, "kg")

# Properly indented if-else block for CC
if CC == 10:
    PC = 100 * P
elif CC == 20:
    PC = 200 * P
elif CC == 30:
    PC = 300 * P
else:
    print("Código de carga inválido")
    # Exit early if CC is invalid to avoid errors later
    exit()  # Or handle this case as you prefer

# Fixed decimal separators (used . instead of ,) and indented CE block
if CE == 1:
    IP = 0.3 * PC  # Changed 0,3 to 0.3
elif CE == 2:
    IP = 0.1 * PC  # Changed 0,1 to 0.1
elif CE == 3:
    IP = 0
else:
    print("Código de Estado inválido")
    exit()

PR = PC + IP
print("O preço da carga é de R$", PC)
print("O imposto cobrado foi de R$", IP)
print("O preço total é de R$", PR)

Key Changes Explained

  • Replaced all , with . in decimal values (e.g., 0,3 → 0.3) so Python treats them as floats instead of tuples.
  • Used elif instead of nested else if for cleaner, more readable logic.
  • Added proper indentation to ensure each condition's code only runs when the condition is met.
  • Added exit() in invalid code cases to prevent the rest of the code from running with undefined variables (you could also use a flag variable if you prefer not to exit).
  • Removed unnecessary semicolons and trailing commas in print statements.

Give this a try—it should fix the TypeError and handle edge cases better!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:06:01