gpt4 book ai didi

Python 3.4 : creating an asterisk diamond

转载 作者:太空宇宙 更新时间:2023-11-03 17:20:08 26 4
gpt4 key购买 nike

所以我一直在尝试创建一个程序,在其中提示用户输入行数,然后程序打印出带有输入行数的菱形。

这就是程序应该执行的操作:(image)

这就是我的代码的样子

def main():
ROWS = get_input()
draw_pattern(ROWS)

def get_input():
return int(input("Enter the number of rows (or -1 or -99 to quit): "))

def draw_pattern(ROWS):
if ROWS == -1 or ROWS == -99:
quit
else:
for x in range(0,(int)((ROWS/2)+1),1):
print ((int)((ROWS/2)-(2*x+1)/2)*" " + (2*x+1) * '*')

for t in range((int)(x),0,-1):
print((int)((ROWS/2)-(2*t-1)/2)*" " + (2*t-1) * '*')

main()

这就是它最终要做的事情:

Enter the number of rows (or -1 or -99 to quit): 7
*
***
*
*****
***
*
*******
*****
***
*

那我做错了什么?我认为这是我的 for 循环中的某些内容导致行无法正确排列。谁能给我一点帮助吗?谢谢。

最佳答案

我让它像这样工作。 (< 3 分钟)(不要使用偶数,它看起来很奇怪)

def main():
ROWS = get_input()
draw_pattern(ROWS)

def get_input():
return int(input("Enter the number of rows (or -1 or -99 to quit): "))

def draw_pattern(ROWS):
if ROWS == -1 or ROWS == -99:
return

else:
for x in range(1, ROWS, 2):
print(' ' * int(ROWS / 2.0 - x / 2.0) + ("*" * x))

for x in range(ROWS, 1, -2):
print(' ' * int(ROWS / 2.0 - x / 2.0) + ("*" * x))

if x != 1:
print(' ' * int(ROWS / 2.0 - 1 / 2.0) + ("*" * 1))

main()

关于Python 3.4 : creating an asterisk diamond,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33249966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com