1

My goal is to build a calculator, where the user is asked to choose what kind of mathematics they would like to do (Roots, addition, etc). They would then be asked to enter two numbers. At the moment, the user can enter random letters when it asks for the numbers and that ends the program. I have tried a few things to test if their input is an integer, in this function:

def vargrab():
    x = (input("Enter a number: "))
    y = (input("Enter a number: "))
    if x or y != int:
        print("Invalid answer")
        x = 0
        y = 0
        vargrab()

I am quite new to python. Any advice would help!

NessAQ
  • 11
  • 1
  • 1
    Use string.isdigit() – Red Jun 04 '20 at 03:27
  • 2
    I presume you mean if the `str` objects returned by `input` represent valid integers... note, a string will never be an `int`. To check if something is some particular type, you use `isinstance(some_object, int)` or something like `type(some_object) is int`. Also note, `x or y != whatever` doesn't work the way you probably think... you probably meant `x != whatever or y != whatever` – juanpa.arrivillaga Jun 04 '20 at 03:28
  • @juanpa.arrivillaga did you try and run it? after running it i get string for int 1... – Je Je Jun 04 '20 at 03:44
  • @NonoLondon I have no idea what you mean. – juanpa.arrivillaga Jun 04 '20 at 03:46
  • Ok, here to notify you of changes. I tested out the type(some_object) is int function and it worked in my test file. But when I tried to apply it I was left with the same outcome. When I add in my vargrab function. Despite whether I enter integers or not, It always says invalid answer, here is my updated code: def vargrab(): x = (input("Enter a number: ")) y = (input("Enter a number: ")) if x or y != int: print("Invalid answer") x = 0 y = 0 vargrab() – NessAQ Jun 04 '20 at 03:50
  • well apparenlty when you input 1 with the keyboard, it is seen as a string, so instance doesn't work.... I was the first one to agree with you but doesn't work. my best answer is with a try/except=> give a try... – Je Je Jun 04 '20 at 03:51
  • @NessAQ yeah agreed – Je Je Jun 04 '20 at 03:55
  • def vargrab(): wrong_input = True while wrong_input: x = (input("Enter a number x: ")) y = (input("Enter a number y: ")) try: x = int(x) y = int(y) print(f'x is :{x} of type:{type(x)}') print(f'y is :{y} of type:{type(y)}') wrong_input=False except Exception as ex: print(f'Error is: {ex}') if __name__ == "__main__": vargrab() – Je Je Jun 04 '20 at 04:02
  • Ok, so another update. I can now make their answer an integer with the int(input("Enter a number")) syntax. But this means it will always convert their answer to an integer. Giving an error when their input was a string of letters. Or I can not tell it that the input is an integer and make it always assume it is a string, therefore giving me an error no matter what. – NessAQ Jun 04 '20 at 04:03
  • happy to open a private chat with you. did you check the link provided by regulator? my answer and linked ones, propose a way to handle the error. My answer in comments, suggests a way to loop back, as you were doing. – Je Je Jun 04 '20 at 04:08
  • Opening a private chat with you would likely be helpful, that way I could send you my full code so far and maybe you could edit it and tell me what you are editing etc. I don't know how, or who regulator is. – NessAQ Jun 04 '20 at 04:18
  • go ahead, i am not sure how to do that? annoying. – Je Je Jun 04 '20 at 04:19
  • did you see my comment/answer 20 minutes ago? indent it, and it should do what you need – Je Je Jun 04 '20 at 04:24
  • @AnnZen hint works better: def vargrab(): wrong_input = True while wrong_input: x = (input("Enter a number x: ")) y = (input("Enter a number y: ")) if x.isdigit() and y.isdigit(): wrong_input=False if __name__ == "__main__": vargrab() – Je Je Jun 04 '20 at 04:31

0 Answers0