Python Forum
Problems with isdigit function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with isdigit function
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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") 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with isdigit() and isalpha() function Gateux 2 1,731 Jul-22-2019, 02:58 PM
Last Post: jefsummers
  passing a value to function problems Ponamis 2 2,014 Nov-14-2018, 05:05 PM
Last Post: Larz60+
  There are problems with my data visualization function. Tony 1 2,260 May-20-2018, 10:09 AM
Last Post: killerrex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020