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 > <[EMAIL PROTECTED]> 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... > -- http://mail.python.org/mailman/listinfo/python-list