1

I was trying to do an exercise ,which asked us to solve this following problem Exercise problem image which I tried to do ,but by not using same exact keywords as shown in the exercise.

Here is my code

def StringLength(length_of_String):
    return len(text)

text = input("length_of_String :")

if type(text) == int:
    print ("python doesn't show length of integers")
else :
    print (len(text))

But the problem I get here is , if I add any text say like"joker" . It will output me length as "5",which is correct .

But when I type any integer or float , say "101" , it still prints it length as "3" because it is reading it as a string.

So how come I add Variable in which when I input a integer or string , it should recognise it as a string or an integer

Kiran kudpane
  • 31
  • 1
  • 6
  • 1
    You can use `try` and `catch` to see what it is. Try adding 1 to it. If it fails it's a string. Possible duplicate of [How can I check if a string represents an int, without using try/except?](https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except) – sniperd Sep 25 '18 at 16:59

1 Answers1

1

some_variable = input() by default will give you string. You may want to modify your code:

def is_number(s):
try:
    float(s)
    return True
except ValueError:
    return False

def StringLength():
    text = input('Enter:')
    if is_number(text):
        print ("python doesn't show length of integers")
    else :
        return(len(text))

#StringLength() #Remove the '#' at the start of the line to test the function

Edit: I have added a function to test if the entered value is a number or not

SmitM
  • 1,366
  • 1
  • 8
  • 14
  • So this are some code that I tried as said by @SmitM . These are links to my code [link](https://imgur.com/0LNSKvW) [link](https://imgur.com/ASHnhl0) [link](https://imgur.com/2NYDPnB) . But none of them are yielding results. Am I doing it right? – Kiran kudpane Sep 26 '18 at 05:41
  • @Kirankudpane - I have edited my answer. Please check – SmitM Sep 27 '18 at 16:24
  • I am still getting this error . [Terminal snap](https://imgur.com/0QJdB61). If I type any alphabet I get this red error and if I use number , it prints the same number in terminl again. – Kiran kudpane Sep 28 '18 at 17:16
  • here is the code - def is_number(s): try: float(s) return True except ValueError: return False def StringLength(): text = input('Enter:') if is_number(text): print ("python doesn't show length of integers") else : return(len(text)) – Kiran kudpane Sep 28 '18 at 17:25
  • It is same as you have answered . I have just intended the "try and except" function – Kiran kudpane Sep 28 '18 at 17:27
  • But you are not calling your function at the end – SmitM Sep 28 '18 at 17:31
  • thank-you so much .That worked for numbers . But it is asking twice for input after I type alphabets and does not give me length of the "text input" and after typing input for two times it gives me "red error " same as this [Terminal old snap](https://imgur.com/0QJdB61) – Kiran kudpane Sep 28 '18 at 18:27
  • The screenshot you shared is of no help. Can you share the screenshot of your test_codes.py file please – SmitM Sep 28 '18 at 18:43
  • It did work . I guess I didn't save the code yesterday . You were too helpful . Thankyou a lot . – Kiran kudpane Sep 29 '18 at 15:05