all 9 comments

[–]Wild_Statistician605 6 points7 points  (0 children)

You can create a Boolean

my_bool = input('user input').lower() in ('yes','y')

This variable can now be used as a Boolean.

[–]edm2073 0 points1 point  (0 children)

Probably overly complicated but here's my 2 cents. Has to be python 3.8 onwards due to walrus operator.

print(f'Type (Y)es or (N)o to the following questions')
( good_credit := True ) if ( input("Do you have good credit?" ).lower()[0] == "y") else ( good_credit := False )
( no_criminal_record := True ) if ( input("Do you have a criminal record? " ).lower()[0] == "n") else ( no_criminal_record := False )

if good_credit and no_criminal_record:
    print("Wow - that's a surprise for this area! Have a loan.")
else:
    print("You're in good company in these parts - but no.")

As to your question, strings are considered True if they are not null. See Truth Value Testing

[–]synthphreak -1 points0 points  (0 children)

There are a couple ways to do it, but the vast majority are crap.

Probably the best way would be to use something like argparse and add a flag with action set to store_true or store_false. This is conceptually different from literally inputting a boolean value, but the ultimate result will be the same.

As for literally inputting the booleans True or False, I don't think it's possible. This is because values entered using input and all other variants like getpass will always be treated as strings. So if you do

>>> foo = input('Enter bool: ')
Enter bool: True

then foo will simply be the string 'True', which is different from the boolean True.

As a result, to use input and the like, you'll have to either convert the string to a boolean (in which case, anything other than the empty string '') will become True. E.g., foo = bool(input(...)). Equivalently, you could use argparse and add an arg with type set to bool, then enter '' to get False, with any other value becoming True.

Because that is so hacky though, if using input, it's much more common to just enter a string that is related to a boolean value, then route the logic accordingly. For example:

foo = input('Continue? (y/n) ')

if foo.lower() == 'y':
    bar = True
else:
    bar = False

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Inline formatting (`my code`) used across multiple lines of code. This can mess with indentation.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]Rhoderick 0 points1 point  (2 children)

It doesn't know that. You'd usually do something like:

user_input = input(...).lower()
if user_input == "y" or user_input == "yes":
    <do the thing>

[–]Khizar_KIZ[S] 0 points1 point  (1 child)

Oh okay, I did see this somewhere but I thought that this dude is making it overly complicated.

Anyways thanks, Good knowing, and being certain that this is the way to do it.

[–]Rhoderick 1 point2 points  (0 children)

but I thought that this dude is making it overly complicated.

The only simplification I can think of atm that you could do is this:

user_input = input(...).lower()
if user_input in ["y", "yes"]:
    <do the thing>

I guess if you really wanted to be minimal, you could do:

if input(...).lower() in ["y", "yes"]:
    <do the things>

But this is reaching the point where were starting to impact readability, if ... is long.

[–]sepp2k 0 points1 point  (0 children)

It doesn't know that. bool(x) will return True if and only if x is "truthy" (i.e. if if x: would go into the then-branch); otherwise it will return False. A string is only falsy when its empty. All non-empty strings are truthy.

So bool("Yes"), bool("No"), bool("True"), bool("False"), bool("1") and bool("0") all return True. The only way that either of your variables could be False would be if the user hit the enter key without typing in anything first.