learning to while loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: learning to while loop (/thread-18593.html) |
learning to while loop - iofhua - May-23-2019 colours = [] while repeat = True colours.append(input('Please enter a favourite color:')) answer = input('Would you like to add another favourite color? (Y/N)') if answer = "N" or "n": repeat = False else: print("More colours!") print("So your favourite colours are:") print(colours)I've been doing google searching trying to find out why my while loop gets a syntax error. It's pointing at "while repeat = true" but I don't understand why it wouldn't work. I've looked at other websites and while they aren't using the True/False boolean like I am, I should be able to do it this way shouldn't I? I tried parentheses "while(repeat = True)" but that didn't work either. I've been learning from a no starch press book (Python Crash Course) but this isn't from the book. It's more free-form so there's not much I can do at this point except ask for help. RE: learning to while loop - perfringo - May-23-2019 You are trying to assign instead of comparing. You could make it work with == , but better is to use while True: and break out of loop if condition is met.
RE: learning to while loop - iofhua - May-23-2019 colours = [] while True: colours.append(input('Please enter a favourite color:')) answer = input('Would you like to add another favourite color? (Y/N)') if answer == "N" or "n": False else: print("More colours!") print("So your favourite colours are:") print(colours)It works now but it's not behaving as expected. When I type "N" shouldn't the line "False" end the loop? Also when I enter "Y" or anything other than "N" I expect it to say "More colours!" and then repeat the loop. It repeats the loop, but never prints "More colours!". RE: learning to while loop - perfringo - May-23-2019 As I wrote - you should break out from loop. So instead of False use break
RE: learning to while loop - iofhua - May-23-2019 colours = [] while True: colours.append(input('Please enter a favourite color:')) answer = input('Would you like to add another favourite color? (Y/N)') if answer == "N" or "n": break else: print("More colours!") print("So your favourite colours are:") print(colours)"break" certainly does break the loop, the problem now is that I entered "Y" to add another colour, but it broke out of the loop anyway. It should have skipped that. Is my if statement not working right? I think I would rather assign a variable to the while loop and set the variable to true or false. This way I can search the program for the name of the variable rather than "while" and "break" which would make things easier if the program is much larger. Is there any way I can do that? RE: learning to while loop - perfringo - May-23-2019 You should be either more careful with your code or learn some fundamentals. Your if conditional is always True , so else clause never executed. Why? You have or in if statement. This means that at least one comparison must be true. First comparison is answer == 'N' . Second comparison is 'n' and this is always true (read about True value testing). >>> if 'n': ... print('Always') ... AlwaysUsual practice for in this type of situations is use if answer.lower() == 'n':
RE: learning to while loop - iofhua - May-23-2019 colours = [] start = True while start == True: colours.append(input('Please enter a favourite color:')) answer = input('Would you like to add another favourite color? (Y/N)') print(answer) if answer == "N" or answer == "n": start = False print(start) else: print("More colours!") print("So your favourite colours are:") print(colours)This works as expected. I had several problems. One was the syntax for the while loop, and the other was a misunderstanding about how I was using the "or" statement. I had to define the variable I assigned to the while loop beforehand. I assumed it would intuitively use the variable when I first typed it in using the while statement, and I think that was causing me a lot of my grief. Thanks for all your help. RE: learning to while loop - DarkCraftPlayz - May-24-2019 Just in case you were wondering how to do it without a variable. colours = [] while True: colours.append(input('Please enter a favourite color: ')) answer = input('Would you like to add another favourite color? (Y/N)') print(answer) if answer.lower() == 'n': break else: print("More colours!") print("So your favourite colours are:") print(colours)The output for the above code will be: Just in case you want to make your output look a little better, you can try this:colours = [] while True: colours.append(input('Please enter a favourite color: ')) answer = input('Would you like to add another favourite color? (Y/N)') print(answer) if answer.lower() == 'n': break else: print("More colours!") print("So your favourite colours are:") print(', '.join(colours))This will have the following output: Hope this helps you
|