2

I'm trying to make a function that makes a pyramid out of an odd number of asterisks (e.g. 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)

I thought the output of this would be:

  *
 ***
*****

Instead, I get an error saying

enter image description here

I don't understand why 5 - 1 gets considered a float.

noobprogrammer
  • 153
  • 1
  • 8

4 Answers4

3

I don't understand why 5 - 1 gets considered a float.

5 - 1 is not a float, but (5 - 1) / 2 is. The reason is simple: the / division operator always returns a float, even if the fractional part of the result is 0.

See https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator for more info.

The error comes from the range function which expects an integer.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
2

Just use // (integer division) instead of / (float division), because range expects integers:

for i in range((base - starcounter) // 2):
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

range can not take floats. Try it like this.

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(4)
  *

 ***

*****

programandoconro
  • 2,378
  • 2
  • 18
  • 33
  • Thanks! Why is there a 4th row with 7 asterisks though? Doesn't the function print the asterisks and *then* add 2? – noobprogrammer Apr 30 '21 at 10:10
  • @noobprogrammer because the first loop starts in 1 and ends in 5, meaning it is running 4 times. Try it ``drawPyramid(4)`` to get your expected output. – programandoconro Apr 30 '21 at 10:15
  • I tried, but that messed up the spaces - only 1 space for the first column and none for the 2nd. – noobprogrammer Apr 30 '21 at 10:16
  • 1
    Add 1 more value to the second loop: `` for i in range((base - starcounter + 1) // 2)`` – programandoconro Apr 30 '21 at 10:18
  • How do I make it so that the base fits the parameter perfectly? I have to make it work with input now. I've tried ``drawPyramid(baseInput - 1)`` because drawPyramid(4) made a base of length 5 (so inputting 5 makes a pyramid with base 5), but when I make baseInput = 3, the base is only 1 long. Sorry if this is difficult to understand. – noobprogrammer Apr 30 '21 at 10:32
1

Use integer division (//) instead of float division (/):

for i in range((base - starcounter) // 2)
nobleknight
  • 755
  • 6
  • 15