[英]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. 我试图接受用户输入,并使用if else函数将结果作为基础。
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'
. 此输入是一个名称,如果是字符串,则输出应该是文本'What a beautiful name you have'
,如果是int或float,则输出应该是'Sorry , please enter a name'
。 But if I enter either an int or a str both outputs are 'What a beautiful name you have'
. 但是,如果我输入int或str,则两个输出都是'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)...
. 1)您使用input
作为type
参数-> elif type(input)...
You should be using name
. 您应该使用name
。
2) input()
ALWAYS returns a str
. 2) input()
总是返回一个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. 3) elif type(input) is int or float
并没有执行您认为的操作。 That is equivalent to elif (type(input) is int) or float
so it will always be True
because float
is Truthy. 这等效于elif (type(input) is int) or float
因此它将始终为True
因为float
是Truthy。 You need to do elif type(name) is int or type(name) is float
. 您需要做elif type(name) is int or type(name) is float
。
4) You shouldn't use type(...)
for comparing, instead use isinstance(name, str)
. 4)您不应使用type(...)
进行比较,而应使用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. 注意: name
始终为字符串格式。 If you want to check whether there is any number in name
, then you can do this : any(i.isnumeric() for i in name)
如果要检查是否有任意数量的name
,那么你可以这样做: 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. 字符串"1"
仍然是字符串。
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: 考虑以下使用正则表达式的代码,以识别您的字符串是否包含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. 这部分工作,但您需要在引号下键入名称,否则您将收到错误消息,因为python正在寻找变量名称。 If the intgers are written inside the quotation marks they will be seen as str but if not the code will do its job. 如果将整数写在引号内,则它们将被视为str,但如果不是,则代码将完成其工作。 TBH I don't see a feasible solution without giving the trick away. TBH如果不给我个窍门,我看不到可行的解决方案。
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.