简体   繁体   中英

How to take user input and use the if else function to give outputs

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")

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) .

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)

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!")

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!

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM