Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mixed function
#1
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.
Reply
#2
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
Reply
#3
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.
Reply
#4
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: "))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting digits in a mixed string snorri 1 1,652 Apr-22-2020, 11:04 AM
Last Post: buran

Forum Jump:

User Panel Messages

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