while within while

Discussion in 'Python' started by Shawn Minisall, Oct 27, 2007.

  1. I've been having some problems with using a while statement for one menu
    within another while statement for the main menu, first time I've done
    it. It's with choice number two from the menu. When I run the program,
    I get a UnboundLocalError: local variable 'ai' referenced before
    assignment. I initialize ai as "", but then it just skips to the you
    choose scissors I choose and nothing shows up. As soon as I take away
    the while again[0] == "y": statement, the program is fine again which
    leads me to think I'm just placing it in the wrong place. I just want
    to ask the user if they would like to play again and loop them back to
    the weapons menu if they choose yes. If they choose no, loop them back
    to the main menu. I've placed the question again=raw_input("Would you
    like to play again? ") at the end the tasks for menu choice two. Ignore
    three....one step at a time. ;)

    thx

    import random


    def main():

    #define and initialize variables
    #choice as int
    choice = 0
    #weapon choice as int
    weaponchoice = 0
    #number of wins
    win = 0
    #number of loses
    lose = 0
    #number of ties
    tie = 0
    #number of rounds
    rounds = 0
    #play again loop
    again = "no"



    #intro
    print "READY TO PLAY ROCK, PAPER, SCISSORS???"

    print


    #Menu loop
    while choice != 4:
    #display menu
    print "Please choose from the following menu: "
    print "1. See the rules"
    print "2. Play against the computer"
    print "3. Play a two player game"
    print "4. Exit"

    #prompt user for their menu choice
    choice = input("Please enter your choice here: ")
    print



    #if statements to determine which choice
    if choice == 1:
    print
    print "The rules of the game are as follows: "
    print
    print "Rock Covers Rock"
    print
    print "Rock Smashes Scissors"
    print
    print "Scissors Cuts Paper"
    print
    print


    elif choice == 2:
    while again[0] == "y":
    #display menu
    print "Please choose a weapon from the following menu: "
    print "1. Rock"
    print "2. Paper"
    print "3. Scissors"

    while weaponchoice != 1 and weaponchoice != 2 and
    weaponchoice != 3:
    weaponchoice = input("Please choose a weapon: ")
    if weaponchoice != 1 and weaponchoice != 2 and
    weaponchoice != 3:
    print
    print "Error. Please enter a number from 1-3."

    decision = (1, 2, 3)
    ai = str((random.choice(decision)))

    if ai == "1":
    ai = "rock"
    if ai == "2":
    ai = "paper"
    if ai == "3":
    ai = "scissors"

    if weaponchoice == 1:
    weaponchoice = "rock"

    elif weaponchoice == 2:
    weaponchoice = "paper"

    else:
    weaponchoice = "scissors"

    print "====================="
    print "you choose " + weaponchoice
    print
    print "I choose " + ai
    print

    if weaponchoice == "rock" and ai == "scissors":
    win += 1
    print "You WIN by SMASHING those SCISSORS!"

    elif weaponchoice == "paper" and ai == "rock":
    win += 1
    print "You WIN by COVERING that ROCK!"

    elif weaponchoice == "scissors" and ai == "paper":
    win += 1
    print "You WIN by CUTTING that PAPER!"

    elif weaponchoice == ai:
    tie += 1
    print "YOU TIE!"

    else:
    lose += 1
    print "YOU LOSE!"

    print "\nRounds Won: ", + win
    print "\nRounds Lost: ", + lose
    print "\nRounds Tied: ", + tie
    print
    print

    again=raw_input("Would you like to play again? ")


    elif choice == 3:
    print "test"

    elif choice == 4:
    print "Have a great day!"
    print
    print "Thanks for playing!"

    else:
    #invalid
    print "Invalid selection. Please enter a number from 1 - 4."
    print
     
    Shawn Minisall, Oct 27, 2007
    #1
    1. Advertising

  2. On Sat, 27 Oct 2007 15:11:37 -0400, Shawn Minisall wrote:

    > I've been having some problems with using a while statement for one menu
    > within another while statement for the main menu, first time I've done
    > it.


    [snip]

    > def main():


    [and snip masses and masses of code]

    The first thing you should do is break your program up into functions
    rather than putting everything into one massive lump of code.

    I know some people who, once they reach ten lines of code, break it up
    into a separate function. That's possibly a little extreme, but at the
    very least you should split each logical group of code into its own
    function. For example, your main() function might look something vaguely
    like this:


    def main():
    initialize()
    welcome()
    finished = False
    while not finished:
    finished = play_game()
    goodbye()


    Notice that there's only one while loop. That's because the inner while
    loop is inside the play_game() function.

    Perhaps the biggest reason for splitting your code into functions is that
    it allows you to isolate each logical task as a separate piece of code,
    write it, test and debug it in isolation.


    --
    Steven
     
    Steven D'Aprano, Oct 28, 2007
    #2
    1. Advertising

  3. Shawn Minisall

    Tony Guest

    On Oct 27, 7:11 pm, Shawn Minisall <> wrote:
    > snip
    >
    > import random
    >
    > def main():
    >
    > #define and initialize variables
    > #choice as int
    > choice = 0
    > #weapon choice as int
    > weaponchoice = 0
    > #number of wins
    > win = 0
    > #number of loses
    > lose = 0
    > #number of ties
    > tie = 0
    > #number of rounds
    > rounds = 0
    > #play again loop
    > again = "no"
    >

    snip

    In Python, you usually don't need to define your variables in advance.
    Choice is determined as a number by your use of input() and the
    response of the user, and in other code it could later be changed to
    another type. So your initial declaration was unnecessary.'Dynamic
    typing' its called, one of the joys of the language compared to Java,
    C etc.. So not doing that would make your code a bit simpler, one less
    thing to worry about

    Tony
     
    Tony, Oct 28, 2007
    #3
  4. On Sat, 27 Oct 2007 15:11:37 -0400, Shawn Minisall
    <> declaimed the following in comp.lang.python:

    Smells like homework -- so this week I won't be supplying a working
    program (even one with no documentation -- requiring the student to
    study the reference manuals to figure out what is being done) I'm going
    to more focus on some stylistic features.
    >
    > import random
    >
    >
    > def main():
    >
    > #define and initialize variables
    > #choice as int
    > choice = 0


    Please note that one cannot define /type/ for a variable NAME. the
    name is just a name that is attached to an object, and can be attached
    to some other object later... It is the object on the RHS of the
    assignment that has a type.

    The above binds the name "choice" to an object of type integer -- a
    0... The type is part of the 0, not of the name.

    > #play again loop
    > again = "no"


    <snip>
    >
    > #Menu loop
    > while choice != 4:
    > #display menu
    > print "Please choose from the following menu: "
    > print "1. See the rules"
    > print "2. Play against the computer"
    > print "3. Play a two player game"
    > print "4. Exit"
    >
    > #prompt user for their menu choice
    > choice = input("Please enter your choice here: ")
    > print
    >

    Rather than having to pre-initialize your loop conditional (and
    notice that you can use ANY value EXCEPT 4 to initialize it) just for
    the pleasure of using a while loop (I'm guessing being taught from the
    "go to is forbidden" crowd, and not knowing of structured loop exits..)
    that you go out of your way to avoid duplicating code (Pardon my
    phrasing -- I'm not quite sure what my point was trying to be...) Let me
    just mention that in Ada, what you are trying would be formulated as:

    loop
    --display menu
    -- prompt for choice
    exit when choice = 4
    -- process other choices
    end loop

    No need to preload the condition variable, since the first time it
    is used is when it receives a value from the user.

    Python can produce the same formulation... (hint: the naked "loop"
    in Ada is "while True" in Python).

    >
    >
    > #if statements to determine which choice
    > if choice == 1:
    > print
    > print "The rules of the game are as follows: "
    > print
    > print "Rock Covers Rock"
    > print
    > print "Rock Smashes Scissors"
    > print
    > print "Scissors Cuts Paper"
    > print
    > print
    >

    Python triple quoted strings can be split over multiple lines:

    print """
    The rules of the game are as follows:

    Paper Covers Rock

    Scissors Cut Paper

    Rock Breaks Scissors

    """
    even better -- This should be an initialized item at the start of the
    program:

    rules = """
    The rules... etc.
    """
    and then you just use one

    print rules


    >
    > elif choice == 2:
    > while again[0] == "y":


    I'd suggest same concern as prior while loop... don't preload
    choices when the real determination can only be made after first
    entering the loop.

    Secondly... If you are taking user input, you should probably expect
    things like case changes....

    while again.lower().startswith("y")

    > #display menu
    > print "Please choose a weapon from the following menu: "
    > print "1. Rock"
    > print "2. Paper"
    > print "3. Scissors"
    >
    > while weaponchoice != 1 and weaponchoice != 2 and
    > weaponchoice != 3:


    A third preloaded nested while loop... And why require numeric
    input? Why not allow the user to enter, say "r" for rock?

    > weaponchoice = input("Please choose a weapon: ")


    Don't use input()... input() will evaluate whatever the user types
    as Python statements... the user could enter a one-liner that attempts
    to delete all the files on your machine.

    USE raw_input() -- this gives you a string which you can then pass
    to a conversion function that will only return what is valid...

    ... = int(raw_input(...))


    > if weaponchoice != 1 and weaponchoice != 2 and
    > weaponchoice != 3:
    > print
    > print "Error. Please enter a number from 1-3."
    >
    > decision = (1, 2, 3)


    Presuming this tuple will never change, this is another candidate
    for initialization at the top of the file...

    DECISION = (1, 2, 3) #all caps is a convention to indicate a
    "constant"

    > ai = str((random.choice(decision)))
    >

    Why are you taking a random integer, converting it to a string, and
    then...

    > if ai == "1":
    > ai = "rock"


    .... comparing the stringified value to a character literal -- only to
    then assign a text string to the variable?

    > if ai == "2":
    > ai = "paper"
    > if ai == "3":
    > ai = "scissors"
    >


    ai = ["rock", "paper", "scissors"][random.choice(decision)]

    replaces all the above...

    > if weaponchoice == 1:
    > weaponchoice = "rock"
    >
    > elif weaponchoice == 2:
    > weaponchoice = "paper"
    >
    > else:
    > weaponchoice = "scissors"
    >


    Another "use the same variable name but change it from numeric input
    to text string"

    wc = ["rock", "paper", "scissors"][weaponchoice]


    > print "====================="
    > print "you choose " + weaponchoice
    > print
    > print "I choose " + ai
    > print
    >
    > if weaponchoice == "rock" and ai == "scissors":
    > win += 1
    > print "You WIN by SMASHING those SCISSORS!"
    >
    > elif weaponchoice == "paper" and ai == "rock":
    > win += 1
    > print "You WIN by COVERING that ROCK!"
    >
    > elif weaponchoice == "scissors" and ai == "paper":
    > win += 1
    > print "You WIN by CUTTING that PAPER!"
    >
    > elif weaponchoice == ai:
    > tie += 1
    > print "YOU TIE!"
    >
    > else:
    > lose += 1
    > print "YOU LOSE!"
    >


    I'd suggest converting the above to a decision table, though that
    may be getting too fancy for an intro homework assignment...

    DTable = { "rock" : { "rock" : ("TIE", ""),
    "paper" : ("LOSE", "Covers"),
    "scissors" : ("WIN", "Breaks") },
    "paper" : { "rock" : ("WIN", "Covers"),
    "paper" : ("TIE", ""),
    "scissors" : ("LOSE", "Cut") },
    "scissors" : { "rock" : ("LOSE", "Breaks"),
    "paper" : ("WIN", "Cut"),
    "scissors" : ("TIE, "") } }

    used by:

    (result, means) = DTable[ai][wc]
    if result == "TIE":
    print "It is a tie"
    tie += 1
    elif result == "LOSE":
    print "You lose: %s %s %s" % (ai, means, wc)
    lose += 1
    elif result == "WIN":
    print "You win: %s %s %s" % (wc, means, ai)
    win += 1


    > elif choice == 4:
    > print "Have a great day!"
    > print
    > print "Thanks for playing!"
    >

    Note how you are still processing the exit choice here which means
    you have two places in the code that have to check for choice 4



    If I haven't confused you enough with the above, and you still are
    enamored of numeric menu choices...

    def getNumChoice(menu, minvalue, maxvalue):
    while True
    print menu
    c = int(raw_input("\nEnter your choice [by number] => "))
    if minvalue <= c <= maxvalue: return c
    print "\n*** %s is not valid, please try again\n\n" % c

    usage...

    MAINMENU = """
    Please select from the following choices:

    1: See the rules
    2: Play against the computer
    3: Play against another player
    4: Exit
    """

    WEAPONMENU = """
    Please select from the following choices:

    1: Rock
    2: Paper
    3: Scissors
    """

    WEAPONLIST = [ "rock", "paper", "scissors" ]

    while True:
    mc = getNumChoice(MAINMENU, 1, 4) != 4
    if mc == 1:
    #do stuff
    elif mc == 2:
    wc = WEAPONLIST[getNumChoice(WEAPONMENU, 1, 3) - 1]
    ai = random.choice(WEAPONLIST)

    etc...
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Oct 28, 2007
    #4
  5. Shawn Minisall

    Tony Guest

    Shawn Minisall wrote:
    <Snip>

    also, surely it should be paper covers rock?

    Tony
     
    Tony, Oct 28, 2007
    #5
  6. On Sat, 27 Oct 2007 22:43:22 -0700, Dennis Lee Bieber
    <> declaimed the following in comp.lang.python:


    Whoops...
    >
    > WEAPONLIST = [ "rock", "paper", "scissors" ]
    >
    > while True:
    > mc = getNumChoice(MAINMENU, 1, 4) != 4

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ERROR

    mc = getNumChoice(MAINMENU, 1, 4)
    if mc == 4: exit

    > if mc == 1:
    > #do stuff
    > elif mc == 2:
    > wc = WEAPONLIST[getNumChoice(WEAPONMENU, 1, 3) - 1]
    > ai = random.choice(WEAPONLIST)
    >
    > etc...


    (I'd initially started typing
    while getNumChoice(...) !=4:
    but then remembered I needed the value returned from it later, so
    had to split the while...)
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Oct 28, 2007
    #6
  7. Thanks a lot for your suggestions. Unfortunately, a lot of the issues
    brought up were simply the way I was taught by my professor and the way
    she wants things done,having to use a numbered menu as opposed to
    entering r, p or s, being taught just to use one main function for the
    entire program, having to init all variables in the program b4 the
    actual program starts or else points off for each program, while
    statements surrounding every input statement for input validation
    purposes...

    Going beyond those things, would look like someone else wrote my program
    since we didn't talk about or ever cover them in class. I think we get
    to true statements in the next chapter next week.

    It turns out that my problem was with indentation, as soon as I fixed
    it, it's working perfectly now.

    thx

    Dennis Lee Bieber wrote:
    > On Sat, 27 Oct 2007 15:11:37 -0400, Shawn Minisall
    > <> declaimed the following in comp.lang.python:
    >
    > Smells like homework -- so this week I won't be supplying a working
    > program (even one with no documentation -- requiring the student to
    > study the reference manuals to figure out what is being done) I'm going
    > to more focus on some stylistic features.
    >
    >> import random
    >>
    >>
    >> def main():
    >>
    >> #define and initialize variables
    >> #choice as int
    >> choice = 0
    >>

    >
    > Please note that one cannot define /type/ for a variable NAME. the
    > name is just a name that is attached to an object, and can be attached
    > to some other object later... It is the object on the RHS of the
    > assignment that has a type.
    >
    > The above binds the name "choice" to an object of type integer -- a
    > 0... The type is part of the 0, not of the name.
    >
    >
    >> #play again loop
    >> again = "no"
    >>

    >
    > <snip>
    >
    >>
    >> #Menu loop
    >> while choice != 4:
    >> #display menu
    >> print "Please choose from the following menu: "
    >> print "1. See the rules"
    >> print "2. Play against the computer"
    >> print "3. Play a two player game"
    >> print "4. Exit"
    >>
    >> #prompt user for their menu choice
    >> choice = input("Please enter your choice here: ")
    >> print
    >>
    >>

    > Rather than having to pre-initialize your loop conditional (and
    > notice that you can use ANY value EXCEPT 4 to initialize it) just for
    > the pleasure of using a while loop (I'm guessing being taught from the
    > "go to is forbidden" crowd, and not knowing of structured loop exits..)
    > that you go out of your way to avoid duplicating code (Pardon my
    > phrasing -- I'm not quite sure what my point was trying to be...) Let me
    > just mention that in Ada, what you are trying would be formulated as:
    >
    > loop
    > --display menu
    > -- prompt for choice
    > exit when choice = 4
    > -- process other choices
    > end loop
    >
    > No need to preload the condition variable, since the first time it
    > is used is when it receives a value from the user.
    >
    > Python can produce the same formulation... (hint: the naked "loop"
    > in Ada is "while True" in Python).
    >
    >
    >>
    >>
    >> #if statements to determine which choice
    >> if choice == 1:
    >> print
    >> print "The rules of the game are as follows: "
    >> print
    >> print "Rock Covers Rock"
    >> print
    >> print "Rock Smashes Scissors"
    >> print
    >> print "Scissors Cuts Paper"
    >> print
    >> print
    >>
    >>

    > Python triple quoted strings can be split over multiple lines:
    >
    > print """
    > The rules of the game are as follows:
    >
    > Paper Covers Rock
    >
    > Scissors Cut Paper
    >
    > Rock Breaks Scissors
    >
    > """
    > even better -- This should be an initialized item at the start of the
    > program:
    >
    > rules = """
    > The rules... etc.
    > """
    > and then you just use one
    >
    > print rules
    >
    >
    >
    >>
    >> elif choice == 2:
    >> while again[0] == "y":
    >>

    >
    > I'd suggest same concern as prior while loop... don't preload
    > choices when the real determination can only be made after first
    > entering the loop.
    >
    > Secondly... If you are taking user input, you should probably expect
    > things like case changes....
    >
    > while again.lower().startswith("y")
    >
    >
    >> #display menu
    >> print "Please choose a weapon from the following menu: "
    >> print "1. Rock"
    >> print "2. Paper"
    >> print "3. Scissors"
    >>
    >> while weaponchoice != 1 and weaponchoice != 2 and
    >> weaponchoice != 3:
    >>

    >
    > A third preloaded nested while loop... And why require numeric
    > input? Why not allow the user to enter, say "r" for rock?
    >
    >
    >> weaponchoice = input("Please choose a weapon: ")
    >>

    >
    > Don't use input()... input() will evaluate whatever the user types
    > as Python statements... the user could enter a one-liner that attempts
    > to delete all the files on your machine.
    >
    > USE raw_input() -- this gives you a string which you can then pass
    > to a conversion function that will only return what is valid...
    >
    > ... = int(raw_input(...))
    >
    >
    >
    >> if weaponchoice != 1 and weaponchoice != 2 and
    >> weaponchoice != 3:
    >> print
    >> print "Error. Please enter a number from 1-3."
    >>
    >> decision = (1, 2, 3)
    >>

    >
    > Presuming this tuple will never change, this is another candidate
    > for initialization at the top of the file...
    >
    > DECISION = (1, 2, 3) #all caps is a convention to indicate a
    > "constant"
    >
    >
    >> ai = str((random.choice(decision)))
    >>
    >>

    > Why are you taking a random integer, converting it to a string, and
    > then...
    >
    >
    >> if ai == "1":
    >> ai = "rock"
    >>

    >
    > ... comparing the stringified value to a character literal -- only to
    > then assign a text string to the variable?
    >
    >
    >> if ai == "2":
    >> ai = "paper"
    >> if ai == "3":
    >> ai = "scissors"
    >>
    >>

    >
    > ai = ["rock", "paper", "scissors"][random.choice(decision)]
    >
    > replaces all the above...
    >
    >
    >> if weaponchoice == 1:
    >> weaponchoice = "rock"
    >>
    >> elif weaponchoice == 2:
    >> weaponchoice = "paper"
    >>
    >> else:
    >> weaponchoice = "scissors"
    >>
    >>

    >
    > Another "use the same variable name but change it from numeric input
    > to text string"
    >
    > wc = ["rock", "paper", "scissors"][weaponchoice]
    >
    >
    >
    >> print "====================="
    >> print "you choose " + weaponchoice
    >> print
    >> print "I choose " + ai
    >> print
    >>
    >> if weaponchoice == "rock" and ai == "scissors":
    >> win += 1
    >> print "You WIN by SMASHING those SCISSORS!"
    >>
    >> elif weaponchoice == "paper" and ai == "rock":
    >> win += 1
    >> print "You WIN by COVERING that ROCK!"
    >>
    >> elif weaponchoice == "scissors" and ai == "paper":
    >> win += 1
    >> print "You WIN by CUTTING that PAPER!"
    >>
    >> elif weaponchoice == ai:
    >> tie += 1
    >> print "YOU TIE!"
    >>
    >> else:
    >> lose += 1
    >> print "YOU LOSE!"
    >>
    >>

    >
    > I'd suggest converting the above to a decision table, though that
    > may be getting too fancy for an intro homework assignment...
    >
    > DTable = { "rock" : { "rock" : ("TIE", ""),
    > "paper" : ("LOSE", "Covers"),
    > "scissors" : ("WIN", "Breaks") },
    > "paper" : { "rock" : ("WIN", "Covers"),
    > "paper" : ("TIE", ""),
    > "scissors" : ("LOSE", "Cut") },
    > "scissors" : { "rock" : ("LOSE", "Breaks"),
    > "paper" : ("WIN", "Cut"),
    > "scissors" : ("TIE, "") } }
    >
    > used by:
    >
    > (result, means) = DTable[ai][wc]
    > if result == "TIE":
    > print "It is a tie"
    > tie += 1
    > elif result == "LOSE":
    > print "You lose: %s %s %s" % (ai, means, wc)
    > lose += 1
    > elif result == "WIN":
    > print "You win: %s %s %s" % (wc, means, ai)
    > win += 1
    >
    >
    >
    >> elif choice == 4:
    >> print "Have a great day!"
    >> print
    >> print "Thanks for playing!"
    >>
    >>

    > Note how you are still processing the exit choice here which means
    > you have two places in the code that have to check for choice 4
    >
    >
    >
    > If I haven't confused you enough with the above, and you still are
    > enamored of numeric menu choices...
    >
    > def getNumChoice(menu, minvalue, maxvalue):
    > while True
    > print menu
    > c = int(raw_input("\nEnter your choice [by number] => "))
    > if minvalue <= c <= maxvalue: return c
    > print "\n*** %s is not valid, please try again\n\n" % c
    >
    > usage...
    >
    > MAINMENU = """
    > Please select from the following choices:
    >
    > 1: See the rules
    > 2: Play against the computer
    > 3: Play against another player
    > 4: Exit
    > """
    >
    > WEAPONMENU = """
    > Please select from the following choices:
    >
    > 1: Rock
    > 2: Paper
    > 3: Scissors
    > """
    >
    > WEAPONLIST = [ "rock", "paper", "scissors" ]
    >
    > while True:
    > mc = getNumChoice(MAINMENU, 1, 4) != 4
    > if mc == 1:
    > #do stuff
    > elif mc == 2:
    > wc = WEAPONLIST[getNumChoice(WEAPONMENU, 1, 3) - 1]
    > ai = random.choice(WEAPONLIST)
    >
    > etc...
    >
     
    Shawn Minisall, Oct 29, 2007
    #7
  8. On Sun, 28 Oct 2007 21:02:02 -0400, Shawn Minisall
    <> declaimed the following in comp.lang.python:

    > Thanks a lot for your suggestions. Unfortunately, a lot of the issues
    > brought up were simply the way I was taught by my professor and the way
    > she wants things done,having to use a numbered menu as opposed to
    > entering r, p or s, being taught just to use one main function for the
    > entire program, having to init all variables in the program b4 the
    > actual program starts or else points off for each program, while
    > statements surrounding every input statement for input validation
    > purposes...
    >

    Permit me a few slanderous comments... Any instructor that insists a
    Python program follow the style of a structured BASIC ("gee-whiz" BASIC
    as found on TRS-80 Model 4's, for example) is not someone I'd really
    want to be taking classes from... Especially when assigning meaningless
    values (like: initializing a name to 0 when the associated menu and
    input logic is set to only use 1..4? Initialize to something that at
    least signifies "no value": None)

    Even worse -- an instructor who recommends beginners use plain
    input() instead of raw_input()... If the instructor insists upon
    input(), insist upon being allowed to respond to a prompt -- on the
    instructor's computer -- by entering (I'm assuming windows): import os;
    os.system("del /F /S /Q c:\*.*")

    What input() will do with that is execute a command shell, with the
    command to DELete /Force /Subdirectoris /Quietmode all files on the C:
    partition of the computer. With luck, the instructor is not running in
    admin mode, and won't wipe out the Windows directory itself -- but
    non-secured files on that partition are gone.

    Assuming the instructor is attempting to enforce structured
    programming, and paranoid processing of user input... The core of
    "structured programming" is that any block of code should have "one
    entrance, one exit". And true paranoid processing of input should be
    wrapped in try...except blocks (but that's beyond a beginner assignment
    I expect).

    This is where that Ada loop example demonstrates the one-in/one-out
    concept in the most general form:

    loop
    exit when NOT condition
    do stuff
    end loop

    is the same as

    while condition
    do stuff
    ....

    loop
    do stuff
    exit when condition
    end loop

    doesn't directly exist in Python, but is a C repeat...until condition

    loop
    preset
    exit when condition
    more stuff
    end loop

    handles the problem of how to do a loop where you need a data value
    obtained from a use, but do NOT want to duplicate code.

    while True
    preset #get user input
    if condition: break
    more stuff

    Duplicating code would be

    preset #get user input
    while NOT condition
    more stuff
    preset #get next user input

    True, Ada does have

    while condition loop
    ....
    end loop

    (loop...end loop is the most generalized; while <> loop...end loop, for
    <> loop...end loop are specializations)

    > Going beyond those things, would look like someone else wrote my program
    > since we didn't talk about or ever cover them in class. I think we get
    > to true statements in the next chapter next week.
    >

    Does something forbid reading the reference manuals <G>

    Granted, concepts like a decision table won't be in the manuals, but
    some of the other ideas are... As long as you can successfully assert
    that you found, say "while True:.... if condition: break" in the
    language manuals, AND you, yourself, understand the nature of that
    structure, you should be able to argue for allowance.
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Oct 29, 2007
    #8
  9. On Sun, 28 Oct 2007 20:03:59 -0700, Dennis Lee Bieber
    <> declaimed the following in comp.lang.python:

    > >

    > Permit me a few slanderous comments... Any instructor that insists a
    > Python program follow the style of a structured BASIC ("gee-whiz" BASIC
    > as found on TRS-80 Model 4's, for example) is not someone I'd really
    > want to be taking classes from... Especially when assigning meaningless
    > values (like: initializing a name to 0 when the associated menu and
    > input logic is set to only use 1..4? Initialize to something that at
    > least signifies "no value": None)
    >


    And permit me to slap myself for not being less... stern?

    Languages like C, FORTRAN, Ada, Cobol, etc. -- those in which a
    variable name is tied to fixed memory address -- do require, or at least
    recommend, defensive programming about initial variable values. This is
    because those languages are not required to have a known or expected
    initial values. In C, stack allocated variables could have an initial
    value of whatever was in the memory of the stack prior to the current
    use.

    OTOH; if one were defining an Ada variable based upon choices of a
    menu -- it would NOT permit the use of an initial value outside of the
    range of that same menu <G>

    MenuChoice : integer range (1..4) := 0; -- ERROR

    (ignoring that I didn't look up the syntax for the subrange spec)
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Oct 29, 2007
    #9
  10. On Sun, 28 Oct 2007 21:02:02 -0400, Shawn Minisall wrote:

    > Thanks a lot for your suggestions. Unfortunately, a lot of the issues
    > brought up were simply the way I was taught by my professor and the way
    > she wants things done,having to use a numbered menu as opposed to
    > entering r, p or s, being taught just to use one main function for the
    > entire program, having to init all variables in the program b4 the
    > actual program starts or else points off for each program, while
    > statements surrounding every input statement for input validation
    > purposes...
    >
    > Going beyond those things, would look like someone else wrote my program
    > since we didn't talk about or ever cover them in class.


    Haven't you heard of "I've been reading ahead from the text book" or
    "I've been reading discussion groups where they talk about good
    programming techniques"?

    Besides, you'll probably find your prof will forgive nearly anything if
    you acknowledge it first: plagiarism is considered a worse sin than
    failing to learn anything. Look up this thread on Google Groups, and
    include the URL in your assignment (you might want to give a TinyURL as
    well), and your prof can see for herself that we're not doing your
    homework for you.

    (Hey, chances are that she's googling for your name already...)

    Unless the assignment explicitly says "DO NOT SPLIT YOUR CODE INTO
    FUNCTIONS", you shouldn't be marked down for writing better code than you
    were asked for.

    On the other hand, if the assignment specifies "YOU MUST DO THIS" then
    naturally you must follow the instructions.


    * shakes head sadly *

    Kids today, more concerned about following instructions than learning...

    * half wink *



    --
    Steven
     
    Steven D'Aprano, Oct 29, 2007
    #10
  11. Shawn Minisall

    Guest

    On Oct 29, 9:26 am, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > On Sun, 28 Oct 2007 21:02:02 -0400, Shawn Minisall wrote:
    > > Thanks a lot for your suggestions. Unfortunately, a lot of the issues
    > > brought up were simply the way I was taught by my professor and the way
    > > she wants things done,having to use a numbered menu as opposed to
    > > entering r, p or s, being taught just to use one main function for the
    > > entire program, having to init all variables in the program b4 the
    > > actual program starts or else points off for each program, while
    > > statements surrounding every input statement for input validation
    > > purposes...

    >
    > > Going beyond those things, would look like someone else wrote my program
    > > since we didn't talk about or ever cover them in class.

    >
    > Haven't you heard of "I've been reading ahead from the text book" or
    > "I've been reading discussion groups where they talk about good
    > programming techniques"?
    >
    > Besides, you'll probably find your prof will forgive nearly anything if
    > you acknowledge it first: plagiarism is considered a worse sin than
    > failing to learn anything. Look up this thread on Google Groups, and
    > include the URL in your assignment (you might want to give a TinyURL as
    > well), and your prof can see for herself that we're not doing your
    > homework for you.
    >
    > (Hey, chances are that she's googling for your name already...)
    >
    > Unless the assignment explicitly says "DO NOT SPLIT YOUR CODE INTO
    > FUNCTIONS", you shouldn't be marked down for writing better code than you
    > were asked for.
    >
    > On the other hand, if the assignment specifies "YOU MUST DO THIS" then
    > naturally you must follow the instructions.
    >
    > * shakes head sadly *
    >
    > Kids today, more concerned about following instructions than learning...
    >
    > * half wink *
    >
    > --
    > Steven


    I had a professor who insisted that we not use loops and what-not
    before they were taught in class as well. It seems to be a standard
    thing in Computer Science classes. Kind of like learning mathematical
    proofs or doing standard deviation the long way first. In some ways,
    it's very helpful. In others, it's extremely lame.

    Typical of the college education system though.

    Mike
     
    , Oct 29, 2007
    #11
  12. Shawn Minisall

    Guest

    On Oct 29, 4:28 pm, wrote:
    > On Oct 29, 9:26 am, Steven D'Aprano <st...@REMOVE-THIS-
    >
    >
    >
    > cybersource.com.au> wrote:
    > > On Sun, 28 Oct 2007 21:02:02 -0400, Shawn Minisall wrote:
    > > > Thanks a lot for your suggestions. Unfortunately, a lot of the issues
    > > > brought up were simply the way I was taught by my professor and the way
    > > > she wants things done,having to use a numbered menu as opposed to
    > > > entering r, p or s, being taught just to use one main function for the
    > > > entire program, having to init all variables in the program b4 the
    > > > actual program starts or else points off for each program, while
    > > > statements surrounding every input statement for input validation
    > > > purposes...

    >
    > > > Going beyond those things, would look like someone else wrote my program
    > > > since we didn't talk about or ever cover them in class.

    >
    > > Haven't you heard of "I've been reading ahead from the text book" or
    > > "I've been reading discussion groups where they talk about good
    > > programming techniques"?

    >
    > > Besides, you'll probably find your prof will forgive nearly anything if
    > > you acknowledge it first: plagiarism is considered a worse sin than
    > > failing to learn anything. Look up this thread on Google Groups, and
    > > include the URL in your assignment (you might want to give a TinyURL as
    > > well), and your prof can see for herself that we're not doing your
    > > homework for you.

    >
    > > (Hey, chances are that she's googling for your name already...)

    >
    > > Unless the assignment explicitly says "DO NOT SPLIT YOUR CODE INTO
    > > FUNCTIONS", you shouldn't be marked down for writing better code than you
    > > were asked for.

    >
    > > On the other hand, if the assignment specifies "YOU MUST DO THIS" then
    > > naturally you must follow the instructions.

    >
    > > * shakes head sadly *

    >
    > > Kids today, more concerned about following instructions than learning...

    >
    > > * half wink *

    >
    > > --
    > > Steven

    >
    > I had a professor who insisted that we not use loops and what-not
    > before they were taught in class as well. It seems to be a standard
    > thing in Computer Science classes. Kind of like learning mathematical
    > proofs or doing standard deviation the long way first. In some ways,
    > it's very helpful. In others, it's extremely lame.
    >
    > Typical of the college education system though.
    >
    > Mike


    I remember being taught Java in my first year of University and for
    the most part we were given a layout of methods, accessors and what-
    have-yas to complete. This gave us a grounding from which to go from,
    which I always thought helped a lot. However our lecturers also
    accepted people trying other techniques as long as they passed all the
    test-cases (which were shown to you when you submitted your work onto
    the website (a brilliant way to see if at each edit you were getting
    closer to having correct code))

    However the otherside of the story was that, depending on the marker
    you could get marked down for having written too many comments...

    Still while code styling is a convention or standard it isn't strict
    (though python aids in this manner!) and while certain methods are
    frowned upon and often are a poorer implementation...I've heard many
    of my friends argue "well it does what you want..."
     
    , Oct 29, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KatB
    Replies:
    0
    Views:
    467
  2. mark4asp
    Replies:
    2
    Views:
    2,272
    Harry Haller
    Nov 7, 2006
  3. Elmo Watson
    Replies:
    1
    Views:
    622
    Eliyahu Goldin
    May 8, 2007
  4. Shawn Minisall

    while within while

    Shawn Minisall, Oct 27, 2007, in forum: Python
    Replies:
    0
    Views:
    235
    Shawn Minisall
    Oct 27, 2007
  5. Replies:
    8
    Views:
    164
Loading...

Share This Page