mike-telford
mike-telford

Reputation: 113

Basic Python program, keeps telling me my elements in list are strings not integers?

I am trying to create this function to multiply 2 positive integers from the user. When I run the code it basically says that the list elements are strings not ints, even though I specify in the input loop that I want the strings converted to ints. Not really sure what's going on? (n.b. I do know this is all highly unnecessary lol but I am a beginner and just trying to learn with basic challenges. Thank you)

def multiply(a, b):
    c = a * b
    return c

input_list = []
for items in range(1, 3):
    input1 = (input('Enter 2 positive, whole numbers you would like to multiply '))
    int(input1)
    input_list = input_list + [input1]  

result = multiply(input_list[0], input_list[1])
print(result)

Upvotes: 0

Views: 1339

Answers (3)

Adrian Staniec
Adrian Staniec

Reputation: 141

How about something like this:

def multiply(a, b):
    c = a * b
    return c


numbers = []
for items in range(2):
    string = input('Enter a number you would like to multiply: ')
    number = int(string)
    numbers.append(number)


result = multiply(numbers[0], numbers[1])
print(result)

Upvotes: 0

def multiply(a, b):
    c = a * b
    return c

input_list = []
for items in range(1, 3):
    input1 = (input('Enter 2 positive, whole numbers you would like to multiply '))
    input_list.append(int(input1))


result = multiply(input_list[0], input_list[1])
print(result)

Try this.

Upvotes: 0

DSC
DSC

Reputation: 1153

You convert input1 to an integer but you don't save the result:

int(input1)

You must do:

input1 = int(input1)

Upvotes: 3

Related Questions