# Help with creating an If /Then statement in python

**URL:** <https://discuss.python.org/t/help-with-creating-an-if-then-statement-in-python/64398>\
**Category:** Python Help\
**Created:** [September 19, 2024, 5:10am UTC](https://discuss.python.org/t/help-with-creating-an-if-then-statement-in-python/64398 "2024-09-19T05:10:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SquatchHunter](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@SquatchHunter](https://discuss.python.org/u/SquatchHunter)\
**Post date:** [September 19, 2024, 5:10am UTC](https://discuss.python.org/t/help-with-creating-an-if-then-statement-in-python/64398/1 "2024-09-19T05:10:15Z")

</div>

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

```python
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')

```

---

<div class="post-metadata">

**Author:** ![skirpichev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/skirpichev/32/10996_2.png) [@skirpichev](https://discuss.python.org/u/skirpichev)\
**Post date:** [September 19, 2024, 5:47am UTC](https://discuss.python.org/t/help-with-creating-an-if-then-statement-in-python/64398/2 "2024-09-19T05:47:26Z")

</div>

I would suggest you to start with the tutorial, especially this section: [4. More Control Flow Tools — Python 3.12.6 documentation](https://docs.python.org/3/tutorial/controlflow.html#if-statements)

---

<div class="post-metadata">

**Author:** ![avi.gross](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@avi.gross](https://discuss.python.org/u/avi.gross)\
**Post date:** [September 19, 2024, 5:56am UTC](https://discuss.python.org/t/help-with-creating-an-if-then-statement-in-python/64398/3 "2024-09-19T05:56:17Z")

</div>

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.

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

```

In your code, lines like this:

```python
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:

```python
# 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:

```python
# ...
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.

---

<div class="post-metadata">

**Author:** ![c-rob](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/c-rob/32/17261_2.png) [@c-rob](https://discuss.python.org/u/c-rob)\
**Post date:** [September 19, 2024, 11:38am UTC](https://discuss.python.org/t/help-with-creating-an-if-then-statement-in-python/64398/4 "2024-09-19T11:38:07Z")

</div>

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.

```python
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.

```python
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")

```
