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 = 
userChoice = raw_input("Enter a number to insert into list (enter 'q' to exit): ")
if userChoice == "q":
print "Here's the contents of your list!"
for i in listItems:
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.