0

I'm working on a script to take user input and write it into an Excel file currently the script works but when trying to add in logic to reject non-integer answers, one of two things happen:

  1. it gets stuck in an infinite loop after properly rejecting the first answer where it keeps asking for a new answer and never accepts it
  2. it properly rejects the first answers, but then accepts the second no matter what it is

this is the code being used where it gets stuck in an infinite loop (I have tried a few other variations that do the same thing but this is the most recent)

true = True
x = input('What is the first marked distance?')

while true == True:
    if x == int:
        cl.value = x
        break
    else:
        x = input('Please enter a whole number')

any advice on why this is looping and not working would be greatly appreciated

  • You are checking if a `str`, `x` is equal to a `type` `int`? What are you trying to do? Use if `x.isdigit()` to check if the string can be converted to an int. Then `c1.value = int(x)`. Also you can just say `while True:` – TheLazyScripter Dec 03 '20 at 19:25
  • 1
    `x == int` doesn't check if `x` is represented by an integer, it checks if `x` is literally the class `int`. – Carcigenicate Dec 03 '20 at 19:28

1 Answers1

0

Use float(x)%1 == 0 instead of x == int

André Kuljis
  • 90
  • 1
  • 9