Easier Homework - April 27, 2015
I would very much like to hear that you are making some progress in using Python. Here are some simpler assignments. I would appreciate your letting me know that you have tried to do one or more of them. Whether it works or not, please send me the code you write. Send to harry@baya.net.
Can you write a program that prints out all the numbers from 10 to 65?
Can you modify that program so that it won't print 50 or 60, but will print all the rest.
Can you write a program to find the sum of the numbers between 1 and 100, i.e 1 + 2 +3 +4 etc. all the way to 100
Can you work with lists? Look at the program below:
#start of program
a_list = [ 9, 24, 22, 12, 48, 2, 19, 26, 18, 17, 54]
how_many = len(a_list)
count = 0
print("Here are all the numbers in the list : ")
while (count < how_many ):
num = a_list[count]
print("for",count," the number in the list is ",num)
count = count + 1
#end of program
Now, can you get that program into your Python system and run it? Type it in or paste it in.
Can you modify it so that it computes the sum of all the numbers in the list? i.e. 9 + 24 + 22 etc.
Can you modify it so that it computes the product of all the numbers in the list ?
Can you modify it so that it prints only the 5th through the 9th numbers in the list?
Can you modify it so that it will print all the numbers until one of them is equal to 19?
Note: New Python command: break will cause the program to jump out of the list. Study the program below and run it. Can you understand what the "break" statement does?
Can you modify the program so that some other condition (a matched number? a number greater than a number? or whatever) causes a "break" statement to terminate the loop?
#start of program
a_list = [ 9, 24, 22, 12, 48, 2, 19, 26, 18, 17, 54]
how_many = len(a_list)
count = 0
print("\n\nNow we will print at least some of the numbers in the list.")
while (count < how_many):
num = a_list[count]
print(num)
num_squared = num * num
if (num_squared == 4):
print( "we are leaving this loop!")
break
print("the loop continues !")
count = count + 1
if (count == (how_many- 1)):
print (" The loop printed all the numbers in the list.")
else:
print("Something caused the loop to stop before it printed all the numbers. ")
#end of program