Alex Barbulescu
Alex Barbulescu

Reputation: 331

A Function That Counts The Number of Loops

Write a function multisplit that consumes two positive integers total and split and produces the number of times total is repeatedly divided into split even pieces before each piece is of size at most 1. For example, the value returned by multisplit(8, 2) will be 3, since 8 can be split into 2 pieces of size 4, which are then each split into 2 pieces of size 2, which are then each split into 2 pieces of size 1 (at which point no further splitting takes place since the pieces are of size at most 1).

    total= int(input("Total:"))
    split= int(input("Split:"))

    def multisplit(total,split):
    x=o
    while value>=1:
        value= total//split
        x= x+1
    return x

    print(x)

It's telling me that the name 'x' is not defined

Upvotes: 1

Views: 200

Answers (1)

3D1T0R
3D1T0R

Reputation: 1060

There are several issues with the code you posted:

  • In python, the contents of a function must be indented.

    def myfunction():
        # code inside the function goes here
    # code after you've unindented is not in the function
    
  • You didn't define your value variable before using it.
  • Assuming that your final line gets appropriately unindented, so that it won't be completely ignored because of being inside the function, but after the return statement:
    You're trying to print the value of a variable that was defined in a different scope. Specifically, you defined x inside the function, and now you're trying to look at it outside the function.
  • You never called your function...
    If I understand what you're trying to do, you want to call the function inside print. i.e.: print(multisplit(total, split))

Upvotes: 2

Related Questions