Python Programming



I have some experience programming in Python. Here are a few examples of some code I wrote for class/leisure:


1) “The Dice Game” - This is a simple game of rolling dice. The player is looking to get a sum of over 7 between two rolls of a die. The code implements a random integer generator and time delays to keep the game unpredictable and at a smooth pace. Here is the code for the game:


import random
import time
while(True):
    
    dice1 = int(random.randint(1,6))

    dice2 = int(random.randint(1,6))

    total = dice1 + dice2

    print("Welcome to the Dice Game\n\n")

    print("To win, you must roll the dice and get numbers that add up to be greater than 7!\n\n")

    z = input("Okay, ready to roll the first die? Press enter to ROLL !\n\n")

    if z == 2:

        print("\n\nYou rolled a " + str(dice1) + "!\n\n")


    else:

        print("\n\nYou rolled a " + str(dice1) + "!\n\n")
        
    time.sleep(1.5)
    
    w = input("\n\nTime for roll number two. Think you're gunna win? Press enter to ROLL and find out!\n\n")

    if w == 2:

        print("\n\nYou rolled a " + str(dice2) + "!\n\n")

    else:
        print("\n\nYou rolled a " + str(dice2) + "!\n\n")

    time.sleep(1.5)

    if total > 7:
        print("\n\nYou got a total of " + str(total) + "! You won the game!\n\n")
        y = input("\n\nCONGRATULATIONS! Press enter to play again.\n\n")

        time.sleep(0.5)
        
        if y == 2:
                  
            print("\n\nLet's play again!\n\n")

        else:
            print("\n\nLet's play again!\n\n")
        
    else:
        print("\n\nYou got a total of " + str(total) + ". You lost the game.\n\n")

        x = input("\n\nPress enter to play again.\n\n")

        time.sleep(0.5)
        
        if x == 2:
                
            print("\n\nLet's play again!\n\n")
        else:
                
            print("\n\nLet's play again!\n\n")


2) “Word Counter” - This program allows for the user to find out how many words are in any specific text file. The functions in this code work in splitting the inputted file into arrays based on whitespace and counting the number of arrays, thus the number of words. Here is the code:

import sys

file = open("Text.txt", "r")

numberwords = 0

for x in file:

    sfunction = x.split()
    numberwords = numberwords + len(sfunction)

file.close()
print('Number of words = ' + str(numberwords))


3) “Disaster Recovery Survey” - This program surveys victims of a disaster and looks to obtain vital information to improve the recovery process. The program saves each person's answers to the survey to a text file that is named after them. In addition, a summary file keeps track of the total number of people surveyed and the times in which they were surveyed. Here is the code:


#!/usr/bin/python

x = 1

import time

while (True):

    
    
    print("This Program Seeks to Obtain Disaster Recovery Information\n\n")
    print("- Please answer the following questions so help can be brought to you efficiently.\n- Answers must be written on one line. \n- Use commas to separate information as necessary.\n- Press enter to proceed to the next question.\n\n")

    
    a = input("1) What is your full name?\n\n")
    t0 = time.ctime()
    b = input("\n\n2) What is the address of your house/apartment?\n\n")
    c = input("\n\n3) How many total people are in your household at the moment?\n\n")
    d = input("\n\n4) Do you have any kids?\n\n")

    if "Yes" in d or "yes" in d:

         e = input("\ta) What are their ages?\n\n\t")
         f = input("\n\tb) Write anything specfic that you need for your children, such as diapers, clothing, etc.\n\n\t")

    g = input("\n\n5) Do you need water?\n\n")

    h = input("\n\n6) Do you need food?\n\n")
              
    if "Yes" in h or "yes" in h:

          i = input("\ta) Is there any specific food you really need?\n\n\t")
          j = input("\n\tb) Any food allergies we need to be aware of?\n\n\t")
    k = input("\n\n7) What types of toiletries do you need?\n\n")
    l = input("\n\n8) Do you need any blankets/flashlights due to a loss of heating/electricity?\n\n")

    if "Yes" in l or "yes" in l:
        m = input("\ta) How many blankets?\n\n\t")
        n = input("\n\tb)How many flashlights?\n\n\t")
    o = input("\n\n9) Do you have any medical needs in your household?\n\n")
    
    if o == "Yes" or o == "yes":
          p = input("\n\ta) What prescriptions do you need?\n\n\t")
          q = input("\n\tb) Are you mobile?\n\n\t")
          r = input("\n\tc) Is there any emeregency medical situations that need immediate attention? If so, write them in detail.\n\n\t")

    s = input("\n\n10) Is there anything else you would like to tell us?\n\n")


    print("\n\n Thank you for answering this interview. This information will be used to get you the help you need.\n\n")



    
    file = open(str(a) + '.txt', "w")
    file.write("1) " + a)
    file.write("\n\n")
    file.write("2) " + b)
    file.write("\n\n")
    file.write("3) " + c)
    file.write("\n\n")
    file.write("4) " + d)
    file.write("\n\n")
    if "Yes" in d or "yes" in d:
        file.write("4a) " + e)
        file.write("\n\n")
        file.write("4b) " + f)
        file.write("\n\n")
    file.write("5) " + g)
    file.write("\n\n")
    file.write("6) " + h)
    file.write("\n\n")
    if "Yes" in h or "yes" in h:
        file.write("6a) " + i)
        file.write("\n\n")
        file.write("6b) " + j)
        file.write("\n\n")
    file.write("7) " + k)
    file.write("\n\n")
    file.write("8) " + l)
    file.write("\n\n")
    if "Yes" in l or "yes" in l:
        file.write("8a) " + m)
        file.write("\n\n")
        file.write("8b) " + n)
        file.write("\n\n")
    file.write("9) " + o)
    file.write("\n\n")
    if o == "Yes" or o == "yes":
        file.write("9a) " + p)
        file.write("\n\n")
        file.write("9b) " + q)
        file.write("\n\n")
        file.write("9c) " + r)
        file.write("\n\n")
    file.write("10) " + s)
    file.close()

    t1 = time.ctime()

  
    sfile = open("Disaster_Recovery_Summary.txt", "a")
    sfile.write("\n\n")
    sfile.write(str(x) + ") ")
    sfile.write(a)
    sfile.write("\n\t" + "File: " + a + ".txt")
    sfile.write("\n\t" + "Start of Interview: " + t0)
    sfile.write("\n\t" + "Time of File Creation: " + t1)
    sfile.write("\n\n")
    sfile.close()
        
    

    x = x + 1
    
 
start/classes/principlesofdesign/frank_longueira/python.txt · Last modified: 2013/02/04 18:32 by flongueira
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki