简体   繁体   English

如何要求用户提供布尔输入以限定if语句并使用Python中的该信息打印句子

[英]How to ask for a boolean input from a user to qualify if statements and print a sentence using that information in Python

I'm here with my first (hopefully not last) query for you. 我在这里是我的第一个查询(希望不是最后一个查询)。

I'm trying to get information from the user, specifically boolean values to determine if they are male/female and tall/short, then print the correct statement of "You are a tall/short male/female" 我正在尝试从用户那里获取信息,特别是布尔值,以确定它们是男性/女性还是身材高/矮,然后打印正确的语句“您是男性/女性身高/矮”

is_tall = input("Are you tall?")
is_male = input("Are you male?")

if is_tall and is_male:
  print("You are a tall male")
elif is_tall and not(is_male):
  print("You are a tall female")
elif not(is_tall) and is_male:
  print("You are a short male, sorry")
else:
  print("You are a short female, sorry")

My code runs and always returns "You are a tall male", searching reveals this is because the user's input is considered always true as it is a string. 我的代码运行并始终返回“您是个高个子男性”,搜索显示这是因为用户输入的内容始终是真实的,因为它是字符串。

I've seen other examples for grabbing boolean values but not for grabbing two and using them both within if statements. 我已经看到了其他示例,它们可以获取布尔值,但不能在if语句中同时获取两个值并同时使用它们。 Am I anywhere close here, what am I missing? 我在附近任何地方吗?我想念什么? I know I can't use bool() around the input to convert it. 我知道我不能在输入周围使用bool()进行转换。

Currently, you are checking if the string is non-zero. 当前,您正在检查字符串是否为非零。 Instead, check the value of the string. 而是,检查字符串的值。

    if is_tall.upper() in ('YES', 'Y') and is_male.upper() in ('YES', 'Y'):
        print("You are a tall male")
    elif is_tall.upper() in ('YES', 'Y') and is_male.upper() in ('NO', 'N'):
        print("You are a tall female")

etc. 等等

You may list as many valid strings (answers) as you like in the brackets after in , and forcing all input to either .upper() or .lower() saves you from dealing with twice as many (or more) possible responses. 您可以在in后面的括号in .upper()任意.upper()有效字符串(答案),并且将所有输入都强制为.upper().lower()可以使您避免处理两倍(或更多)可能的响应。

you could try something like this: 您可以尝试这样的事情:

tall_str = input("Are you tall? ")
while tall_str.lower() not in {'y', 'yes', 'n', 'no'}:
   tall_str = input("Are you tall [y/n] ? ")

is_tall = True if tall_str.lower() in {'y', 'yes'} else False

this will accept things like 'Yes' and 'nO' (for tall_str ) and then convert them to True or False (for is_tall ) and ask again if the given answer is not acceptable. 这将接受'Yes''nO' (对于tall_str )之类的东西,然后将它们转换为TrueFalse (对于is_tall ),然后再次询问给定的答案是否可接受。

you could then do the same for is_male . 然后可以对is_male执行相同的is_male

having cast the string to a boolean your if clauses will work. 将字符串转换为布尔值后, if子句将起作用。

Would this approach seem fit to you? 这种方法适合您吗?

is_tall = True if input("Are you tall?").lower() == 'y' else False
is_male = True if input("Are you male?").lower() == 'y' else False
...

There is no easy way to get bool in input, although you can convert it yourself. 尽管可以自己进行转换,但没有简单的方法来获取布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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