Python Forum
Problems with isdigit function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Problems with isdigit function (/thread-11263.html)



Problems with isdigit function - Omegax77 - Jun-30-2018

Made a guessing game but I think I coded something wrong when checking whether if the input is a number or not. My goal is that the user is only allowed to print 4 numbers(no letters). I think I did a mistake with the isdigit() area. Help plz! Thank you
Error: "invalid literal for int() with base 10: 'f999' "
BTW this is my first post. Not sure if my formatting is correct

import random
n = random.randint(0, 9999)
guess = int(input("Enter number from 0 to 9999"))
print()

while n != "guess": 
    if len(str(guess)) < 4:
      print ("Invalid! Not enough characters. 4 characters only")
      guess = int(input("Enter number from 0-9999 "))
      print()
    
    elif not guess.isdigit():
      print("Only numbers!")
      guess = int(input("Enter number from 0-9999 "))
      print()
  
    elif len(str(guess)) > 4:
      print ("Invalid! Too many characters. 4 characters only!")
      guess = int(input("Enter number from 0-9999 "))
      print()

    elif guess < n:
       print ("too low")
       guess = int(input("Enter number from 0-9999 "))
       print()

    elif guess > 9999:
      print("Invalid")
      guess = int(input("Enter number from 0-9999 "))
      print()

    elif guess < 0:
      print("Invalid")
      guess = int(input("Enter number from 0-9999 "))
      print()

    elif guess > n:
      print ("too high")
      guess = int(input("Enter number from 0-9999 "))
      print()
    else:
      print ("You guessed it!")
      break
    print



RE: Problems with isdigit function - ichabod801 - Jun-30-2018

Your formatting is not correct. Please use Python tags. See the BBCode link in my signature below for details/instructions.

I think you want to remove the quotes in the while loop condition. Your guess variable is never a string because you are always applying int to it when you get it from input.


RE: Problems with isdigit function - woooee - Jun-30-2018

You are trying to put too many elifs in one routine. Also, you convert to an int on input and then convert back to a string to test?? First, use a function to get the input. Then, use the verified input from the function to test against the number to guess. Note that if the user is to enter 4 digits, then the range for random.randint and everything else is 1000-9999 = 4 digits.
import random
n = random.randint(1000, 9999)

def get_input():
    while True:
        guess = input("Enter a number from 1000 through 9999 ")
        if guess.isdigit():
            guess_int=int(guess)
            if 999 < guess_int < 10000:  ## covers every number with a length of 4
                return guess_int
            else:
                print("You must enter 4 digits")
        else:
            print("Enter only numbers in the range 1000-->9999")


guess= -1
while n != guess:
    guess=get_input()
    if guess == n:
      print ("\nYou guessed it! \n")
      ##break

    elif guess < n:
       print ("Your guess is too low \n")

    elif guess > n:
      print ("Your guess is too high \n")