pythonnumbersintdivisiondivmod

Python Divmod help for a mathematical exercise for beginner


So basically all I am trying to do is take the third and forth digits from a set of numbers:

# Number Set
num_set = (28,54,79,37,2,9,0)

and divide them both(79 and 37), This is the code I have written:

# Division of third and fourth digits
# Seperating the digits
div_num = ((num_set[2,3]))
print("we are going to divide", div_num)
ans = (divmod(div_num))
print("the answer for 79 divide by 37 is", ans)

which gives me the error

"TypeError: tuple indices must be integers or slices, not tuple"

Any help would be appreciated ! Thanks


Solution

  • What you want is to replace this line of code

    ans = (divmod(div_num))
    

    with:

    ans = divmod(num_set[2], num_set[3])
    

    You don't need div_num so remove all its references.


    Why do you get your error?

    num_set[2,3] is same as num_set[(2,3)]. You are trying to index a tuple by a tuple when it should be by integers or slices.


    Code:

    ans = divmod(num_set[2], num_set[3])
    print("the answer for 79 divide by 37 is", ans)