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

How to print an error saying that an integer must be entered if either input value is not a number, and multiply x by y when no error is caught

I am trying to make it so that if either input value is not a number, it will print an error message saying that an integer must be entered. And whenever no error is caught, it will multiply x by y. How would I do this?

Here is what I have so far:

def main():
    try:
        x = int(input("Please enter a number: "))
        y = int(input("Please enter another number: "))
    except:

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

>Solution :

It’s good practice to not have empty except as it makes debugging difficult

def main():
    try:
        
        x = int(input("Please enter a number: "))
        y = int(input("Please enter another number: "))
        print(x*y)
    
    except(ValueError):
        print("An integer must be entered. ")
main()
1 comments

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