Name:
Answer Key

SEE BODY TAG

SEE BODY TAG

 

SEE BODY TAG


Write scripts for the following questions, and upload them to the course website.
Begin each script with a comment that includes your name, the date, and the question number.

  1. [25 pts] if-else

    Write a Python3 script that can be run from the command line. Name this script "question1.py".

    This script must decide what order its inputs are in: ascending, descending, equal, or no-order.

    $ python3  question1.py
    first number? -7.4
    second number? 0
    third number? 66
    The user inputs are Ascending 
    $ python3  question1.py
    first number? 8
    second number? 1
    third number? 2
    The user inputs are Not ordered 
    $
    
    • Input three numbers from the user, and convert them to floating-point numbers.
    • If the numbers are equal, set a variable equal to the string "Equal".
    • If the second number is larger than (or equal to) the first, and the third number is larger than (or equal to) the second, set the variable to "Ascending" instead.
    • If the second number is smaller than (or equal to) the first, and the third number is smaller than (or equal to) the second, set the variable to "Descending" instead.
    • Otherwise, set the variable to "Not ordered".
    • Finally, use a single print statement to print the variable as part of a string that reads "The user inputs are ____". (Your variable takes the place of the underline.)
     
  2. [25 pts] Files and loops

    Write a Python3 script that can be run from the command line. Name this script "question2.py".

    $ python3  question2.py
    file? Enterprise.txt
    Enterprise.txt: 232 words, average length 4.61 letters.
    $
    $ python3  question2.py
    file? zombies.txt
    zombies.txt: 4528 words, average length 4.48 letters.
    $
    $ python3  question2.py
    file? USConstitution.txt
    USConstitution.txt: 7652 words, average length 4.87 letters.
    $
    

    First download these files: (a) Enterprise.txt, (b) US constitution, (c) zombies.txt. Then read one of them, count the words and the lengths of the words, and finally report the total number of words and their average length.

    • Prompt for the name of the file.
    • Open the file for reading, and read it one line at a time.. Split each line into words.
    • Keep count of the number of words, and sum up their lengths. (Hint: Use a for loop over the words and calculate each one's length.)
    • When the file is finished, calculate the average word length as the total length of the words divided by the number of words.
    • Report the number of words and their average length. Use the format shown in the example shown on the right.
        
     
  3. [25 pts] Function Write a Python3 script that can be run from the command line. Name this script "question3.py".
    #!/usr/bin/env python3
    # question3.py
    import random
    
    # put your function here
    
    numbers = []
    for i in range(8):
        numbers.append(random.randrange(0,101))
    print("Original numbers:", numbers)
    
    answer = q3func( numbers )
    
    print("q3funct gives answer", answer)
    #--------
    

    The basic script is shown at right; you must add a function to it. You can copy-and-paste the script to start.

    578] python  question3.py
    Original numbers: [81, 30, 31, 6, 26, 39, 61, 81]
    q3funct gives answer (6, 44.375, 81)
    579]
    

    Your added function must receive a list of values, and must return a list of answers. The answer-list must contain the smallest (minimum) value, the average, and the largest (maximum) value, in that order. An example run is shown below right. (Every run will give different answers, because the numbers are random.)

     
  4. [25 pts] String slicing

    Write a Python3 script that can be run from the command line. Name this script "question4.py".

    $ python question4.py
    file? Enterprise.txt
    
    $
    pos: 39, len 26:   does it take to change a
    
    pos: 16, len 37:   continues on its five year mission.
    
    $
        
     

    First download this file: Enterprise.txt. Your script must prompt for the name of the file, then open and read it one line at a time.

    On each line, use the .find() method to locate the first occurrence of the word 'Enterprise'. Print the position of the found string, length of the slice of the line starting at the end of 'Enterprise' and running to the end (but excluding the word 'Enterprise'). Also print the following slice, again excluding 'Enterprise'.