Python代码在一个编辑器中工作,而不是在另一个编辑器中工作

Python代码在一个编辑器中工作,而不是在另一个编辑器中工作,python,string,numbers,syntax-error,addition,Python,String,Numbers,Syntax Error,Addition,我是学习Python的新手。在这段代码中,我尝试添加两个数字字符串。在我的编辑PyCharm的第13行,我写了下面的陈述。它不起作用。我试着写作 求和:int=int(x)+int(y)+int(剩余) 它成功了。然而,在leetcode上,它不起作用。它说第14行有一个无效的语法。在leetcode中,我也是用Python编写的,而不是Python3 class Solution(object): def addStrings(num1, num2): last = &

我是学习Python的新手。在这段代码中,我尝试添加两个数字字符串。在我的编辑PyCharm的第13行,我写了下面的陈述。它不起作用。我试着写作 求和:int=int(x)+int(y)+int(剩余)


它成功了。然而,在leetcode上,它不起作用。它说第14行有一个无效的语法。在leetcode中,我也是用Python编写的,而不是Python3

class Solution(object):
    def addStrings(num1, num2):
        last = ""
        left_over = 0
        i = len(num1) - 1
        j = len(num2) - 1
        while (i >= 0) or (j >= 0):
            x = 0
            y = 0
            if i >= 0:
                x = num1[i]
            if j >= 0:
                y = num2[j]
            **sumation = x + y + left_over
            left_over = sumation // 10
            last = str(sumation % 10) + last
            i -= 1
            j -= 1
        if left_over > 0:
            last = str(left_over) + last
        return last
    

这一行有一个小错误

**sumation = x + y + left_over
我猜你会想做一些类似的事情:

summation = int(x) + int(y) + left_over
另外,您可能忘记在
addStrings()
中传递
self

刚刚测试了您的解决方案,通过LeetCode:

class Solution(object):
    def addStrings(self, num1, num2):
        last = ""
        left_over = 0
        i = len(num1) - 1
        j = len(num2) - 1
        while (i >= 0) or (j >= 0):
            x = 0
            y = 0
            if i >= 0:
                x = num1[i]
            if j >= 0:
                y = num2[j]

            summation = int(x) + int(y) + left_over
            left_over = summation // 10
            last = str(summation % 10) + last
            i -= 1
            j -= 1
        if left_over > 0:
            last = str(left_over) + last
        return last


print(Solution().addStrings("100", "500"))

印刷品

600

这也行:

class Solution:
    def addStrings(self, num1, num2):
        length1, length2 = len(num1), len(num2)
        reversed_num1, reversed_num2 = num1[::-1], num2[::-1]
        max_length = max(length1, length2)
        added_num = ''
        carry = index = 0
        while index < max_length or carry:
            digit1 = int(reversed_num1[index]) if index < length1 else 0
            digit2 = int(reversed_num2[index]) if index < length2 else 0
            added_digit = (digit1 + digit2 + carry) % 10
            carry = (digit1 + digit2 + carry) // 10
            added_num += str(added_digit)
            index += 1

        return added_num[::-1]

工具书类
  • 有关更多详细信息,请参阅,您可以在其中找到大量解释良好的公认解决方案,包括低复杂度算法和渐近/分析

“它表示存在无效语法”-请提供完整的错误消息
**sumation=…
无效语法。请从
**sumation=…
中删除
**
?谢谢。但我有一个小问题,如果我已经将x和y初始化为0,为什么我们需要将它们强制转换为x和y,Python难道不知道它们是整数吗?
class Solution:
    def addStrings(self, num1, num2):
        length1, length2 = len(num1), len(num2)
        reversed_num1, reversed_num2 = num1[::-1], num2[::-1]
        max_length = max(length1, length2)
        added_num = ''
        carry = index = 0
        while index < max_length or carry:
            digit1 = int(reversed_num1[index]) if index < length1 else 0
            digit2 = int(reversed_num2[index]) if index < length2 else 0
            added_digit = (digit1 + digit2 + carry) % 10
            carry = (digit1 + digit2 + carry) // 10
            added_num += str(added_digit)
            index += 1

        return added_num[::-1]
from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue

from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter


class Solution(object):
    def addStrings(self, num1, num2):
        last = ""
        left_over = 0
        i = len(num1) - 1
        j = len(num2) - 1
        while (i >= 0) or (j >= 0):
            x = 0
            y = 0
            if i >= 0:
                x = num1[i]
            if j >= 0:
                y = num2[j]

            summation = int(x) + int(y) + left_over
            left_over = summation // 10
            last = str(summation % 10) + last
            i -= 1
            j -= 1
        if left_over > 0:
            last = str(left_over) + last
        return last


print(Solution().addStrings("100", "500"))  # 600
# print(Solution().()) #
# print(Solution().()) #
# print(Solution().()) #
# print(Solution().()) #