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
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.
Below are some useful commands to know when using Jupyter
In [ ]:
!python --version
Refer to Week 2 classwork 2 for sample exercises - https://github.com/cis024c/fall2017classwork/blob/master/week2/week2.ipynb
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
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
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
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
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
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
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
In [49]:
myList = range(10)
print myList
myList = [index for index in range(10) if index !=1 and index !=5]
print myList
In [68]:
jose = range(10)
print jose
for index in jose:
if index ==5:
jose.remove(index)
print jose
In [79]:
a = range(10)
print a
ind2remove = [1,5]
for i in sorted(ind2remove, reverse=True):
del a[i]
print a
In [ ]:
### YOUR CODE GOES BELOW
#userNumber = raw_input("Enter a Number:")
### END CODE
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