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

报错‘Invalid literal for int() with base 10’含义及短信功能代码问题求助

Understanding "Invalid literal for int() with base 10" Error + Fixes for Your SMS Sending Code

Let's break this down step by step, starting with the error itself, then digging into why it's showing up in your code.

What does "Invalid literal for int() with base 10" mean?

This error fires in Python when you try to convert a string that isn't a valid whole decimal number into an integer using the int() function. For example:

  • int("abc123") → Fails, since letters can't be parsed into a number
  • int("123.45") → Fails because of the decimal point (it's a float, not an integer)
  • int(" +12345") → Fails due to the leading plus sign or whitespace

Python can't translate these strings into base-10 (regular decimal) integers, so it throws this error to let you know the conversion is impossible.

Why is this happening in your SMS sending code?

Looking at your snippet, here are the most likely culprits:

  1. self.dental_pnumber contains invalid characters
    You're passing self.dental_pnumber as the mobile parameter to the SMS API. If this field has:

    • Empty string (your current check has a redundant if self.dental_pnumber: followed by if not self.dental_pnumber:—this logic is backwards and won't catch missing values properly)
    • Non-numeric characters like country codes with +, spaces, parentheses (e.g., +1 (555) 123-4567)
    • Letters or symbols

    If the SMS API expects an integer for the mobile field, or if somewhere in your code you're trying to convert this value to an int, this will trigger the error.

  2. Issues with self.SMS dictionary values
    You use self.SMS.get(message_state)[0] to fetch an attribute via getattr(). If that attribute's value is a non-numeric string, and it's being used in a context that expects an integer (like API parameters or internal processing), that could also cause the error.

  3. Malformed parameters in requests.get()
    Your requests.get(url, xxxxx) line has incomplete parameters. If any of the missing parameters include a string that's supposed to be an integer (like an API key or port number), that's another potential source of the error.

Fixes to try

  • Validate and sanitize dental_pnumber:
    Clean up the phone number to remove non-digit characters before passing it to the API. Example:
    # Strip all non-numeric characters from the phone number
    cleaned_number = ''.join(filter(str.isdigit, self.dental_pnumber))
    if not cleaned_number:
        self.raise_user_error("Invalid or missing Patient Contact Number.")
    # Use the cleaned number—convert to int only if the API explicitly requires it
    params = {
        'msg': self.SMS.get(message_state)[1],
        'mobile': cleaned_number
    }
    
  • Fix the redundant contact check:
    Your current check for missing phone numbers is backwards. Swap it to:
    if not self.dental_pnumber:
        self.raise_user_error("Missing Patient Contact Number.")
    
  • Verify self.SMS structure:
    Double-check that self.SMS['send_sms'] returns a tuple where the first element points to an attribute with a valid numeric value (if it's used in an integer-required context).
  • Complete and validate requests.get() parameters:
    Ensure all parameters passed to the SMS API are of the correct type. If the API expects mobile as an integer, only convert the cleaned string to int after confirming it's a valid numeric value.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:41:10