简体   繁体   English

如何获取用户输入并使用if else函数提供输出

[英]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.

相关问题 如何将用户输入作为功能输入 - How to take user input as function input 如何在 function 中获取用户的 *args 输入 - How take *args input from user in a function 如何给出不同的输出取决于python中的输入? - how to give different outputs depends on input in python? 如果用户没有在 python3 中提供语音输入,如何手动接受用户输入 - How to manually take user input if user doesn't give voice input in python3 如何在导入 numpy 模块时获取用户输入“n = str(input("Give str"))”? - How do I take user input "n = str(input("Give str"))" while having the numpy module imported? 如何在python中将用户定义的函数作为用户输入? - How to take a user defined function as user input in python? 当我在这里将 2 作为输入并在 function 中使用 else 时,它返回 non 但我删除了 else 部分并使用没有缩进的 return 它工作正常 - when I take 2 as an input here and use else in function it returns non but i remove the else part and use return without indentation it works properly 如何获取函数的输出并将其用作另一个函数python的输入? - How to take output of function and use as input of another function python? 我如何使用数组来获取用户输入 - How can i use an array to take user input 如何使用模型从Django用户获取输入作为文件? - how to use models to take an input as a file from user in django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM