471,312 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

greatest and least of these...

I just wrote a program to let the user input a series of whole numbers
and tell them which is least and which is greatest based off of a menu.
However, the menu isn't kicking in after they pick a number. I included
a while statement for a loop just for the menu and compared it to my
other programs that have a similar setup and are working, but I'm
stumped. Here's the program...

def main():

#define and initialize variables
#choice as int
choice = 0
#number as int
number = 0

#intro
print "WELCOME TO THE GREATEST AND LEAST NUMBER PROGRAM!"
print

#Menu loop
while choice != 2:
#display menu
print "Please choose from the following menu: "
print "1. Enter a number"
print "2. Exit"
print

#prompt user for their menu choice
choice = input("Enter your choice here: ")

#if statements to determine which choice
if choice == 1:
nums = []
while number >=0:
nums.append(number)
number = input("Please enter a number.")
elif choice == 2:
print "Have a great day!"

if len(nums) 0:
print "The smallest number that you entered was:",min(nums)
print "The largest number that you entered was:",max(nums)

else:
#invalid
print "Invalid selection. Enter either one or two."
print

Also, if they quit the program with choice #2 and entered numbers, it
should display the greatest and least of them. If they just started and
didn't enter anything and want to quit, I get an error message saying
UnboundLocalError: local variable 'nums' referenced before assignment.
Isn't the if statement supposed to keep python from going there since if
they didn't enter any input, the length of the list should just be zero.

Oct 23 '07 #1
2 2192
On Tue, 23 Oct 2007 11:56:33 -0400, Shawn Minisall wrote:
I just wrote a program to let the user input a series of whole numbers
and tell them which is least and which is greatest based off of a menu.
However, the menu isn't kicking in after they pick a number. I included
a while statement for a loop just for the menu and compared it to my
other programs that have a similar setup and are working, but I'm
stumped. Here's the program...

def main():

#define and initialize variables
#choice as int
choice = 0
#number as int
number = 0

#intro
print "WELCOME TO THE GREATEST AND LEAST NUMBER PROGRAM!"
print

#Menu loop
while choice != 2:
#display menu
print "Please choose from the following menu: "
print "1. Enter a number"
print "2. Exit"
print

#prompt user for their menu choice
choice = input("Enter your choice here: ")

#if statements to determine which choice
if choice == 1:
nums = []
while number >=0:
nums.append(number)
number = input("Please enter a number.")
Maybe you want to exchange those two last lines!?
elif choice == 2:
print "Have a great day!"

if len(nums) 0:
print "The smallest number that you entered was:",min(nums)
print "The largest number that you entered was:",max(nums)
Also, if they quit the program with choice #2 and entered numbers, it
should display the greatest and least of them. If they just started and
didn't enter anything and want to quit, I get an error message saying
UnboundLocalError: local variable 'nums' referenced before assignment.
Isn't the if statement supposed to keep python from going there since if
they didn't enter any input, the length of the list should just be zero.
Which list? If the branch for ``choice == 1`` isn't executed then the
list will never be created an the name `nums` doesn't exist.

Ciao,
Marc 'BlackJack' Rintsch
Oct 23 '07 #2
What do you mean when you say the menu doesn't kick in? Do you get an
exception, or does simply nothing happen?

Before the if statements, you should put "print choice" so you can see
what value is being returned by the input function. Also maybe "print
type(choice)" for a bit more inspection.

Speaking of which, you should probably be using something like
"int(raw_input())" instead of just "input()" - if you read the help on
the two functions you should see why.

Hope this helps.

Jason

On Oct 23, 11:56 am, Shawn Minisall <trekker...@comcast.netwrote:
I just wrote a program to let the user input a series of whole numbers
and tell them which is least and which is greatest based off of a menu.
However, the menu isn't kicking in after they pick a number. I included
a while statement for a loop just for the menu and compared it to my
other programs that have a similar setup and are working, but I'm
stumped. Here's the program...

def main():

#define and initialize variables
#choice as int
choice = 0
#number as int
number = 0

#intro
print "WELCOME TO THE GREATEST AND LEAST NUMBER PROGRAM!"
print

#Menu loop
while choice != 2:
#display menu
print "Please choose from the following menu: "
print "1. Enter a number"
print "2. Exit"
print

#prompt user for their menu choice
choice = input("Enter your choice here: ")

#if statements to determine which choice
if choice == 1:
nums = []
while number >=0:
nums.append(number)
number = input("Please enter a number.")

elif choice == 2:
print "Have a great day!"

if len(nums) 0:
print "The smallest number that you entered was:",min(nums)
print "The largest number that you entered was:",max(nums)

else:
#invalid
print "Invalid selection. Enter either one or two."
print

Also, if they quit the program with choice #2 and entered numbers, it
should display the greatest and least of them. If they just started and
didn't enter anything and want to quit, I get an error message saying
UnboundLocalError: local variable 'nums' referenced before assignment.
Isn't the if statement supposed to keep python from going there since if
they didn't enter any input, the length of the list should just be zero.

Oct 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by tompa1969 | last post: by
8 posts views Thread by Mike Nolan | last post: by
21 posts views Thread by Frederick Gotham | last post: by
4 posts views Thread by tomek milewski | last post: by
30 posts views Thread by Amar Kumar Dubedy | last post: by
35 posts views Thread by aarklon | last post: by
reply views Thread by rosydwin | last post: by

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.