创建包含 3 和 10 的范围

问题描述 投票:0回答:1

我必须专门用限制符和连字符创建一个平行四边形。我的盒子必须向右倾斜一格。然而,我在开始时遇到了困难,因为高度和宽度的范围必须完全在 3 到 10 之间。

height = int(input("Enter a height for your parallelogram between 3 and 10: " ))
for h in range(3,10):
    print("*", end="")
    print()
else:
    height = int(input("Invalid input"))
python range nested-for-loop
1个回答
0
投票

range(3,8)
只能用于确保选择正确的数字。从那里您应该直接与
height
打交道。为了获得你的倾斜,只需在星号前添加一个递减的空格即可。另外
range
不包含在内。如果你想要3到10个,你需要使用
range(3,11)

MESSAGE = "Enter a height for your parallelogram between 3 and 10: "

#keep asking the same question until the input is correct
while not (height := int(input(MESSAGE))) in range(3,11):
    print('Invalid Input')
    
#draw "parallelogram"
for h in range(height):
    print(f"{' '*(height-h)}*")
© www.soinside.com 2019 - 2024. All rights reserved.