Looping Lab

In this lab, you will learn about loops. Read over Chapter 6. Answer the questions below in one big file that your TA Marc can run as a Python script/program by using the Python launcher. You can use the print command to make the output look sensible for Marc. Name the file yourlastnameLoop.py. Email your completed assignment (i.e., attach your script file) from your preferred class email address to marctomlinson@mail.utexas.edu.

1. Consider this python function:

def times2(y):

  x=2*y

(1a) Modify times2 (just add one line of code) so that it prints out two times whatever number is passed to it as an argument. Also, change the name of the function to question1a. After you define your function, call it with the argument 5, then again with the argument 2.

(1b) Modify times2 (just add one line of code) so that it returns two times whatever number is passed to it as an argument. Also, change the name of the function to question1b. After you define your function, call it with the argument 8.

2. Write a function that asks the user to type in a number then prints out whether the number is a prime number or not.

3. Write a function that asks the user to guess a random number between 0 and 100. If the user is right, tell them and end the function. If the user is wrong, tell them higher or lower and let them guess again.

To get a random number in python, include this as the first command in your program:

import random

To get a random number in your function, use this command:

int(101*random.random())