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

编写检测两数字相同数位位置的Python程序并修复数位标识错误

Fixing Your Digit Matching Program

Let's break down what's wrong with your current code first, then fix it step by step:

Key Issues in the Original Code

  • Double loops cause redundant output: You're looping through every character in both strings, so even a single mismatch will print "No digits are same" multiple times—this isn't what we want.
  • index() returns wrong positions: The index() method only gives the first occurrence of a character. For example, if your number has duplicate digits like 4 in 1234453, ns1.index('4') will always return 3, not the other position of 4.
  • 数位位置计算颠倒: Your code treats the leftmost digit (highest place value) as position 0, but we need to start from the rightmost digit (1's place) instead. That's why you got "3th position" instead of "100th position"—the position mapping was backwards.

Fixed Code

n1 = int(input())
n2 = int(input())

# Convert numbers to strings and reverse them to start from 1's place
s1 = str(n1)[::-1]
s2 = str(n2)[::-1]

# Track if any matching digits were found
found_match = False

# Iterate through each position (starting from 0 = 1's place)
for i in range(max(len(s1), len(s2))):
    # Get the digit at current position, or None if out of bounds
    digit1 = s1[i] if i < len(s1) else None
    digit2 = s2[i] if i < len(s2) else None
    
    if digit1 == digit2 and digit1 is not None:
        found_match = True
        # Calculate the place value (10^i)
        place_value = 10 ** i
        # Format the place name correctly
        if place_value == 1:
            place_name = "1's position"
        else:
            place_name = f"{place_value}th position"
        print(f"Same at {place_name}")

# If no matches found after checking all positions
if not found_match:
    print("No digits are same in corresponding positions")

How This Works

  1. Reversing the strings: By reversing str(n1) and str(n2), the 0th index now corresponds to the 1's place, index 1 to 10th place, index 2 to 100th place, etc.—this aligns with how we count digit positions from right to left.
  2. Handling different lengths: We loop up to the length of the longer number, and use None to mark positions where one number has no digit (so we skip those).
  3. Correct place naming: We calculate the actual place value (10^i) and format the name properly (like "1's position" instead of "1th position").
  4. Single match check: We only print "No digits are same" once at the end if no matches were found, instead of spamming it for every mismatch.

Testing with your example: n1=1234453 and n2=2444853

  • Reversed s1: 3544321, reversed s2: 3584442
  • Comparing each index:
    • i=0: '3' == '3' → Same at 1's position
    • i=1: '5' == '5' → Same at 10th position
    • i=3: '4' == '4' → Same at 1000th position
      Which gives exactly the output you expected!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 19:02:48