[Tutor] double ended queue

Atux Atux atuxnull at gmail.com
Sun Nov 5 03:07:31 EST 2017


Hi. Thanks a lot for the replies. i am actually nw to programming as well.
your code keeps asking the user for a number and puts it to the end. how
can i make it to add a number at the beginning if the user adds 01, then
the program it will strip 0 and add 1 at ythe beginning, please?

John


On Sun, Nov 5, 2017 at 3:31 AM, Martin A. Brown <martin at linux-ip.net> wrote:

>
> Hello and welcome duplicative Atux,
>
> >i am new to the area and i am struggling with a small project that i have.
> >i need some help please.
>
> New to Python, perhaps.  New to programming, as well?
>
> >i need to create a program that runs constantly unless the user
> >presses q to end it.
>
> This is a good way to learn how to handle loops which can handle
> various conditions.  I mention below a module called cmd, which
> ships as a part of the Python library, but it may be good to
> understand first how to handle the looping constructs before you
> move on to the cmd module.
>
> Consider a loop like you indicated:
>
> >while True:
> >    if input("\n\n\nType  a number to add it to the queue or q to exit:
> ") == 'q':
> >        break
>
> There are quite a few valuable lessons in this simple task.
>
>   * consider the what kind of data you are asking for; is it an
>     integer, a string or a floating point value?  Alan mentioned
>     that in his message
>   * be careful with the kind of data your user is entering, but it
>     also teaches you the value of knowing what the data type is (an
>     important lesson in most programming languages)
>   * how do you loop successfully and how do you break out of the
>     loop; you'll want to know about 'break' and you might find
>     'continue' useful, as well
>
> -------------------------------
> # -- Python3
> queue = list()
> status = ''
> instructions = "\n\nAdd a number to the queue or q to exit: "
>
> while True:
>
>     # -- convert the string inputs to int(), float() or whatever type you
> want
>     #    the program to operate on
>     #
>     response = input(status + instructions)
>     response = response.lower()  # -- lower case the string
>     if response == 'q':
>         break
>
>     #
>     try:
>         number = int(response)  # -- or float(response)
>     except ValueError:
>         status = "Ignoring non-integer input: " + response
>         continue
>
>     queue.append(response)
>     status = "queue = " + str(queue)
> -------------------------------
>
> >the program asks the user for a number and puts the number in a
> >queue, then it prints the queue with the new element at the end.if
> >the number is with 01,02 then it will be added at the left hand
> >side without the 0 at the beginning, otherwise at the right hand
> >side.
>
> Steven has demonstrated how you could use the list() data structure
> to do what you describe.  Note that Python comes with additional
> data structures and what you describe is known in Python as a deque.
> It's a bit less common of a data structure, so you could benefit
> from learning how to perform the prepend and append operations like
> Steven has suggested.
>
> If you haven't noticed that you can run the Python interpreter
> directly, you may find this helpful simply to play with the data
> structures.
>
> You can use a list() as you can see in the core documentation (and
> as Stephen mentioned), however, even in the main documentation is a
> pointer to the collections.deque, which is a more specific sort of
> data structure geared for exactly the use case you present.
>
> Important note;  If humans are interacting with this, though, it's
> unlikely that any of the efficiency gains of a deque over a list are
> necessary, but here are links to the documentation:
>
>   https://docs.python.org/3/tutorial/datastructures.html#
> using-lists-as-queues
>   https://docs.python.org/3/library/collections.html#collections.deque
>
> >the user can remove an item from the end of the queue by typing r.
> >if the user types '0r' it will be removed from the beginning of the
> >queue.
>
> Once you have the addition of items to the queue and the looping
> worked out to your satisfaction, maybe you could share your progress
> and there might be somebody to provide a bit more help on the next
> step of your learning
>
> >but i a stuck on how to continue asking and adding to the list
>
> Good luck,
>
> -Martin
>
> --
> Martin A. Brown
> http://linux-ip.net/
>


More information about the Tutor mailing list