Adding Up str input

Im getting an error saying I cant multiply sequences by non-int of type(str) which is confusing me because all I wanted to do is multiply the 2 str(input)’s together, I tried finding resources on the internet and nothing to be seen.

length = str(input("Enter length (cm): "))
width = str(input("Enter the width (cm):"))
area = [( width * length )+"cm"]
print("The area of the rectangle is"+ area)

>Solution :

What you did wrong is that you are trying to multiply strings and not integers, when getting input, stored value is a string. You need integers for this task so convert input to int with simple int(input()).

Here is the code that you need to use:

def main():
    try:
        length = int(input("Enter the length (cm): "))
        width = int(input("Enter the width (cm):"))
    except ValueError:
        print("Input should be only numbers!")
        main()
        return
    area = width * length
    print(f"The area of the rectangle is {area} cm.")

main()

Added: Exception to prevent people from typing strings in input field.

Leave a Reply