Skip to main content

Python Practice Excercises

Guessing Game


import random

# Computer picks a number between 1 and 100
def computerguess():
    print "Mac: I am ready!"
    rdm = random.randint(1,100)
    return rdm

# Checks if the users number is High or Low
def checklevel(r, g):
    if(r < g):
        print "Your number is High"
    elif(r > g):
        print "Your number is Low"
  
def main():   
    
    rdm = computerguess()
    guess=input("Guess the number! ")   
    
    while(rdm!=guess):
        checklevel(rdm,guess)
        guess=input("Guess again")
    print "You got it"       
    
if __name__ == '__main__':
    main()












Leap Year Finder



def checkleapyear(x):
    if(x >= 400):
        if((x%4==0 and x%100!=0) or x%400==0):
            print "{} is a leap year".format(x)
        else:
            print "{} is not a leap year".format(x)
    else:
        if(x%4):
            print "{}is a leap year".format(x)
        else:
            print "{} is not a leap year".format(x)                

def main():
    
    checkleapyear(400)
        
if __name__ == '__main__':
    main()

Loan and Payment Calculator

Powerball generator



#!/usr/bin/python3
#random
import random

#Generate the required number of sets of Lucky numbers and Powerbell number. Drum1 has 53 balls and Drum2 has 42 balls
def powerballgen(n):
    Drum1 = list(range(1,53))
    Drum2 = list(range(1,42))
    j=k=0
    while(k<n):
        cset=[]
        for i in range(1,6):
            set1num = random.choice(Drum1)
            cset.append(set1num)
            Drum1.remove(set1num)
        set2num = random.choice(Drum2)
        cset.append(set2num)        
        for j in range(0,5):
            print cset[j],
        print Powerball:" + str(set2num),
        k=k+1
        Drum1 = list(range(1,53))
        Drum2 = list(range(1,42))
        print         
    
def main():
    powerballgen(5)

if __name__ == '__main__':
    main()




Comments

Popular posts from this blog

GIT/GITHUB

GIT for source code management. To see where is GIT installed: $ which git To get the version of GIT: $ git --version Configuration: Three places where GIT stores configuration information System -     /etc/gitconfig User -       ~/.gitconfig Project -   my_project/.git/config Update configuration: For System:   $ git config  --system For User:   $ git config --global For project: $ git config $ git config --global user.name "Mohan Donthabhaktuni" $ git config --global user.email "mohand.bi@outlook.com" To view the configuration details: $ git config user.name   (shows the user name) To view all the configuration details: $ git config --list These configuration details get stored in the .gitconfig file under the users home directory Setup Auto-completion: $ ~ mohand$ cd ~ $ ~ mohand$ curl -OL https://githum.com/git/git/raw/master/contrib/completion/git-completion.bash $ mv ~/gi...

Installing MYSQL Test Environment on Mac using XAMPP package

SID - SQL Interactive Demonstrator is a web based MYSQL client. Written in PHP. Hence it requires a web server that supports PHP to use it with a standard browser. There is an excellent cross platform package called XAMPP that includes, MYSQL, The Apache Webserver, PHP and a bunch of other things. Steps: 1. Installing XAMPP 2. Setting up MYSQL users 3. Installing SID 1 - Installing XAMPP Make sure that no other web server is running. Also Skype runs (I think this applies to windows only b/c my Skype is still running on my Mac) on the same port as the web server and it will prevent Apache from running. So it turn it off before installing XAMPP. - Download and install XAMPP for OS X from apachefriends.org (or from here http://bw.org/ldcsql/) - Launch the Finder window in Mac. Go to Applications > XAMPP and drag manager-osx application to the task bar - Launch the manager-osx. Provide the administrator password and start the My SQL database from the Manage Servers...

Tableau

Tableau Command Line:     Check the status of Tableau service:           tabadmin status --v       Start/stop/restart Tableau service     tabadmin stop     tabadmin start     tabadmin restart       Saving configuration changes:           tabadmin config Tableau Configuration:     Update Logo:           tabadmin customize sign_in_logo "<png_image_path>"      tabadmin customize header_logo "<png_image_path>"     Configure Tableau Desktop Reporting      Tableau Upgrade:      1. Backup:           tabadmin backup <backupfilename> -d            -d will ...