Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading