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

如何合并Python双金字塔代码,实现输入1-9整数输出指定双金字塔?

Combined Pyramid Program

Here's the combined code that prompts for a single 1-9 integer and displays both pyramids one after another:

# Get valid input between 1-9
while True:
    try:
        rows = int(input("Enter an integer between 1 and 9: "))
        if 1 <= rows <= 9:
            break
        print("Input must be between 1 and 9. Please try again.")
    except ValueError:
        print("Invalid input. Please enter an integer.")

# Display star pyramid
print("\nStar Pyramid:")
k = 0
for i in range(1, rows + 1):
    # Print leading spaces
    for space in range(1, (rows - i) + 1):
        print(end="  ")
    # Print stars
    while k != (2 * i - 1):
        print("* ", end="")
        k += 1
    k = 0
    print()

# Display number pyramid
print("\nNumber Pyramid:")
k = 0
count = 0
count1 = 0
for i in range(1, rows + 1):
    # Print leading spaces
    for space in range(1, (rows - i) + 1):
        print("  ", end="")
        count += 1
    # Print numbers
    while k != ((2 * i) - 1):
        if count <= rows - 1:
            print(i + k, end=" ")
            count += 1
        else:
            count1 += 1
            print(i + k - (2 * count1), end=" ")
        k += 1
    count1 = count = k = 0
    print()

Key Changes Made:

  • Single Input Prompt: Replaced duplicate input() calls with a validated loop to ensure the user enters a valid 1-9 integer.
  • Sequential Display: Added clear labels and blank lines to separate the two pyramids for better readability.
  • Reused Variable: The rows value is shared between both pyramids instead of being redefined.

If you prefer to display the pyramids side-by-side, here's an alternative version:

# Get valid input between 1-9
while True:
    try:
        rows = int(input("Enter an integer between 1 and 9: "))
        if 1 <= rows <= 9:
            break
        print("Input must be between 1 and 9. Please try again.")
    except ValueError:
        print("Invalid input. Please enter an integer.")

print("\nSide-by-Side Pyramids:")
for i in range(1, rows + 1):
    # Build star pyramid line
    star_line = ""
    star_line += "  " * (rows - i)
    star_line += "* " * (2 * i - 1)
    
    # Build number pyramid line
    num_line = ""
    count = rows - i
    count1 = 0
    k = 0
    num_line += "  " * (rows - i)
    
    while k != (2 * i - 1):
        if count <= rows - 1:
            num_line += f"{i + k} "
            count += 1
        else:
            count1 += 1
            num_line += f"{i + k - 2 * count1} "
        k += 1
    
    # Print both lines side by side
    print(f"{star_line}    {num_line}")

Side-by-Side Explanation:

  • For each row, we construct the string for both pyramids separately.
  • Both strings are printed on the same line, separated by spaces for clarity.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 23:35:50