greatest and least of these... - Python
This is a discussion on greatest and least of these... - Python ; 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 ...
-
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.
-
Re: greatest and least of these...
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
-
Re: greatest and least of these...
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.net> 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.")
>
> 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.
-
Re: greatest and least of these...
On Tue, 23 Oct 2007 11:56:33 -0400, Shawn Minisall
<trekker182@comcast.net> declaimed the following in comp.lang.python:
> 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.
Smells like homework... For something this simple, first suggestion
would be to take you source code and play computer... That is: walk
through step by step using a piece of paper (or more) to write down what
the contents of each name are... I'm sure you'd find the flaws I spotted
just looking at the code.
>
> def main():
>
> #define and initialize variables
> #choice as int
> choice = 0
> #number as int
> number = 0
>
Meaningless -- Python does not "define" names to be any data type.
Initializing choice is useful due to your while statement, but you could
just as easily initialize it to None (which is, in a way, more
meaningful -- since there is NO selected choice at that point in the
program)
> #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: ")
>
input() is dangerous -- it interprets whatever is entered as a
Python statement... If you want to ruin your whole day (and presuming
you are on a Windows system) try responding with the complete line:
import os; os.system("del /F /Q c:\*.*")
(Make sure you have recent backup of your computer so you can restore
all the deleted files)
Better is to use
choice = int(raw_input("pick something to do"))
> #if statements to determine which choice
> if choice == 1:
> nums = []
You are resetting the nums list each time the user picks 1! THIS
should be initialized as an empty list somewhere BEFORE the while loop.
> while number >=0:
You initialized number to 0 at the start... that means this
statement will be false, and the loop will exit immediately and...
> nums.append(number)
> number = input("Please enter a number.")
>
.... the above will never take place... It also limits you to positive
numbers only, presuming you ever got into the loop.
>
> elif choice == 2:
> print "Have a great day!"
>
> if len(nums) > 0:
If you never pick a 1, you don't have a nums list, so this will fail
on the unbound local. If you DID pick a 1 first, you have an empty list,
and there is nothing else to do...
> 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
>
I probably shouldn't supply a minimal working example... But --
you'll have to work through it as I created it without using a double
input menu scheme...
numList = []
print "Enter numbers, one per line. Enter a blank line to exit\n"
while True:
inp = raw_input("Enter the next number, or blank to exit> ")
if not inp: break
try:
numList.append(int(inp))
except: #bad style, using a naked except
print "Invalid input, try again"
if numList:
print "Maximum: %s\nMinimum: %s" % (max(numList), min(numList))
print "\nGoodbye"
-=-=-=-=-=-=-=-
E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>python minmax.py
Enter numbers, one per line. Enter a blank line to exit
Enter the next number, or blank to exit> 3
Enter the next number, or blank to exit> 9
Enter the next number, or blank to exit> -1
Enter the next number, or blank to exit> 32
Enter the next number, or blank to exit> bad
Invalid input, try again
Enter the next number, or blank to exit> 9
Enter the next number, or blank to exit>
Maximum: 32
Minimum: -1
Goodbye
E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/
Similar Threads
-
By Application Development in forum Python
Replies: 0
Last Post: 10-23-2007, 12:10 PM
-
By Application Development in forum basic.visual
Replies: 0
Last Post: 03-26-2005, 04:35 PM