[Tutor] Question

Ashley Fowler afowler2 at broncos.uncfsu.edu
Thu Aug 23 19:02:04 CEST 2012


I am trying to complete an assignment and I am stuck at the if-else statements area. Could someone please help me?
the instructions and what I have so far are below...


 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".



What I have so far..


def main():
    l = list()
    x = eval(input('Enter a number: '))
    while x >= 0:
        l.append(x)
        x = eval(input('Enter a number: '))
    ask = input (" Do you want to perform a list operation?")
    if "yes":
        input (" Do you want to test, peek, add, or remove?")
        if "test":
            if not l:
                print("The list is not empty")
            else:
                print("The list is empty")

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






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120823/1848a9e7/attachment-0001.html>


More information about the Tutor mailing list