UserPreferences

Assignment1Comments


Other pages: Bio/Geo/CS250, MainModelingWiki


Assignment 1

Here are some programs your classmates submitted for Assignment 1. They all work, though some work better than others.

They all share a big initial if statement, sort of. The first two program below actually have two if statements to take care of the Option 1 / Option 2 split. The third program below uses an if-else statement. What's the difference? The difference comes out if the user chooses some imaginary Option 3. In the first program, a user who chooses Option 3 simply exits the program, since everything else the program does is contingent on choosing either 1 or 2. In the third program, a user who chooses option 3 is given Option 2, since Option 3 counts as an "else."

One more thing: the second program does something clever with internal function definitions. Python doesn't require that you define functions at the top of the program: a def statement is a program statement like any other, so it can go anywhere in the program. The second program uses def statements embedded in if statements, so not all of the definitions even happen. It's a clever use of def, but I don't really recommend it. That kind of cleverness can just be confusing for a beginning programmer, and for now I suggest that you use def statements only at the thop of the program, outside of what you think of as the main part of the program, and use them as a way of organizing your code and setting aside segments of code for possible re-use in later programming projects.

First program:

option=raw_input("Welcome to Python Geek-world.  What option would you like, 1 or 2?")
if option == "1":
    n=input("You've got it! Option 1: Enter a positive integer.")
    if n < 0 :
        n=n*-1
        t=n
        a=1
        while n > 1 :
            a=a*n
            n=n-1
        print "The factorial of", t,"is",a,"."
    else:
        t=n
        a=1
        while n > 1 :
            a=a*n
            n=n-1
        print "The factorial of", t,"is",a,"."
                          
if option == "2":
    n=input("You've got it! Option 2: Enter a positive integer.")
    if n < 0:
        n=n*-1
        t=n
        k=2**n
        print "The answer of 2^",t,"is",k,"."
    else:
        k=2**n
        if n == 0 :
            print "The answer of 2^0 is 1."
        else:
            t=n
            k=2**n
            print "The answer of 2^",t,"is",k,"."

Second program:

opt=raw_input("Enter option 1 or option 2: ")
if opt == "option 1":
    number=input("Please enter a positive integer: ")
    if number < 0:
        b=(number * -1)
        def a(b):
            if b == 0:
                return 1
            else:
                return b * a(b-1)
    else:
        b=number
        def a(b):
            if b == 0:
                return 1
            else:
                return b * a(b-1)
    print " The factorial of your integer is" , a(b)   
if opt == "option 2":
    number=input("Please enter a positive integer: ")
    if number < 0:
        b=(number * -1)
        def a(b):
            if b == 0:
                return 1
            else:
                return 2**b
    else:
        b=number
        def a(b):
            if b == 0:
                return 1
            else:
                return 2**b
    print "2 to the power of your integer is" , a(b)

Third program:

print "Welcome to ANONYMOUS's Factorial Nightmare."
s = input("Please select Option 1 or Option 2. (1/2)")
print "Thank you for selecting Option" , s
if s == 1 :
    n=input("Please enter a positive integer.")
    if n > 0 :
        a = n - 1
        while a > 0 :
            n = a * n
            a = a - 1
        print "The factorial of your integer is" , n , "."
    else:
        n = n * -1
        a = n - 1
        while a > 0 :
            n = a * n
            a = a - 1
        print "The factorial of your integer is" , n , "."
else:
    n=input("Please enter a positive integer.")
    if n < 0 :
        n = n * -1
        t = 2**n
        print "2 to the power of your integer is" , t , "."
    elif n > 0 :
        t = 2**n
        print "2 to the power of your integer is" , t , "."
    elif n == 0 :
        print "Your answer is 1."