Beginner Python question

i’m new to python and i have a question about the numbers guessing game project in this video 5 Mini Python Projects - For Beginners - YouTube. he uses isdigit() to identify whether the user’s input is a number, but isdigit() can’t identify negative numbers if i’m correct? since a negative number would return as false, it defaults to the else statement that says to type a number next time. is this an oversight since a negative number can’t get the program to generate “please type a number larger than 0 next time”?

thank you for your response! did you have a chance to look at the example in the video? if so, is the creator’s use of isdigit() an oversight since it doesn’t account for negative numbers? mainly from a user experience perspective

Technically it’s correct since, as Randell points out, a negative number is not a digit. A negative number can’t be a digit since there are 2 characters.

It depends if the instructions given to the user are to enter a digit or enter a number, since a negative number is still a number (but not a digit, since it’s 2 characters).

You could add a check for a negative number and add a case for that, if you wanted, I think it would improve the program in the end.

I wouldn’t call it an “oversight” for a “mini python project” though, I’m sure there might be a lot you could add to make it more robust. Good catch, though!

In Python, the “input()” function takes input as a string. Therefore, when a negative sign is present, it is considered a non-integer value. However, you can use exception handling for a better user experience. For example:

   user_guess = int(user_guess)
   if user_guess == random_number:
      # your code

except : 
   # if user_guess is not a number
   print( " please type a number next time")

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.