473,287 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

while within while

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
Oct 27 '07 #1
6 2398
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
Oct 28 '07 #2
On Oct 27, 7:11 pm, Shawn Minisall <trekker...@comcast.netwrote:
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

Oct 28 '07 #3
Shawn Minisall wrote:
<Snip>

also, surely it should be paper covers rock?

Tony
Oct 28 '07 #4
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
Oct 29 '07 #5
On Oct 29, 9:26 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
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 '07 #6
On Oct 29, 4:28 pm, kyoso...@gmail.com wrote:
On Oct 29, 9:26 am, Steven D'Aprano <st...@REMOVE-THIS-

cybersource.com.auwrote:
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 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
9
by: -Nacho- | last post by:
Hi all I have a table with some email addresses of a mailing list subscribers. I'm triying to send a mail to them by querying the table and sending the mail from a 'while' loop, but I only get...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
3
by: libsfan01 | last post by:
hi all in my js code i have a while loop contained within a while loop executed on the basis of a conditional if statement, what i want to do is end the entire function on the last execution on...
3
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text...
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: ...
16
by: koutoo | last post by:
I start my code with some constants then a while statement. But I have some For statements towards the end within the While statement where I start getting some errors. I'm hoping I won't have to...
15
Markus
by: Markus | last post by:
What i want to do: Get urls from the database and echo them out into a multiple columned table i.e. 4 pictures per row (recently uploaded table) MY problem is: I have, in my MySQL database,...
3
by: King Coffee | last post by:
Hi, I have an asp:hyerLink control followed by an asp:Login control. If I inadvertently press the return key while within the login control... the asp.net page will perform the hyperlink and...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.