0

Hello I have recently started learning python3 and in the course I'm doing it asks me to do a if-else statement with bool variable.

I have written my code and checked off all the criteria for the code need but it keeps saying I should define a Boolean variable and I can't for the life of me figure out what it wants me to do.

Here is my code :

rain = bool(input('is it raining outside?: ').lower())
if rain == "Yes":
  print("I'm going to dance in the rain!")
else:
  print ("I'm going to dance in the sun!")
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • *Hint:* `rain == "Yes"` is a boolean value. – Sayandip Dutta Jun 10 '21 at 19:55
  • 2nd hint - `rain` will NEVER be equal to "Yes" ... you do a `.lower()` on its content – Patrick Artner Jun 10 '21 at 19:56
  • Please, post your assignment verbatm – buran Jun 10 '21 at 19:56
  • 2
    "it keeps saying I should define a Boolean variable" What is saying that? – MisterMiyagi Jun 10 '21 at 19:56
  • 1
    `is_it_raining = rain == "yes"` would be a boolean variable.... – Patrick Artner Jun 10 '21 at 19:57
  • `rain` will always be `True`, unless user input is empty str in which case it will be `False`. – buran Jun 10 '21 at 19:59
  • What exactly is "it"? Are you using some sort of automated practice / exam system? Note that a boolean variable in Python must have value of one of the reserved words `True` or `False` not a string of "yes" or "no". While other value can be evaluated to `True` or `False` , like `0 == False` an automated system would probably expect one of these values to appear directly in code. – Lev M. Jun 10 '21 at 20:04
  • Does this answer your question? [Converting from a string to boolean in Python?](https://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python) – mkrieger1 Jun 10 '21 at 20:06
  • `It keeps saying I should define a Boolean variable` - could you post a screenshot of that message ? – TheEagle Jun 10 '21 at 20:10
  • How does a question like this get closed? It's clear, concise, provides example of attempt, is likely to help other people in the future, and is very answerable. OP has a requirement to provide a boolean variable as the condition for the `if` statement and is confused about how to do that in Python. – JNevill Jun 10 '21 at 20:11
  • And it's already been asked before. – mkrieger1 Jun 10 '21 at 20:12
  • @mkrieger1 That is a fair point, should this have been closed as a duplicate I would have no beef. At any rate, I'll take it to meta.stackoverflow.com and stop complaining here about knee-jerk closing like this. – JNevill Jun 10 '21 at 20:14
  • 1
    @JNevill This *would* be a simple case if a good portion of the question weren't about "it" saying ominous things that need to be respected. There are obviously some unknown requirements, and unless these are known any answer is guesswork. Closing it until it is in shape to be answered seems *exactly* like the thing to do. – MisterMiyagi Jun 10 '21 at 20:41

3 Answers3

1

This does not work, because:

In Python, bool(…) returns True for all non-empty strings. Example:

>>> bool("")
False
>>> bool("foo")
True
>>> bool("yes")
True
>>> bool("no")
True
>>> bool("False")
True

To resolve this problem, you can

  1. define your own str_bool function, e.g.
def str_bool(s):
    if s.lower() in ["y", "yes", "yeah", "true", "positive", "1"]:
        return True
    elif s.lower() in ["n", "no", "nope", "false", "negative", "0"]:
        return False
    else:
        raise ValueError("string %s is not bool-able" % s)
  1. Assign the result of a condition evaluation to your rain variable:
rain = input('is it raining outside?: ').lower() in ["yes", "y", "yeah", "positive", "1"]
if rain:
  print("I'm going to dance in the rain!")
else:
  print ("I'm going to dance in the sun!")

The second one of course does not handle the case that the user inputs maybe - the first one is the clearly better and cleaner one !

TheEagle
  • 5,808
  • 3
  • 11
  • 39
  • @MisterMiyagi taken at face value it's pretty simple to see that a boolean variable is not being used in the condition of OP's `if` statement attempt. While a little round-about, this answer satisfies that requirement. `if rain:` where `rain` is a boolean variable is the correct answer. – JNevill Jun 10 '21 at 20:12
  • @JNevill Since Python itself does not emit that error, it's entirely unclear whatever tool inspecting the code expects. The question *already* did define a boolean variable, and that obviously wasn't enough. – MisterMiyagi Jun 10 '21 at 20:43
0

You convert your input to a boolean and compare it then with a string.

rain should be compared on assignement to have a boolean:

rain = input('is it raining outside?: ').lower() == "yes"
if rain:
  ...

or rain should just not be a boolean:

rain = input('is it raining outside?: ').lower()
if rain == "yes":
  ...
TheEagle
  • 5,808
  • 3
  • 11
  • 39
signedav
  • 146
  • 1
  • 4
-1

I have made a small modification, for this:

rain = input('is it raining outside?: ')
if rain.lower() == "yes":
    print("I'm going to dance in the rain!")
else:
    print ("I'm going to dance in the sun!")

You do not need to declare as a bool. Just place the answer as lower, and the check with the 'Yes' statement. Like this, whether is 'Yes' or 'yes', it will work!

bellotto
  • 445
  • 3
  • 13
  • 1
    What if the user answers "maybe"? Seriously though, I am not downvoting you, but this does not answer the question since there is clearly some requirement for storing actual boolean value in some variable. Please read the question carefully! – Lev M. Jun 10 '21 at 20:07