1

I'm a noob and probably dumb, but help is appreciated greatly. I want to know why the code finishes when it executes the "else" statement, I want it to continue the "while" loop.

import random
happy == "no"
while happy == "no":
    stat = random.randint(1,50)
    print (stat)
    happy = input("Are you happy with this?" )
    if happy == "yes":
        print("Okay moving on. ")
        break
else:
    print("Please enter a valid answer. ")
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
tmoney
  • 5
  • 2

3 Answers3

3

Your else should be at the same indent level of if, and you're trying to make a comparison (==) in the second line instead of defining the variable happy to be no (with =). This is the fixed code, which I could verify works for me in Python 3.7.4:

import random
happy = "no"
while happy == "no":
    stat = random.randint(1,50)
    print (stat)
    happy = input("Are you happy with this?" )
    if happy == "yes":
        print("Okay moving on. ")
        break
    else:
        print("Please enter a valid answer. ")

The else clause in while is for a specific purpose - somewhat counterintuitive even for non-beginner Python developers (let alone newcomers). You stumbled upon this construct "by accident", that's why your code didn't throw a SyntaxError and may have mislead you during debugging.

Rafael Almeida
  • 10,352
  • 6
  • 45
  • 60
  • Hi, sorry I formatted the code in my question wrong!! In my actual code the indentation is correct (the same as you posted) but the code still terminates after executing the "else" statement rather than re-rolling the "stat". – tmoney Jul 01 '20 at 16:41
  • The "else" statement is also triggered when the user enters "no" as an answer, even though it is a valid answer. I don't know why this is – tmoney Jul 01 '20 at 16:44
  • Worked correctly for me here with Python 3.7.4. I did have to change the second line to `happy = "yes"` (removing one `=`). But it threw an error before that, so this is unlikely to be it. – Rafael Almeida Jul 01 '20 at 17:58
  • Argh, just cant understand why it terminates! – tmoney Jul 01 '20 at 23:39
  • 1
    There’s a number of moving gears involved - Python version, operating system, IDE, etc. There’s also problems with the copy-and-paste: the indent came wrong at first and it's weird that the code ran at all with `==` in the second line. I would triple-check that you’re copy-pasting the code you’re actually running and also add as many information as you can to the question. Record your screen if you must. With what I’m seeing in the question, there’s not much I (or anyone) can do to help diagnose your issue. – Rafael Almeida Jul 02 '20 at 01:13
  • My friend fixed the code by changing my While Loop. He changed "while happy == "no" to "while True". And he explained that the reason it didn't work before was because the "else" statement was still changing the "happy" variable, because I asked the user to input a string for it in the previous "if" statement. Thanks for all help. – tmoney Jul 02 '20 at 14:42
  • That would work as well, but I still have no idea why the code I posted works for me and not for you – Rafael Almeida Jul 02 '20 at 14:43
1

The answer is because your indentation. Indentation is very important in Python.

What you want to do is move the else statement to the same indentation level of if, so that way the logic says:

Check if this condition is true, otherwise (else), do this

A simple indent should fix your problem.

import random
happy = "no"
while happy == "no":
    stat = random.randint(1,50)
    print (stat)
    happy = input("Are you happy with this?" )
    if happy == "yes":
        print("Okay moving on. ")
        break
    else:
        print("Please enter a valid answer. ")
artemis
  • 6,857
  • 11
  • 46
  • 99
  • Hi, sorry I formatted the code in my question wrong!! In my actual code the indentation is correct (the same as you posted) but the code still terminates after executing the "else" statement rather than re-rolling the "stat". – tmoney Jul 01 '20 at 16:45
  • The "else" statement is also triggered when the user enters "no" as an answer, even though it is a valid answer. I don't know why this is – tmoney Jul 01 '20 at 16:45
0

My friend fixed the code by changing my While Loop. He changed "while happy == "no" to "while True". And he explained that the reason it didn't work before was because the "else" statement was still changing the "happy" variable, because I asked the user to input a string for it in the previous "if" statement.

tmoney
  • 5
  • 2