I want to compare the input with the list, but the else loop continues infinitely even when i give it a input that matches the list.
def check_input():
while True:
guessing_range = input("Please enter a guessing range.")
if guessing_range.isdigit:
guessing_range = int(guessing_range)
if guessing_range in lst:
break
else:
print("Guessing range must be 10, 100, or 1000!")
continue
return guessing_range
check_input()
CodePudding user response:
It looks like your check_input function is trying to prompt the user to enter a guessing range, and then check whether the entered value is in a list of acceptable guessing ranges. However, there are a few issues with the code.
First, when you check whether guessing_range is in the list, you are using the in keyword to check whether guessing_range is in the list. However, guessing_range is a string at this point, so the in keyword will always return False, even if the string is an acceptable guessing range. This is because the in keyword checks for identity, not equality, so it will only return True if the string is the same object as one of the elements in the list, which is unlikely to be the case.
To fix this, you can convert the string to an integer before checking whether it is in the list. This way, you will be checking whether the integer value of guessing_range is in the list, rather than the string itself.
Another issue is that you are using the isdigit method to check whether guessing_range is a digit. However, the isdigit method returns a bool value indicating whether the string is a digit, so you need to call it like a function. In other words, you need to add parentheses after isdigit to actually call the method and get the return value.
Here is how you could modify your check_input function to fix these issues:
def check_input():
while True:
guessing_range = input("Please enter a guessing range.")
if guessing_range.isdigit(): # Call the isdigit method to get the return value
guessing_range = int(guessing_range) # Convert the string to an integer
if guessing_range in lst: # Check whether the integer value is in the list
break
else:
print("Guessing range must be 10, 100, or 1000!")
continue
return guessing_range
With these changes, your check_input function should be able to check whether the user's input is an acceptable guessing range and break out of the while loop if it is.
CodePudding user response:
There is an error in the line if guessing_range.isdigit:
. isdigit()
is a method, so it needs to be called using parenthesis. The correct syntax would be if guessing_range.isdigit():
.
Additionally, the continue
statement at the end of the loop is unnecessary as the loop will continue iterating automatically.
There are a few other issues with the code:
- The
lst
variable is not defined in the code, so theif guessing_range in lst:
line will throw aNameError
. The list of valid guessing ranges needs to be defined before it can be used in thecheck_input()
function. - The
guessing_range
variable is assigned an integer value only if the user input is a digit. If the user enters a non-digit value, theguessing_range
variable will still have the original string value, which will not be in the list of valid ranges. This will cause theif guessing_range in lst:
line to always returnFalse
.
To fix these issues, the code can be modified as follows:
lst = [10, 100, 1000]
def check_input():
while True:
guessing_range = input("Please enter a guessing range.")
if guessing_range.isdigit():
guessing_range = int(guessing_range)
else:
print("Guessing range must be a number!")
continue
if guessing_range in lst:
break
else:
print("Guessing range must be 10, 100, or 1000!")
return guessing_range
check_input()