Python Forum
adding a number to the list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding a number to the list
#1

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?
Reply
#2
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 conditions
Basically, 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.
Reply
#3
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.
Reply
#4
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?
Reply
#5
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.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 1,971 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  list digit into number Voldyy 2 1,496 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  Displaying list correspond to the column number danlopek14q 9 3,896 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  How to convert every even number in a list to odd? Bruizeh 4 3,670 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  Get the biggest number from a two dimensional list rs74 13 3,941 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,035 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,194 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  adding parts of a list Eric7Giants 4 2,676 Nov-17-2019, 05:53 PM
Last Post: buran
  Adding values to list and pickling mefiak 2 2,786 May-31-2018, 08:57 AM
Last Post: mefiak
  Multiplying number in a list in an order pythoneer 12 6,477 Mar-23-2018, 08:21 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020