CS 115 Summer 2017,

Lab 5 :

Due in Thursday 6/29/2017 in Lab

Educational goals of this lab - verify that every student can do the following:

Verify that every student can use loop for solving problems the concepts of:

  • Use loop (for, while).
  • Using differetn structure of if statments.
  • Output operators.
  • typecasts for input of numbers.



    Overview:

    Loops in programming are used to perform repetitive tasks. If there is a statement which is to be repeated multiples times we use loop. There are generally two types of loops for loop, and while loop. In this lab we will implement different program using for loop:



  • Problem1: Finding the sum
    Write a python program to print the sum of all odd numbers from 1 to n using for loop. Where n is a value that should be read from the user.

  • Run Examples
    When you have the program running correctly, Verify that your program produces the same output as in the following test cases.

  • --- Test case 1 ---
    Input range: 5
    The sum of odd numbers from 1-5: 9
    
    --- Test case 2 ---
    Input range: 100
    The sum of odd numbers from 1-100: 2500
    



  • Problem2: Find the power of a number
    Write a python program to find power of any number using for loop. The program should not use built-in math.pow() function. How to find power of any number without using built-in functions in python programming.

  • Run Examples
    When you have the program running correctly, Verify that your program produces the same output as in the following test cases.

  • --- Test case 1 ---
    Input the base number: 2
    Input the exponent value: 5
    
    The result of 2^5: 32 
    



  • Problem3: Guess a word
    Write a program which keeps prompting the user to guess a word. The user is allowed up to ten guesses. Write your code in such a way that the secret word and the number of allowed guesses are easy to change. If the user guess the right word get out of the loop (hint: Use the reserved word break) Print messages to give the user feedback.

  • Run Example
    When you have the program running correctly, Verify that your program produces the same output as in the following test cases.

  • --- Test case 1 ---
    welcome to our simple and easy game
    You will be asked to guess our secret word
    Our secret word is a name of a sport, Please enter your guess: basketball
    Please try again :( 
    Our secret word is a name of a sport, Please enter your guess: soccer
    Good Job :) 
    
    



  • Program Prolog
    For every of the the problem above, provide your solution in a file with the comment and prolog provided explaining your information and the program details:
    # supply program prolog
    # Prolog
    # Author:  Mary Smith
    # Email:  Mary.Smith@uky.edu
    # Section: 00
    # Date: 8/31/16
    
    '''
      Purpose: ....
      Preconditions: ....
      Postconditions: ....
    '''
    

  • Requirments:
    1- Choose names for your variables that make sense, dont use short names consist of one letter. Use good, multi-character, meaningful identifiers (variables) names.

    2- Your output should match the run examples provided with the problems above.

    3- Document your work by putting comments at the top of your program, remember that comments in python are either: starts with # in case the comment is one line only or ''' and ''' if the comment is taking more than one line.

    4- At the top of your program, you should have a prolog (name, date, etc.)

    5- Save the solution for problem #1 as sum.py, problem #2 as pow.py, and problem#3 as guess.py.

  • When you are done with the three problems, show your three files to the instructor. Then zip the files and submit your work as a submission for Lab 5 on CANVAS.