#πŸ”’ How to run an "if" statement again

32 messages Β· Page 1 of 1 (latest)

rich rover
#

So I'm quite rusty on Python and I've done this before but I'm not sure how, and for the life of me I can't figure out what to search to come up with an answer.

My task is to allow the user to enter in as many numbers as they want and append those values to a list, then once they've finished adding the numbers they want, I present them with an average of all the numbers.

Everything is going perfectly but I'm just unsure how to repeat an if statement.

The area I need help on is between lines 94 and 104. I want to be able to repeat this over and over again until the user is finished, and then once they finish, run it one more time to show them the average.

#Activity 8
finish = False
numbers = []
while finish == False and len(numbers) > 0:
    avg = sum(numbers) / len(numbers)

num = input("Enter the number you would like to add: ")

if finish == True:
    print("Your final average is:", avg)
else:
    finishCheck = input("Are you finished adding to the list ")
    print()
    if finishCheck == "No" or "no" or "n":
        num = input("Enter the number you would like to add: ")
    elif finishCheck == "Yes" or "yes" or "y":
        finish == True
    else:
        print("Not a valid repsonse. ")

I know this is a pretty simple question but I would appreciate any help πŸ™‚

junior slateBOT
#

@rich rover

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

shut saddle
#

!indent

junior slateBOT
#
Indentation

Indentation is leading whitespace (spaces and tabs) at the beginning of a line of code. In the case of Python, they are used to determine the grouping of statements.

Spaces should be preferred over tabs. To be clear, this is in reference to the character itself, not the keys on a keyboard. Your editor/IDE should be configured to insert spaces when the TAB key is pressed. The amount of spaces should be a multiple of 4, except optionally in the case of continuation lines.

Example

def foo():
    bar = 'baz'  # indented one level
    if bar == 'baz':
        print('ham')  # indented two levels
    return bar  # indented one level

The first line is not indented. The next two lines are indented to be inside of the function definition. They will only run when the function is called. The fourth line is indented to be inside the if statement, and will only run if the if statement evaluates to True. The fifth and last line is like the 2nd and 3rd and will always run when the function is called. It effectively closes the if statement above as no more lines can be inside the if statement below that line.

Indentation is used after:
1. Compound statements (eg. if, while, for, try, with, def, class, and their counterparts)
2. Continuation lines

More Info
1. Indentation style guide
2. Tabs or Spaces?
3. Official docs on indentation

dire spear
#

!or-gotcha

junior slateBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
shut saddle
#

you want your if checking done inside the while loop so it will be run multiple times

#

and you also want your avg calculation outside the while loop so i will only be calculated after you loop all your values

rich rover
# shut saddle and you also want your avg calculation outside the `while` loop so i will only b...

I've change it to this which works now

finish = False
numbers = []
num = int(input("Enter the number you would like to add: "))
numbers.append(num)
while finish == False and len(numbers) > 0:
    if finish == True:
        print("Your final average is:", avg)
    else:
        finishCheck = input("Are you finished adding to the list ")
        print()
        if finishCheck == "No" or "no" or "n":
            num = input("Enter the number you would like to add: ")
            numbers.append(num)
        elif finishCheck == "Yes" or "yes" or "y":
            finish = True
            break
        else:
            print("Not a valid repsonse. ")


if finish == True:
    avg = sum(numbers) / len(numbers)
    print("Your final average is: ", avg)

but the loop does not break when I'm finished appending

shut saddle
#

you while loop should only check for the status, you are now calculating the whole thing twice

rich rover
#
finish = False
numbers = []
num = int(input("Enter the number you would like to add: "))
numbers.append(num)
while finish == False and len(numbers) > 0:
    finishCheck = input("Are you finished adding to the list ")
    print()
    if finishCheck == "No" or "no" or "n":
        num = input("Enter the number you would like to add: ")
        numbers.append(num)
    elif finishCheck == "Yes" or "yes" or "y":
        finish = True
        break
    else:
        print("Not a valid repsonse. ")


if finish == True:
    avg = sum(numbers) / len(numbers)
    print("Your final average is: ", avg)
#

i dont think thats the problem

dire spear
shut saddle
#

you need if finishCheck == "No" or finishCheck == "no" or finishCheck == "n":

dire spear
#

or alternatively, if finishCheck.lower() in ("no", "n"):

dire spear
shut saddle
#

if finishCheck.lower() in ("no", "n"):

dire spear
#

what quote?

shut saddle
#

"no", "n"

dire spear
shut saddle
#

you missed the middle quotes

#

not a native speaker, but you got my point

dire spear
#

not until I asked you to clarify πŸ€·β€β™‚οΈ

rich rover
#

I've done that but now I get this error which i know what it means but im not sure how to fix it: avg = sum(numbers) / len(numbers)
^^^^^^^^^^^^
TypeError: unsupported operand type(s) for +: 'int' and 'str'

finish = False
numbers = []
num = int(input("Enter the number you would like to add: "))
numbers.append(int(num))
while finish == False and len(numbers) > 0:
    finishCheck = input("Are you finished adding to the list ")
    print()
    if finishCheck == "No" or finishCheck == "no" or  finishCheck == "n":
        num = input("Enter the number you would like to add: ")
        numbers.append(num)
    elif finishCheck == "Yes" or finishCheck == "yes" or finishCheck == "y":
        finish = True
        break
    else:
        print("Not a valid repsonse. ")


if finish == True:
    avg = sum(numbers) / len(numbers)
    print("Your final average is: " + avg)

dire spear
shut saddle
#

hes doing calculation so i think the number part is fine, just use str() when doing the print

rich rover
#

adding int() to that worked thankyou for the help!

#

It works well now thankyou guys

dire spear
junior slateBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.