-1
def Validation(digits):
    while not digits.isdigit():
        digits = input("Please select an interger for this part")
    digits = int(digits)

length_1 = input("What is the length of one of the sides?")
    Validation(length_1)
length_2 = input("What is the length of another side?")
    Validation(length_2)
answer = length_1 * length_2 / 2 

I am attempting to use a function which validates the users inputs. At the end it should turn it into an interger so that they can be multiplied together. However, I get the error: answer = length_1 * length_2 / 2 TypeError: can't multiply sequence by non-int of type 'str'. I can fix it by adding int(length_1) and int(length_2), however the point of the function was to not do this

DavidG
  • 24,279
  • 14
  • 89
  • 82
J Bales
  • 3
  • 1
  • https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers – Olex Prost Nov 30 '18 at 15:47
  • Read https://nedbatchelder.com/text/names.html. Your function can't modify what `length_1` refers to by reassigning to `digits`. – chepner Nov 30 '18 at 15:52

3 Answers3

1

Python passes by value. The digits passed to your function is re-assigned in the last line. That does not change the original value, it just creates a new variable. Instead of what you are doing, return int(digits)

blue_note
  • 27,712
  • 9
  • 72
  • 90
0

digits = int(digits) can't alter the string passed to Validation. All reassigning in the function does is change what the function parameter is pointing at. That has no effect outside the function.

Just return the parsed digits:

def Validation(digits):
    while not digits.isdigit():
        digits = input("Please select an interger for this part")
    return int(digits)

length_1 = input("What is the length of one of the sides?")
parsed_length_1 = Validation(length_1)
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
0

You have to return the value from the function and also replace the variable that you are going to use.

 def Validation(digits):
        while not digits.isdigit():
            digits = input("Please select an interger for this part")
        return int(digits)

    length_1 = input("What is the length of one of the sides?")
    length_1 = Validation(length_1)
    length_2 = input("What is the length of another side?")
    length_2 = Validation(length_2)
    answer = length_1 * length_2 / 2 
    print(answer)
KeZ
  • 20
  • 1
  • 4