adding a number to the list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: adding a number to the list (/thread-6063.html) |
adding a number to the list - atux_null - Nov-04-2017 i need to create an empty list and by asking the user to input one number every time to update the list. up to now i have: while True: if input("\n\n\nPlease enter a number to add it to the queue or press q to end the game: ") == 'q': break MyList = [] num = int(input("Please type a number: "))-the number will be added to the beginning of the queue if it has a 0, eg 01,02..But it will be added without the 0. otherwise at the end of the queue -the user can remove an entry at the end of the queue by typing r. If the user types 0r then it will be removed from the beginning of the queue. Some help please? RE: adding a number to the list - Lux - Nov-04-2017 Well, you have your number input outside of the loop, and declare your list after it's done executing. For what you described, the basic structure of your program might look like this: MyList = [] while True: userInput = input("string that asks for a number or gives directions") if userInput == 'q': break elif #other conditionsBasically, you will continue asking for input until the user types in 'q'. The elif statement could be the first of several, checking for each of the different conditions you described. RE: adding a number to the list - Larz60+ - Nov-05-2017 int 00001 is the same as int 1. If you want 01, use a string '01', but then it must be converted to int for any math operations. RE: adding a number to the list - atux_null - Nov-05-2017 i have changed my code and i have the following: import collections queue = list() status = '' instructions = "\n\nPlease enter an integer to add to queue or q to exit: " while True: response = input(status + instructions) response = response.lower() if response == 'q': break if response == 'r': #remove a number from right queue.pop() if response =='0r': #remove a number from left queue.popleft() # try: number = int(response) except ValueError: status = "Non integers are ignored: " + response continue queue.append(response) status = "queue = " + str(queue) #print(queue)issues up to now. The 0r and r are not working at all. also everything is getting added at the right end of the queue. Need to make 0r and r to work, as well the number with a zero prefix eg 01, to remove the 0 and add the 1 at the left (beginning). Please some help on the above code? RE: adding a number to the list - gruntfutuk - Nov-06-2017 popleft is pop(0). Currently, after you have done your pop, you ALSO do the try - except block and the append, which is not what you want to do having already processed the input. |