Help with creating an If /Then statement in python

Hello all. I have another question about my code. I have been trying to write a code that asks what your favorite color is, and then asking if you have any other favorite colors. I have part of my code hopefully correct for a yes answer, but I’m struggling on creating a no answer. I’ve tried to use “Else” but that just repeats my code for a yes answer. My code looks like this

from tkinter import YES
from tkinter.messagebox import NO


color = input('what is your favorite color?')
shade = input ('do you like the dark or light shade of the color?')
print('okay your favorite color is')
print(shade, color)
color = input('Is there any other color you like?')
if YES:
print('What color?')
color = input('My other favorite color is')
print( color, 'is a good color')

I would suggest you to start with the tutorial, especially this section: 4. More Control Flow Tools — Python 3.12.6 documentation

Sam,

SHORT REPLY: See the proper way to write your IF query and how to compare things, below.

I won’t advise you on using tkinter and have no idea why you include it, at least in the code shown. However, the YES/NO you imported may not at all mean what you think.

>>> from tkinter import YES
>>> from tkinter.messagebox import NO
>>> YES
1
>>> NO
'no'

In your code, lines like this:

color = input('what is your favorite color?')

Will return a character string of whatever you typed in, but not the newline you typed. As such, comparing them should be to another character string unless you convert them to a character.

And, please note you ask for the color twice and all the second one does is replace the first. You could have put it in a different variable name like anotherColor.

You asked how to do an if/then. Strictly speaking, python does not use the keyword then. It uses indentation of lines after a line that ends with a colon.

Your code would look more like this:

# NOTE CODE IS WRONG
if YES:
    print('What color?')
    # more such code as needed

# Code after the IF completes 
# indented back out after a blank line.

But I not an error in your approach.

The if statement in python is quite flexible but my bet is you meant to ask if the variable called color in your code currently, matches what YES contains. Unfortunately, it seems YES is mapped to the numeric digit 1. By the rules of the Python Gods, any integer other than 0 is deemed true, or I should say truthy. so your if statement is inevitably true.

Before I supply code, decide what the user should type when asked if they want another color. An example would be to expect exactly “yes” in all lowercase. You can match more complex things when ready. But you need to tell the if statement what you want. Consider my modification of your code:

# ...
another = input('Is there any other color you like? [yes] or [no]')

if another == "yes":
    print('What color?')
    color2 = input('My other favorite color is: ')
    print( color2, 'is a good color')

# Any additional code

If I misunderstood your purpose, someone else may explain better. I assume the assignment contains more such as what to do with two or more colors, …

Good Luck.

You don’t have the proper indentation, and indentation is critical in Python. You must also compare your string variable color to a value. Try this.

if color=="YES":
    print('What color?')
    color = input('My other favorite color is')
    print( color, 'is a good color')
elsif color=="NO":
    print("You said NO")

Note that in the code I wrote above, these strings will not do anything if the user enters them: “Yes”, “yes”, “No”, “no”. To fix that we will check the answer in the variable “color” as uppercase. And we will address other answers as an invalid answer.

if color.upper()=="YES":
    print('What color?')
    color = input('My other favorite color is')
    print( color, 'is a good color')
elsif color.upper()=="NO":
    print("You said NO")
else: 
    print("Invalid answer")