Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 learning to while loop
#1
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.
Quote
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#3
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!".
Quote
#4
As I wrote - you should break out from loop. So instead of False use break
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#5
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?
Quote
#6
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')
...
Always
Usual practice for in this type of situations is use if answer.lower() == 'n':
iofhua likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#7
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.
Quote
#8
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:
Output:
['Green', 'Blue']
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:
Output:
Green, Blue
Hope this helps you Smile
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Can you help about my learning project Qusha 1 122 Jul-21-2019, 07:46 PM
Last Post: Yoriz
  Learning Python in a month MuhammadNauman 1 144 Jul-17-2019, 11:53 AM
Last Post: metulburr
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 141 May-18-2019, 09:29 PM
Last Post: Lastwizzle
  Question about for loop not creating an infinite loop. FWendeburg 1 303 Feb-03-2019, 08:45 PM
Last Post: ichabod801
  learning python and need help with an output. defective_programmer 3 384 Dec-16-2018, 11:37 PM
Last Post: snippsat
  loop through list or double loop 3Pinter 4 513 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  A Learning Neural Network (?) CoderMan 1 535 Oct-10-2018, 12:54 AM
Last Post: CoderMan
  Customised Learning bkpsusmitaa 4 806 Sep-28-2018, 10:44 AM
Last Post: bkpsusmitaa
  Help with learning threading please. jarrod0987 2 1,315 Sep-23-2018, 01:09 PM
Last Post: stullis
  So I was learning python Sanlus 8 705 Aug-26-2018, 12:28 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)