Allowing a letter to exit populating a list

This is the place for queries that don't fit in any of the other categories.

Allowing a letter to exit populating a list

Postby sc25893 » Sat Jun 08, 2013 12:00 pm

Hi everyone,

I'm new to python and working through how to populate a list. Here's the code:

Code: Select all
#build a list in python 2.7

listItems = []

while True:
    userChoice = raw_input("Enter a number to insert into list (enter 'q' to exit): ")
   
    if userChoice == "q":
        break
    else:
        listItems.append(userChoice)
       
print "Here's the contents of your list!"

for i in listItems:
    print "{0},".format(i),


The problem I have is that users can enter anything, number, letter or just press 'Enter'.

Solutions I have tried:

1) Using int(raw_input("Enter a number to insert into list (enter 'q' to exit): ")). This then does not allow 'q' to exit.
2) The 'try' statement. I couldn't work out how to use it properly in this case.

Here is the link I used: http://docs.python.org/2/tutorial/errors.html

I got it working in the sense that a user could ONLY enter numbers, anything else didn't work, but I wanted to be able to quit using the 'q' character.

Any help on this would be most appreciated, thanks.
sc25893
 
Posts: 23
Joined: Sat May 18, 2013 2:51 pm

Re: Allowing a letter to exit populating a list

Postby micseydel » Sat Jun 08, 2013 12:24 pm

Code: Select all
vals = []

user_in = raw_input('> ')
if user_in == 'q':
    pass
else:
    try:
        num = int(user_in)
        vals.append(num)
    except ValueError:
        pass

print vals

You need simply call int() on the value returned by raw_input() after checking if it is equal to "q" rather than trying to do it immediately. After that check, you can call int() inside the try/except.

I'm attempting to give you an example you can learn from without giving you the whole thing, if this isn't enough, give us your best code attempt and explain what it doesn't do that you need it to, and ask as specific of questions as you can.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1355
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Allowing a letter to exit populating a list

Postby sc25893 » Sat Jun 08, 2013 12:40 pm

Superb, thanks! This is what I've got:

Code: Select all
#build a list in Python v2.7

listItems = []

while True:
    userChoice = raw_input("Enter number: ")
   
    if userChoice == "q":
        break
    else:
        try:
            num = int(userChoice)
            listItems.append(num)
        except ValueError:
            print "You need to enter a number"
       
print "Here's the contents of your list!"

for i in listItems:
    print "{0},".format(i),


The one remaining problem is how to print the list like this:

12, 44, 433, 322

instead of:

12, 44, 433, 322,

Code: Select all
#build a list in Python v2.7

listItems = []

while True:
    userChoice = raw_input("Enter number: ")
   
    if userChoice == "q":
        break
    else:
        try:
            num = int(userChoice)
            listItems.append(num)
        except ValueError:
            print "You need to enter a number"
       
print "Here's the contents of your list!"
print str(listItems).strip('[]')


This does it but I'm not sure the difference of iterating through the list as in the first example and converting to a string in the latter example. Is there a 'preferred' way of doing this or is it a matter of taste?

Thanks.
sc25893
 
Posts: 23
Joined: Sat May 18, 2013 2:51 pm

Re: Allowing a letter to exit populating a list

Postby Yoriz » Sat Jun 08, 2013 12:47 pm

if the items are strings already
Code: Select all
the_items = ['12', '44', '433', '322']
print ', '.join(the_items)

if not
Code: Select all
the_items = [12, 44, 433, 322]
print ', '.join(map(str, the_items))
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 832
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Allowing a letter to exit populating a list

Postby sc25893 » Sat Jun 08, 2013 3:25 pm

Yoriz wrote:if the items are strings already
Code: Select all
the_items = ['12', '44', '433', '322']
print ', '.join(the_items)

if not
Code: Select all
the_items = [12, 44, 433, 322]
print ', '.join(map(str, the_items))


Ok I see, thanks.
sc25893
 
Posts: 23
Joined: Sat May 18, 2013 2:51 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], snippsat and 3 guests