-2

When I give the input to the input function it just shows the message from the else statement output after completing giving input to the if and elif statements. So, this is the code:

import turtle
my_turtle = turtle.Turtle()
def heart():
    my_turtle.color('red')
    my_turtle.left(140)
    my_turtle.forward(150)
    my_turtle.circle(-60, 190)
    my_turtle.left(100)
    my_turtle.circle(-60, 190)
    my_turtle.forward(150) 
favorite_song = input('Which song do you listen the most: ')
Normal_song = input('Which song is your normally heard song: ')
if favorite_song == True:
    print(favorite_song + ' is your favorite song')
    print(heart())
elif Normal_song == True:
    print(Normal_song + ' is your normally heard song')
else:
    print('NO INPUT GIVEN')
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • `favorite_song == True` will always be `False` since `favorite_song` is a string. Do you mean to check the truthiness of `favorite_song`, e.g. `if favorite_song:` ? – Brian61354270 Aug 13 '20 at 17:09
  • Does this answer your question? [What is Truthy and Falsy? How is it different from True and False?](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false) – Albert Rothman Aug 13 '20 at 17:27
  • You want to check the truthiness of the string, not that it equals true. An empty string is "falsey", while other strings are truthy. Delete `==true` and all should work as expected. – Albert Rothman Aug 13 '20 at 17:28
  • Welcome to Stack Overflow. Did you already read [how to ask a good question](https://stackoverflow.com/help/how-to-ask)? It explains that you should post a [mre]. – wovano Aug 17 '20 at 12:22

2 Answers2

1

That happens because you're comparing a boolean with a String (the String is what you get in the input), so you have

True == 'True'

if you want to check that favorite_song and Normal_song are not None, you have to write:

if favorite_song:
    print(favorite_song + ' is your favorite song')
    print(heart())
elif Normal_song:
    print(Normal_song + ' is your normally heard song')
else:
    print('NO INPUT GIVEN')

I think you should replace your Normal_song with "normal_song" (lower case)

0

You are correct in saying that you are getting only else block output, because only else block is executing. This is because both of your comparisons are failing (both comparisons are resulting into false).

Here's why: In python, we have two concept, one is "truth value" which can be true or false, and the other is boolean value, which also can be truth or false.

Now, if you want your code to work, you should check truth value for both variables.

You can write:

if favorite_song:
    print(favorite_song + ' is your favorite song')
    print(heart())
elif Normal_song:
    print(Normal_song + ' is your normally heard song')
else:
    print('NO INPUT GIVEN')

and hopefully it will work. What you are doing is, you are comparing truth value of both variables (and this is what you are basically trying to do.) and if the truth value is returned, it will execute.

It will return "truth value" as false, if you enter empty string or None.

In that case, when you enter something, some non-empty string, it has truth value = True, so, the case becomes:

if True:  # favorite_song 's value
    print(favorite_song + ' is your favorite song')
    print(heart())
wovano
  • 4,543
  • 5
  • 22
  • 49
Hari Kishore
  • 2,201
  • 2
  • 19
  • 28