CIS024C - Fall 2017 - Thursday 5:30-9:25pm

Homework 2

Homework 2 covers exercises in Conditionals and Repetition. It involves the user of the if..elif..else statements and the for/while loops.

You will need to download this notebook and use this as a starting point for your homework. You will just need to fill in the content of each code-block (cell) and execute. Once you have completed all the exercises, you will need to save and upload this to your github repository under a folder called hw2.

Note also the exercises build on top of one another so you might be able to do the next exercise if you have not completed the previous exercise.

Post any questions you have on our Slack at cis-024c1.slack.com

ALL THE WORK THAT WE DID IN CLASS DURING WEEK 2 IS NOW IN GITHUB AT THE BELOW LINK

https://github.com/cis024c/fall2017classwork/blob/master/week2/week2.ipynb

Please refer back to hw1 and slack for instructions on how to setup your computer for developing using Python.

Helpful Jupyter Commands

Below are some useful commands to know when using Jupyter

  1. You can add a new cell by clicking on the "+" icon on top.
  2. You can delete a cell by selecting that cell and clicking on the "scissors" icon on top.
  3. You can execute a cell by either pressing shift+enter or selecting the "play" button on top.
  4. You can create a new file in Jupyter via the File menu->New Notebook option. Make sure to select Python 2 when creating your notebook.
  5. Also, for your code blocks make sure that Code is selected instead of another option like Markdown.
  6. Use the Enter key to go to the next line in a cell to enter the next statement.
  7. You can clear results by clicking on the Cell menu item and selecting Current Output->Clear or All Output->Clear depending on whether you are trying to just clear the output for one cell or for all cells.
  8. In case your program has crashed for some reason (infinite loop, for example), you can restart your Python session by select Kernel in the menu and selecting Restart.

Check Python Version


In [ ]:
!python --version

Sample Exercises with conditionals and repetitions

Refer to Week 2 classwork 2 for sample exercises - https://github.com/cis024c/fall2017classwork/blob/master/week2/week2.ipynb

Exercise 1 - if statement

Write a python program to accept a number from the user. Use the if statement to check to see if the number is greater than 100. If the number is greater than 100, print the message "Found a number greater than 100"


In [1]:
### YOUR CODE GOES 
numberStr = raw_input("Enter a number:")
number = int(numberStr)

if number > 100:
    print "Found a number greater than 100"
### END CODE


Enter a number:234
Found a number greater than 100

Exercise 2 - if...else statement

Write a python program to accept the name of a user. Use the if statement to check to see if the name is "Joe". If the name is "Joe", print the message "User Joe was entered", otherwise if the name is not "Joe", print a message saying that "User Joe was not entered"


In [3]:
### YOUR CODE GOES BELOW
userName = raw_input("Enter your name:")

if userName == "Joe":
    print "User Joe was entered"
else:
    print "User Joe was not entered"

### END CODE


Enter your name:Leonardo
User Joe was not entered

Exercise 3 - if..elif...else statement

Write a python program to accept the salary of the user.

If the salary is less than 70,000, then print the message "User salary is less than 70000", otherwise if the salary is less than 100,000, print the message "User salary is less than 100,000, otherwise, print the message "User salary is greater than or equal to 100,000"


In [4]:
### YOUR CODE GOES BELOW
userSalary = raw_input("Enter your annual salary:")
salary = int(userSalary)
if salary < 70000:
    print "User salary is less than 70000"
elif salary < 100000:
    print "User Salary is less than 100,000"
else: 
    print "User salary is greater than or equa; to 100,000"

### END CODE


Enter your annual salary:2344
User salary is less than 70000

Exercise 4 - for loop

Write a program to get a number from the user. Find the some of all numbers from 1 to the number that the user entered.

For example, if the user enters 10, then you would need to find the sum of all numbers from 1 to 10 like so. Use the range statement in the for loop...see week 2 classwork for more information on the range statement

1+2+3+4+5+6+7+8+9+10

Print the result


In [8]:
### YOUR CODE GOES BELOW
number = raw_input("Enter a number:")
newNumber = int(number)

total = 0
for index in range(0,newNumber):
    total = total + index
print total

### END CODE


Enter a number:99
4851

Exercise - while loop

Guessing a number.

Initialize a variable called myNumber to any arbitrary value. For example, maybe you thought of a number 9

Use the while loop to ask the user to enter a number. Check to see if the number entered by the user matches the number myNumber that you thought of. If there is a match, break from the while loop and print "Yippee! User guessed the right number". If there is no match allow the while loop to continue to ask the user for a number until a match is found


In [9]:
### YOUR CODE GOES BELOW
myNumber = 9
while True:
    userNumber = raw_input("Enter a number:")
    newUserNumber = int(userNumber)
    
    if newUserNumber == myNumber:
        print "Yippee!"
        break
    else:
        print "Keep Guessing..."

### END CODE


Enter a number:2
Keep Guessing...
Enter a number:3
Keep Guessing...
Enter a number:5
Keep Guessing...
Enter a number:7
Keep Guessing...
Enter a number:8
Keep Guessing...
Enter a number:9
Yippee!

OPTIONAL EXERCISES

Below is a set of optional exercises. These will not be graded but the solutions will be posted. I would strongly encourage you to try these out if you are done with the mandatory homework exercises to improve your understanding of python.

Exercise 6

Write a Python program to convert temperatures to and from celsius, fahrenheit.

[ Formula : c/5 = f-32/9 [ where c = temperature in celsius and f = temperature in fahrenheit ]


In [11]:
### YOUR CODE GOES BELOW
temperatureInC = float(raw_input("Enter a T in Celsius:"))
temperatureInf = float(raw_input("Enter a T in Fahrenheit:"))
x = 5
y = 9
x1 = float(x)
y1 = float(y)
number = (x1/y1)
c = number*(temperatureInf-32)
print "The T in degree C =", c

f = y1/x1*temperatureInC + 32
print "The Temperature in degree F =", f


### END CODE


Enter a T in Celsius:212
Enter a T in Fahrenheit:9
The T in degree C = -12.7777777778
The Temperature in degree F = 413.6

In [ ]:
# The trick here is to recall the difference between flots and integers.  The equation required floats. to work properly.
temperatureInC = float(raw_input("Enter a T in Celsius:"))
temperatureInf = float(raw_input("Enter a T in Fahrenheit:"))

c = (5.0/9.0)*(temperatureInf-32)
print "The T in degree C =", c

f = 9.0/5.0*temperatureInC + 32
print "The Temperature in degree F =", f

Exercise 7

Write a Python program that prints all the numbers from 0 to 10 except 1 and 5. Use the **for** loop. Is there a way that I can remove 1 and 5 with one line of code

In [49]:
myList = range(10)
print myList
myList = [index for index in range(10) if index !=1 and index !=5] 
print myList


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 3, 4, 6, 7, 8, 9]

In [68]:
jose = range(10)
print jose
for index in jose:
    if index ==5: 
        jose.remove(index)
        print jose


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 6, 7, 8, 9]

In [79]:
a = range(10)
print a
ind2remove = [1,5]

for i in sorted(ind2remove, reverse=True):
    del a[i]
print a


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 3, 4, 6, 7, 8, 9]

Exercise 9

Use a while loop to ask the user to enter a number. Print a running total of all the numbers that the user enters. If the user enters 0, break from the while loop.


In [ ]:
### YOUR CODE GOES BELOW
#userNumber = raw_input("Enter a Number:")


### END CODE

Exercise 10

Write a Python program to create the multiplication table (from 1 to 10) of a number. Use the formatted print statements that you learnt in Week 2 and the for loop to print this table.

Input a number: 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60


In [ ]:
### YOUR CODE GOES BELOW


### END CODE