Funny Python

Sono alle prime armi col Python (Wikipedia).
A dire il vero sono alle prime armi con un linguaggio di programmazione in generale. Avevo fatto degli esperimenti col C qualche anno fa e successivamente proprio col Python, ma mi ero farmato dopo poco. Beh ero ggiovane avevo altri interessi. Non che programmare sia roba da non giovani/vecchi, anzi. Boh è andata così.
Dicevo.
Sto seguendo qualche manuale in rete e un manuale cartaceo…
Devo dire che per ora mi diverto un sacco, anche con le cose più semplici (non sono arrivato ancora ai capitoli che riguardano i cicli FOR e WHILE).

Ecco un esempio di codice che mi ha fatto divertire scrivendolo:


# Main function
# After user's inputs, it calculates the two areas
# and then calls the "calc_greater" function.

def main():
    start = raw_input('This is a funny program!n' +
        'Now I will ask you some questions...n' +
        'Press ENTER to go!')
    print
    w1 = input('Enter the width of a rectangle ' +
        'of your choice: ')
    l1 = input('Now enter the lenght: ')
    w2 = input('Enter the width of a second ' +
        'rectangle: ')
    l2 = input('And finally enter its lenght: ')
    A1 = w1 * l1
    A2 = w2 * l2
    calc_greater(A1, A2)

# Calc_greater function
# It calculates the bigger rectangle and assigns
# a value to the variable "truth".
# Then it calls the "user_decis" function.

def calc_greater(val1, val2):
    if val1 > val2:
        truth = 1
    elif val1 < val2:
        truth = 2
    else:
        truth = 3
    user_decis(truth)

# User_decis function
# It asks the user which is the bigger rectangle.
# If the user's input is equal to
# "value" (so to "truth") it prints a congrats
# message, else a bad message.

def user_decis(value):
    print
    usr = input('Which is the rectangle ' +
        'with the greater area?n' +
        '(Enter "1" for the first one, ' +
        '"2" for the second one n' +
        'and "3" if you think that the ' +
        'two areas are the same): ')
    if usr == value:
        print
        print 'Alright! o/'
        print
    else:
        print
        print 'Bad, very bad... :('
        print

# The main function is called
main()

So che non è niente di speciale, spero solo che la divertanza non mi abbandoni troppo presto…

Interessante risorsa utile in sviluppo:

The Hazel Tree