1

EDIT - Apparently this is a duplicate. And while I've no doubt the core problems are answered elsewhere I think that the level is pretty blatant and thus the ability to comprehend other solutions and transpose them into my own problem is diminished. I searched before asking.

I'm just having a mess about and was trying to write a little thing with user input.

However, I'm not sure how to go about this without knowing how many iterations are needed, or having two questions?

I tried to modify it to take an if condition, which I don't really want anyway but that didn't work either :

for i in range(50):
    userIn = raw_input()
    urlList.append(userIn)
    print 'Continue? Y/N'
    ynAns = raw_input()
    if ynAns == 'n' or 'N':
        break

Basically I'm just trying to take user input to fill up a list and then print it out.

I also tried

import sys

listOne = []
num = int(raw_input('How many'))

for x in range(num):
    listOne.append(raw_input(('Entry #' + x+1 + ' '))


print listOne

pretty basic

baxx
  • 3,956
  • 6
  • 37
  • 75

3 Answers3

1

You need to compare ynAns with both 'n' and 'N':

if ynAns == 'n' or ynAns == 'N':

An alternative syntax:

if ynAns in ('n', 'N'):

The reason why your if statement doesn't work is that ynAns == 'n' and 'N' are two separate expressions. 'N' is always evaluated to True, so the if condition is always true.

jh314
  • 27,144
  • 16
  • 62
  • 82
  • 1
    cheers... I didn't think / realise that they worked like that. Nice one for the tips – baxx Aug 21 '14 at 14:39
1

It's basically jh314's answer, but shorter:

if ynAns.lower() == 'n':

What it does is converts ynAns to lowercase, making your code more concise.

Beta Decay
  • 805
  • 1
  • 8
  • 20
1

Dont use a for loop for this, you´re restricting your app to run within a limit of 50 iterations, instead use while:

userInput = None
userInput = raw_input("Enter input (N or n to stop): ")
while(userInput not in ['N', 'n']):
    urlList.append(userIn)
    userIn = raw_input("Enter input (N or n to stop): ")
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
  • Yeah I didn't want to use a for loop (because of the reasons you state...) I see how your's works though, I'm going to have a play with that, thanks – baxx Aug 21 '14 at 14:40