Re: [Tutor] double ended queue

2017-11-05 Thread Atux Atux
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  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/
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] double ended queue

2017-11-05 Thread Steven D'Aprano
On Sun, Nov 05, 2017 at 10:07:31AM +0200, Atux Atux wrote:
> 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?

This sounds like homework. We don't do people's homework for them. What 
have you tried?

Hint: how to tell whether a string ends with "01" or not.

if the_string.endswith("01"):
print("insert the_string at the beginning")
else:
print("append the_string at the end")


Second hint. Try running this in the Python interpreter and see what it 
does:

the_string = "1234501"
the_string = the_string[:-2]
print(the_string)


Third hint: I already showed you how to insert at the beginning, or 
append at the end, of the queue.

We can provide you the pieces of the puzzle, but you have to put them 
together.



-- 
Steve

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] double ended queue

2017-11-05 Thread Atux Atux
ok then. how do i link 'r' to remove an item from right with mylist.pop(0)
and how can i remove an item from left with '0r'?

On Sun, Nov 5, 2017 at 11:27 AM, Steven D'Aprano 
wrote:

> On Sun, Nov 05, 2017 at 10:07:31AM +0200, Atux Atux wrote:
> > 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?
>
> This sounds like homework. We don't do people's homework for them. What
> have you tried?
>
> Hint: how to tell whether a string ends with "01" or not.
>
> if the_string.endswith("01"):
> print("insert the_string at the beginning")
> else:
> print("append the_string at the end")
>
>
> Second hint. Try running this in the Python interpreter and see what it
> does:
>
> the_string = "1234501"
> the_string = the_string[:-2]
> print(the_string)
>
>
> Third hint: I already showed you how to insert at the beginning, or
> append at the end, of the queue.
>
> We can provide you the pieces of the puzzle, but you have to put them
> together.
>
>
>
> --
> Steve
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] double ended queue

2017-11-05 Thread Alan Gauld via Tutor
On 05/11/17 15:05, Atux Atux wrote:
> ok then. how do i link 'r' to remove an item from right with mylist.pop(0)
> and how can i remove an item from left with '0r'?

Same way you detected the 'q' to quit.
Using a variation of the 'code' I sent earlier:

response = input().lower() # allow user some latitude
if response == 'q':
   break
elif response == 'r':
   # remove right
elif response == '0r':
   # remove left
else:
   number = int(response)  # or do we need float???


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor