gpt4 book ai didi

python - For 循环得到错误说 (odd number - odd number)/2 is a float

转载 作者:行者123 更新时间:2023-12-05 09:33:23 28 4
gpt4 key购买 nike

<分区>

我正在尝试制作一个函数,用奇数个星号(例如 1、3、5)制作金字塔

import math
baseInput = 0
while baseInput % 2 == 0:
baseInput = int(input('Enter base length: '))
def drawPyramid(base):
#makes variable for amount of asterisks that have to be drawn
starcounter = 1
for i in range(starcounter, base):
#calculates number of spaces needed and prints them
for i in range((base - starcounter + 1) // 2):
print(' ', end='')
#prints current amount of asterisks
for i in range(starcounter):
print('*', end='')
print('\n')
#increases amount of stars
starcounter += 2
drawPyramid(baseInput - 1)

我认为这个输出会是:

  *
***
*****

相反,我收到一条错误消息

enter image description here

我不明白为什么 5 - 1 被认为是 float 。

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