Re: [Tutor] Groups of mutually exclusive options
On Mon, Apr 21, 2014 at 08:26:41PM -0400, rail shafigulin wrote: > For example say I have a the script with called myscript.py which can take > two groups of options > > group1 > option1a > option1b > option1c > group2 > option2a > option2b > otpion2c > > I can run this script only with the following options > > myscript.py --option1a --option1b --option1c > or > myscript.py --option2a --option2b --option2c Normally the way to handle that with argparse is to define subcommands, and write something like this: myscript.py spam --option1a --option1b --option1c myscript.py eggs --option2a --option2b --option2c http://stackoverflow.com/questions/18046540/add-a-second-group-of-parameters-that-is-totally-mutually-exclusive-from-the-fir I don't think there is any directly supported way to handle it in argparse without the subcommands. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Groups of mutually exclusive options
On Mon, Apr 21, 2014 at 05:05:49PM -0400, rail shafigulin wrote: > Does anybody know if there is a way to specify groups of mutually exclusive > options using argparse module? I'm not an expert on argparse, but I think not. If I've understand correctly, this seems to suggest that argparse does not support what you want: http://stackoverflow.com/questions/4770576/does-argparse-python-support-mutually-exclusive-groups-of-arguments If I've misunderstood what you are after, there are very many variations on the theme of mutually exclusive groups of arguments on Stackoverflow, perhaps you can find something more appropriate. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Groups of mutually exclusive options
On Mon, Apr 21, 2014 at 05:37:28PM -0700, Alex Kleider wrote: > On 2014-04-21 14:05, rail shafigulin wrote: > >Does anybody know if there is a way to specify groups of mutually > >exclusive > >options using argparse module? > > > > As someone pointed out on this list some months ago, you might want to > consider using docopt instead of argparse. It is much more in keeping > with the SPoL philosophy of Unix. Does docopt solve the Original Poster's question? If not, that advice is not terribly helpful. By the way, I think you mean Single Point Of Truth, not Light. http://www.faqs.org/docs/artu/ch04s02.html SPOT, also known as DRY (Don't Repeat Yourself), is an excellent principle to follow, but in my experience it is often too difficult to follow religiously. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Groups of mutually exclusive options
On 2014-04-21 14:05, rail shafigulin wrote: Does anybody know if there is a way to specify groups of mutually exclusive options using argparse module? As someone pointed out on this list some months ago, you might want to consider using docopt instead of argparse. It is much more in keeping with the SPoL philosophy of Unix. http://docopt.org/ SPoL: Single Point of Light- see Eric Raymond's book http://en.wikipedia.org/wiki/The_Art_of_Unix_Programming ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Groups of mutually exclusive options
> > >> > Sorry, I didn't follow your example. Can you explain what you mean in > English? What would be the outcome if you succeeded? > What could the user do and not do? The idea is to use two groups of parameters. The user can use only one group of parameters. For example say I have a the script with called myscript.py which can take two groups of options group1 option1a option1b option1c group2 option2a option2b otpion2c I can run this script only with the following options myscript.py --option1a --option1b --option1c or myscript.py --option2a --option2b --option2c I cannot run run a script with the following options myscript --option1a --option2a So it is similar to having mutually exclusive options, however this is sort of on a larger scale. Instead of having mutually exclusive options, we would have mutually exclusive sets of options. Let me know if it didn't clarify the details. I will try to come up with a better explanation. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work
On 21/04/14 19:12, Stephen Mik wrote: ...I am inputting or trying to input,a Sentry Variable to a While Loop. I want to test out the Main program" While" Loop before I add an inner "While" Loop. The program I have written,when run on the Python 3.4.0 Shell,does not stop for input of the "While" Sentry Variable,it just gives a program error: "Value of smv_grandVariable undefined". What am I doing wrong here? > import random > ... > print("Do you want to play the game?\n") > print("Enter a 1 to play or 0 to exit:") > input(smv_grandVariable) You have completely misunderstood input... input takes as an argument a prompt string and returns the value input by the user so your usage should look like: smv_grandVariable("Enter a 1 to play or 0 to exit:") But that's a terrible name for a variable. You should name variables after their purpose. What does this variable represent? You say its a sentry? So call it sentry... Having the word "variable" in a variable name is nearly always a mistake. > while (smv_grandVariable == 1 and smv_grandVariable != 0): And your second mistake is that you have not converted the string typed by the user to a number(specifically an int) but you are comparing the variable to the numbers 0,1 Finally the logic of your test can be replaced by the simpler while int(smv_grandVariable) != 0: since 1 is also not zero. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work
Hi, On 21 April 2014 19:12, Stephen Mik wrote: > Dear Python Community: > I am new to Python,with only about a month's experience. I am writing > Python 3.4.0 code that apparently isn't doing what it should be doing. > Specifically, I am inputting or trying to input,a Sentry Variable to a While > Loop. I want to test out the Main program" While" Loop before I add an inner > "While" Loop. The program I have written,when run on the Python 3.4.0 > Shell,does not stop for input of the "While" Sentry Variable,it just gives a > program error: "Value of smv_grandVariable undefined". What am I doing wrong > here? You are misunderstanding how input() works. It is a function, which means it returns a result, and takes one parameter which is a prompt/message to display, e.g you should have something like this: result = input('Input a string:') This displays 'Input a string:' to the user and then waits for input, after which it puts the inputted value into the variable 'result'. That also hopefully explains the error message -- it's telling you that 'smv_grandVariable', which you've given to input() and which Python's duly trying to display is undefined. (By the way, try to pick a better name for that variable which suggests what its role is supposed to be.) Also see here: https://docs.python.org/3.4/library/functions.html#input Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error
On 21/04/14 19:41, Geocrafter . wrote: im trying to make a board, and is detecting the pieces. Here is my code:http://pastebin.com/L3tQLV2g And here is the error: http://pastebin.com/4FiJmywL Do you knwo hwo to fix it? You are passing in a cell location that results in an index out of range. Try figuring out what happens when x is 6 for example. BTW Cant you combine those two enormous if statements into one by just passing ionm the test character(x or y) as an parameter? Like this: def check_around(x, y, test='x'): if test == 'x': check = 'o' elif test == 'o': check = 'x' else: raise ValueError if board[x][y] == test: if board[x - 3][y - 3] == check or board[x - 2][y - 3] == check or board[x - 1][y - 3] == check or... However even better would be to get rid of the huge if statement and replace it with loops to generate the indices. Pythons range function can deal with negatives too: Try: >>> print list(range(-3,4)) to see if that gives you any ideas. Finally your mqain code doesn't appear to do anything very much... for x in range (0, 7): for y in range (0, 7): check_aroundx(x, y) This only checks the x cells. It would be better practice to have the function return a result that you can print externally. Maybe return a list of touching cells say? # sys.stdout.write ("%c" % (board[x][y])) print And these two lines don't do anything much. Just some thoughts. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work
Dear Python Community: I am new to Python,with only about a month's experience. I am writing Python 3.4.0 code that apparently isn't doing what it should be doing. Specifically, I am inputting or trying to input,a Sentry Variable to a While Loop. I want to test out the Main program" While" Loop before I add an inner "While" Loop. The program I have written,when run on the Python 3.4.0 Shell,does not stop for input of the "While" Sentry Variable,it just gives a program error: "Value of smv_grandVariable undefined". What am I doing wrong here? I'll try to post that part of the Code that is malfunctioning as well as Python Shell 3.4.0 Traceback Analysis. Please help if you can,this program is due on Thursday the 22nd of April,2014.#CS 110A Spring 2014 Sect 4988 Assignment 4 #MODIFIED GUESS MY NUMBER PROGRAM #Date April ,2014 #Programmer Stephen W. Mik #This program is modified after a version found #in the TextBook "Python Programming 3rd Edition" #by Michael Dawson Copyright 2010;ISBN-13: 978-1-4354-5500-9 #Or the Alternate ISBN-10: 1-4354-5500-2. #It is based on the material found on page 50,and pages 81-84 #of the Book, along with a downloaded basic program Structyre #"guess_my_number" ,found in the book companion's Website Files #"www.courseptr.com/downloads" Chapter 3. ##EXPLANATION OF GUESS MY NUMBER### #This program uses a random number generator(supplied by Python 3.4.0) to #to pick a changeable number between 1 and 60. The User is prompted to guess the number #by inputting a guess at the prompt. The user is then advised whether the correct number #has been guessed. If it has been guessed correctly,the user is congratulated. If the number #has not been guessed correctly,the attempt is noted and quantified. then the user is told whether # the guess number was too large (and then for the user to guess a lower number) or the number #guessed was too small (and in this case for the user to guess a higher number). The number of #attempted non-successful guesses is accumulated and totalled and then when finally the User guesses #the correct number ; the guess attempts are outputted along with Congatulations. The User is queried #if they want to play the game again,if not,the Program ends. ## MAIN PROGRAM SECTION #Use the random import module for a random number between 1 and 60 import random print("\tWelcome to the Guess My Number Game! ") print("\n The Mysterious Number to guess is between 1 and 60. ") print("Try to guess the Mystery Number in as few tries as you can.\n") #Enter Main Query Loop print("Do you want to play the game?\n") print("Enter a 1 to play or 0 to exit:") input(smv_grandVariable) while (smv_grandVariable == 1 and smv_grandVariable != 0): #Enter the play games main loop area #Take Control of the main While Loop print("Do you want to run the Guess My Number game again? \n") print("IF so, 1 to play again or 0 to not play \n") input(smv_grandVariable) #end of main control Loop #print out ending comments print("Program is Ended") input("\n\nPress the enter key to exit. ") #End of Program Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> RESTART >>> Welcome to the Guess My Number Game! The Mysterious Number to guess is between 1 and 60. Try to guess the Mystery Number in as few tries as you can. Do you want to play the game? Enter a 1 to play or 0 to exit: Traceback (most recent call last): File "E:\My Code\assignment4.py", line 37, in input(smv_grandVariable) NameError: name 'smv_grandVariable' is not defined >>> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recognising Errors
Thanks for the advice and tips. I wasn't taking anything as an insult; I just have a problem with one of the staffs attitude responses. He needs sleep. Alan Gauld has been very helpful- a special thanks to you. Kind regards Saba On 21 Apr 2014, at 18:40, "Danny Yoo" wrote: >> If for some reason you can't read this code properly as outlook has >> formatted it to look messy/cluttered; you do not have to respond. > > You are missing the point of people point this out. Look at what the > email archive thinks of your previous messages: > > https://mail.python.org/pipermail/tutor/2014-April/100904.html > https://mail.python.org/pipermail/tutor/2014-April/100940.html > https://mail.python.org/pipermail/tutor/2014-April/100985.html > https://mail.python.org/pipermail/tutor/2014-April/100997.html > > > When folks are saying that we can't read your programs, we're not > trying to insult you. Rather, we're making a very technical > observation: your email client is interfering with the indentation of > your program. In Python, indentation and formatting _changes_ the > meaning of your program, so we really can not tell what your program > _means_. Under those conditions, we can't help very effectively. > > If you really can not fix your email client, then post your programs > on a "pastebin" which should preserve formatting and meaning. For > example, http://gist.github.com. But please do not treat the advice > you're been getting as personal insults. They are not intended to be > such. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Error
im trying to make a board, and is detecting the pieces. Here is my code: http://pastebin.com/L3tQLV2g And here is the error: http://pastebin.com/4FiJmywL Do you knwo hwo to fix it? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Groups of mutually exclusive options
On 21/04/14 22:05, rail shafigulin wrote: Does anybody know if there is a way to specify groups of mutually exclusive options using argparse module? Sorry, I didn't follow your example. Can you explain what you mean in English? What would be the outcome if you succeeded? What could the user do and not do? What I need is a way to specify groups of mutually exclusive options. In other words parser = argparse.ArgumentParser(prog='PROG') group1 = parser.add_argument_group() group2 = parser.add_argument_group() group1.add_argument('--foo', action='store_true') group1.add_argument('--bar1', action='store_false') group2.add_argument('--foo2', action = 'store_true') group2.add_argument('--bar2', action = 'store_false') mutually_exclusive_group = parser.add_mutually_exclusive_argument_group() mutually_exclusive_group.add_group(group1) mutually_exclusive_group.add_group(group2) parser.parse_args(['--foo1', '--bar1', '--bar2']) usage: PROG [-h] [--foo1, --bar1] | [--foo2, --bar2] PROG: error: argument --foo1 or bar1 not allowed with argument --foo2 or bar2 -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Groups of mutually exclusive options
Does anybody know if there is a way to specify groups of mutually exclusive options using argparse module? Currently argpase allows to specify mutually exclusive options in the following way (taken from https://docs.python.org/release/3.4.0/library/argparse.html#argparse.ArgumentParser.add_mutually_exclusive_group ) >>> parser = argparse.ArgumentParser(prog='PROG')>>> group = >>> parser.add_mutually_exclusive_group()>>> group.add_argument('--foo', >>> action='store_true')>>> group.add_argument('--bar', >>> action='store_false')>>> parser.parse_args(['--foo'])Namespace(bar=True, >>> foo=True)>>> parser.parse_args(['--bar'])Namespace(bar=False, foo=False)>>> >>> parser.parse_args(['--foo', '--bar'])usage: PROG [-h] [--foo | --bar]PROG: >>> error: argument --bar: not allowed with argument --foo What I need is a way to specify groups of mutually exclusive options. In other words >>> parser = argparse.ArgumentParser(prog='PROG')>>> group1 = >>> parser.add_argument_group()>>> group2 = parser.add_argument_group()>>> >>> group1.add_argument('--foo', action='store_true')>>> >>> group1.add_argument('--bar1', action='store_false')>>> >>> group2.add_argument('--foo2', action = 'store_true') >>> group2.add_argument('--bar2', action = 'store_false') >>> mutually_exclusive_group = parser.add_mutually_exclusive_argument_group() >>> mutually_exclusive_group.add_group(group1) >>> mutually_exclusive_group.add_group(group2)>>> parser.parse_args(['--foo1', >>> '--bar1', '--bar2'])usage: PROG [-h] [--foo1, --bar1] | [--foo2, >>> --bar2]PROG: error: argument --foo1 or bar1 not allowed with argument >>> --foo2 or bar2 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which book to read next??
> From: Alex Kleider >To: tutor@python.org >Sent: Monday, April 21, 2014 9:37 PM >The book I currently keep close at hand as a reference is >Programming in Python 3 by Mark Summerfield (2nd Ed) but I would not >recommend it as an introductory book about Python. That's an awesome book. It does contain some very/too advanced chapters, but it is absolutely worth buying. Here is a sample chapter of it, about regexes: http://www.informit.com/content/images/9780137129294/samplepages/0137129297_Sample.pdf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which book to read next??
On 2014-04-21 07:13, lee wrote: Hi, I have read the book 'a byte of python' and now I want to read another book. But I just get confused about which one to read next. There is a book list below: 1, pro python 2, python algorithms 3, python cookbook 4, the python standard library by examples which one is suitable for me?? Or I need to start a project with pygame or flask? Thanks for your help! If you aren't already a programmer, I would strongly recommend Downey's book: http://www.greenteapress.com/thinkpython/thinkpython.html It covers Python v2 but I found it fairly easy to transition to Python v3. The book I currently keep close at hand as a reference is Programming in Python 3 by Mark Summerfield (2nd Ed) but I would not recommend it as an introductory book about Python. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recognising Errors
> If for some reason you can't read this code properly as outlook has > formatted it to look messy/cluttered; you do not have to respond. You are missing the point of people point this out. Look at what the email archive thinks of your previous messages: https://mail.python.org/pipermail/tutor/2014-April/100904.html https://mail.python.org/pipermail/tutor/2014-April/100940.html https://mail.python.org/pipermail/tutor/2014-April/100985.html https://mail.python.org/pipermail/tutor/2014-April/100997.html When folks are saying that we can't read your programs, we're not trying to insult you. Rather, we're making a very technical observation: your email client is interfering with the indentation of your program. In Python, indentation and formatting _changes_ the meaning of your program, so we really can not tell what your program _means_. Under those conditions, we can't help very effectively. If you really can not fix your email client, then post your programs on a "pastebin" which should preserve formatting and meaning. For example, http://gist.github.com. But please do not treat the advice you're been getting as personal insults. They are not intended to be such. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which book to read next??
On Mon, Apr 21, 2014 at 11:41 AM, Alan Gauld wrote: > On 21/04/14 15:13, lee wrote: > >> Hi, I have read the book 'a byte of python' and now I want to read >> another book. But I just get confused about which one to read next. >> There is a book list below: >> 1, pro python >> 2, python algorithms >> 3, python cookbook >> 4, the python standard library by examples >> which one is suitable for me?? >> > > We would need to know a lot more about you. > What is your skill level in programming (as opposed to python)? > What are your areas of interest? > What is your preferred teaching style? In depth background > detail or surface level but hands-on style? > > Book choice is always a very personal thing. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > -- > https://mail.python.org/mailman/listinfo/python-list > Don't forget to look at the python.org site: https://wiki.python.org/moin/BeginnersGuide -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] which book to read next??
Hi, I have read the book 'a byte of python' and now I want to read another book. But I just get confused about which one to read next. There is a book list below: 1, pro python 2, python algorithms 3, python cookbook 4, the python standard library by examples which one is suitable for me?? Or I need to start a project with pygame or flask? Thanks for your help! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recognising Errors
On 21/04/2014 07:20, Dave Angel wrote: Saba Usmani Wrote in message: If for some reason you can't read this code properly as outlook has formatted it to look messy/cluttered; you do not have to respond It'd save trouble if you continued in the same thread you started, instead of repeatedly starting a new one. And you clearly have read at least one of the other responses, as you have the nerve to dis those who give you good advice about posting in html. It should be a simple menu choice, unless Outlook is more busted than I remember. Depends on whether you're talking about Outlook the Office application or Outlook the new name for Hotmail. I think :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor