473,508 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2318
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1352
by: tompa1969 | last post by:
Help vote for our father of the master language as the greatest in the software industry! http://www.sys-con.com/story/?storyid=47349&page=35
8
7723
by: Mike Nolan | last post by:
As far as I can tell, Postgres has no equivalent to greatest and least functions in Oracle. Yes, you can do the same thing with a case statement, but at the expense of writing MUCH longer SQL...
7
4167
by: Mathew Butler | last post by:
Suppose I have a table t with columns id, col1, col2, col3, col4, col5, col6 all numeric. I want to query the table and for each value of col<x> in the resultset I want to identify the largest value...
21
2042
by: Frederick Gotham | last post by:
I'm trying to devise a compile-time constant for X, where X is the greatest number which satisfies both the following criteria: (1) X <= DESIGNATED_MAX_VALUE (2) X % Y == 0 I'll try to...
4
2911
by: tomek milewski | last post by:
Hello, I have a map with keys that can be compared each other. I need a method that returns the lowest and the greatest key from that map. Now I'm using begin() and rbegin() which gives...
4
13671
by: sdlt85 | last post by:
Hi, Can someone help me with an idea on how to start writing a C++ code for generating greatest common divisor and the linear combination of two intergers represented as gcd(m, n)= mx + ny and...
30
8190
by: Amar Kumar Dubedy | last post by:
How to find the greatest of three numbers without using any comparison operator or ternary operator??
3
5681
by: stressedstudent | last post by:
I dont know where I am going wrong so I dont know which part to post, this is what I have, can anyone help me figure out where I am going wrong? THanks for any and all help. // into to c++ //...
35
3609
by: aarklon | last post by:
Hi all, The following question is asked frequently in interviews How to find the greatest of 2 numbers without using relational operators ? the solution i have seen is ( a+b + abs(a-b) )...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.