[Tutor] Question

eryksun eryksun at gmail.com
Thu Aug 23 21:41:59 CEST 2012


On Thu, Aug 23, 2012 at 1:02 PM, Ashley Fowler
<afowler2 at broncos.uncfsu.edu> wrote:
>
>  Instructions: Your "main" function should do the following:
> (1) create an empty list;
> (2) ask the user if he/she wants to perform a list operation.
>      if "yes":
>          (a) prompt the user for the operation:
> 		"test", "peek", "add", or "remove";		
>          (b) perform the operation:
> 	   (i) if "test" then print whether the list is empty or not;
> 	   (ii) if "peek" then print the number at the beginning of the
> 		list (with a label) but don't remove it from the list;
> 	   (iii) if "add", then prompt the user for a number and add
> 		it to the end of the list;
> 	   (iv) if "remove", then delete the number at the beginning
> 		of the list and print it (with a label);
> 	 (c) print the entire list from beginning to end;
>      repeat step (2) until the user enters "no".
>
>
> def main():
>     l = list()

You can also use "l = []". By the way, "l" is a bad variable name. It
looks like a number "1" in many fonts, and it isn't descriptive. You
could be very original and call it

    numbers = []

>     x = eval(input('Enter a number: '))
>     while x >= 0:
>         l.append(x)
>         x = eval(input('Enter a number: '))

Don't use eval. Use float or int. Why are you looping until it's
negative? According to your problem specification, a number should be
appended when the user requests to "add" a number, and it says nothing
about the valid range.

More importantly this is your main loop and the statements that
follows aren't in it. Pay close attention to indent level in Python
code.

>     ask = input (" Do you want to perform a list operation?")
>     if "yes":

The "if" sees a literal "yes" and treats that as a True statement --
always. Instead you need to do something like the following:

    run = input("Do you want to perform a list operation? (yes/no) ")
    if run == "yes":

Let's show it in the while loop where it belongs:

def main():
    numbers = []
    run = "yes"
    while run != "no":
        run = input("Do you want to perform a list operation? (yes/no) ")
        if run == "yes":

>         input (" Do you want to test, peek, add, or remove?")
>         if "test":

OK, now you haven't even assigned the input to a variable. You're
being cheeky, eh? Let's give this one an original name, too. Call it
"op" for operation.

            op = input("Do you want to test, peek, add, or remove? ")
            if op == "test":

>             if not l:
>                 print("The list is not empty")
>             else:
>                 print("The list is empty")

This is apparently the opposite of what you thought. The expression
"not l" is True when the list *is empty*. Let's just use "if numbers"
(using the new, more descriptive name for the list):

                if numbers:
                    print("The list is not empty")
                else:
                    print("The list is empty")

>         elif "peek":
>             print(l[0])

What will happen if the list is empty and you try to get index 0?

For the "remove" operation, look into the list method "pop".


More information about the Tutor mailing list