banro
banro

Reputation: 3

Can't multiply sequence by non-int of type 'float'?Tried, and tried

def half_finished_diamond(height):
    n = 1
    for i in range(height):
        spaces = height / 2 - n
        blank = " "
        print(blank*spaces + '/' * n + '\\' * n + "\n")
        n += 1


half_finished_diamond(8)

Wanting to get the upper part of diamond shape

Upvotes: 0

Views: 59

Answers (2)

sal
sal

Reputation: 3593

As pointed out in the comments, since you are using Python3, you can use the floor division operator to assure an int result:

def half_finished_diamond(height):
    n = 1
    for i in range(height):
        spaces = height // 2 - n    # <-- note the // instead of /
        blank = " "
        print(blank*spaces + '/' * n + '\\' * n + "\n")
        n += 1


half_finished_diamond(8)

See it in action on https://eval.in/1078035

Upvotes: 0

Calvin Godfrey
Calvin Godfrey

Reputation: 2369

In Python 3, when you say space = height / 2 - n, it automatically casts the result to a float, so spaces will be 4.0 - 1 = 3.0. You'll have to cast it to an int to multiply a string by it.

Upvotes: 2

Related Questions