0

I've just started learning python.

I came across this excercise: Ask a user to input a value, if it's a string count it's length, if it's an int or a float, return a sentece, and ask them to input a string instead if ints and floats.

I came up with this:

 def string_length(mystring):
    return len(mystring)


mystring = input("Enter your string to count: ")

if type(mystring) == int:
    print("You must input a string, integers don't have length!")
elif type(mystring) == float:
    print("You must input a string, floats don't have length!")
else:
    print(string_length(mystring))

the problem is that it counts the characters of ints and floats as well like this:

Enter your string to count: 111
3
Process finished with exit code 0

i'm using pycharm and the prebuilt "command line" i'm not executing the program by running it normally.

Exitl0l
  • 459
  • 2
  • 11
  • 27
  • 1
    The return value of `input` is always a string (if you are using Python 3). – timgeb Nov 14 '17 at 13:30
  • 1
    To input a int, use mystring = int(input("Enter your string to count: ")) – Abhijeetk431 Nov 14 '17 at 13:32
  • The duplicate is not *exactly* the same question, but covers everything you need in great detail. – timgeb Nov 14 '17 at 13:33
  • I have modified your code, please check https://repl.it/repls/UnawareCornsilkIndianglassfish – akash karothiya Nov 14 '17 at 13:37
  • I was going to ask another question but i got that anything that i prompting the user to input is going to be a string. But i have a question to counter that, how can i modify my code to work with the concept that i have in mind? – Exitl0l Nov 14 '17 at 13:39
  • @AlexBene read the duplicate, it covers that question. – timgeb Nov 14 '17 at 13:39

0 Answers0