报错‘Invalid literal for int() with base 10’含义及短信功能代码问题求助
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 numberint("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:
self.dental_pnumbercontains invalid characters
You're passingself.dental_pnumberas themobileparameter to the SMS API. If this field has:- Empty string (your current check has a redundant
if self.dental_pnumber:followed byif 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
mobilefield, or if somewhere in your code you're trying to convert this value to an int, this will trigger the error.- Empty string (your current check has a redundant
Issues with
self.SMSdictionary values
You useself.SMS.get(message_state)[0]to fetch an attribute viagetattr(). 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.Malformed parameters in
requests.get()
Yourrequests.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.SMSstructure:
Double-check thatself.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 expectsmobileas an integer, only convert the cleaned string to int after confirming it's a valid numeric value.
内容的提问来源于stack exchange,提问作者akash soni

