0

I am trying to take the user input and use the if else function to base the outcome on it.

This input is a name and if it's a string, the output should be the text 'What a beautiful name you have' and if it is a int or a float, the output should be 'Sorry , please enter a name'. But if I enter either an int or a str both outputs are 'What a beautiful name you have'. What should I do? Here is my code:

name = input("What is your name?")

if type(input) is str:    
    print("What a beautiful name you have!")

elif type(input) is int or float:
    print("Sorry, enter a proper name")
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
  • 2
    input() always returns a str object, so every time the first condition is invoked. – Shuvojit Jul 18 '19 at 13:02
  • 1
    Another problem with your code is, you are checking the type of `input`, you should be checking the type of `name` which holds the value – Mezbaul Haque Jul 18 '19 at 13:06

5 Answers5

3

There are several issues.

1) Your using input as your type argument -> elif type(input).... You should be using name.

2) input() ALWAYS returns a str. You have to type cast it to what you want.

3) elif type(input) is int or float doesn't do what you think it does. That is equivalent to elif (type(input) is int) or float so it will always be True because float is Truthy. You need to do elif type(name) is int or type(name) is float.

4) You shouldn't use type(...) for comparing, instead use isinstance(name, str).

2

Try this :

name = input("What is your name?")
if all(x.isalpha() or x.isspace() for x in name):
    print("What a beautiful name you have!")
else:
    print("Sorry, enter a proper name")

Note : name is always in string format. If you want to check whether there is any number in name, then you can do this : any(i.isnumeric() for i in name)

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
  • 1
    This is probably good enough for most basic uses, [but some assumptions may be wrong](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) – Arthur.V Jul 18 '19 at 13:22
1

The input function returns a string regardless the content of the string. The string "1" is a string nonetheless.

What you may want to do is check if the string can be converted into a number without error:

s = input("what is your name?")
try:
    float(s)
    print("Sorry, enter a proper name")
except ValueError:
    print("What a beautiful name you have!")
Arthur.V
  • 676
  • 1
  • 8
  • 22
  • That is alittle going around, trying to check for `float` when you want the input to be a pure string. What about `float("=")`? – Tomerikoo Jul 18 '19 at 13:10
  • 1
    @Tomerikoo Obviously you're right. Was following the logic of the OP regarding to what is a legal name. The real problem of recognizing a name is a bit more complex. – Arthur.V Jul 18 '19 at 13:13
1

The problem is that the input from the machine will always be a string. Therefore even if you input a number or a float, it will be considered a string. Consider the following code using regular expressions to identify if your string contains an int/float:

import re
name = input("What is your name?")

if re.match('[0-9]', name): 
  print("Sorry enter a proper name")
else:
  print("What a beautiful name") 

Hope this helps!

DWu
  • 11
  • 1
0

This one works partially, but you need to type the name under quotation marks otherwise you will get an error cuz python will be looking for a variable name. If the intgers are written inside the quotation marks they will be seen as str but if not the code will do its job. TBH I don't see a feasible solution without giving the trick away.

x = eval(input("What is your name? "))
if isinstance(x, str):
    print ("What a beautiful name you have!")
else:
    print ("Sorry, enter a proper name")
Zesima29
  • 184
  • 12