Python Forum

Full Version: Mixed function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My final work is to add elements that I used so far in different programs. I wrote this code:

def str_analysis(argstr):
    
    if argstr.isdigit() > 100:
        if argstr.isdigit() > 1000:
            print("We have a big number!")
        else:
            print("we have a moderately big number")
    elif argstr.isdigit() < 100:
        print("We have a small number!")
    elif argstr.isalpha():
        print("We have all alphabetic!")
    else:
        print("We have multiple character type")
str_analysis(input("Add string: "))
What ever input I give it prints "We have a small number!". Don't understand why.
You never convert your string to a number.
This makes no sense:
argstr.isdigit() > 100
argstr.isdigit() is just True or False (1 or 0). Not what you want.

You need to convert to int:
>>> string = "100"
>>> string.isdigit()
True
>>> number = int(string)
>>> number
100
argstr.isdigit() > 100
Result of isdigit() is True or False, so you are comparing them to 100, which always ends up False. A possible solution is to make two separate checks in the "if", and see if both are true with AND operator.
There is a similar issue with with argstr.isdigit() < 100. This always evaluates to True, that is why you always get "we have a small number" printed.
Thank you for the explanation, I adjusted the code to:

def str_analysis(argstr):
     
    if argstr.isdigit():
        if int(argstr) >= 1000:
            print("We have a big number!")
        else:
            print("we have a small number.")
    
    elif argstr.isalpha():
        print("We have all alphabetic!")
    else:
        print("We have multiple character type")
str_analysis(input("Add string: "))