![]() |
![]() First Summer 2006 |
Solutions will be added shortly.
This program simply implements a formula:
mileage = float(raw_input("Car's efficiency (miles per gallon): ")) fuel = float(raw_input("Amount of fuel (gallons): ")) print "The car's autonomy is:", mileage * fuel, "miles"
This problem is very simple, and short, if we use a loop (as we should, now):
balance = float(raw_input("Please enter initial balance: ")) rate = 0.05 for i in range(1, 11): balance = balance + balance * rate print "After", i, "years the balance becomes", balance
Note this is slightly different from the original program that was asking how many years until the balance doubles.
This program appears in the lecture notes for Monday May 22.
def average(scores): sum = 0 for score in scores: sum += score return sum/float(len(scores)) scores = [] score = raw_input("Enter: ") while score != 'done': scores.append(int(score)) print "Current average is: ", average(scores) score = raw_input("Enter: ") print "Thank you for using this program."
It was also discussed that day and then again on Wednesday.
This is a simple eggy-peggy program:
sentence = (raw_input("Enter sentence: ")).lower() sentence = sentence.replace("a", "(a)") sentence = sentence.replace("e", "(e)") sentence = sentence.replace("i", "(i)") sentence = sentence.replace("o", "(o)") sentence = sentence.replace("u", "(u)") print sentence
For a more general approach see below.
Application of the general eggy-peggy program:
sentence = raw_input("Enter sentence: ") translation = "" for e in sentence: if e in "aeiou": # skip it, it is a vowel translation = translation + "" else: translation = translation + e print translation
This is a simple program, especially since we assume a program-friendly user:
one = int(raw_input("Please enter the first number: ")) if one < 0 or one > 100: print "Sorry, ", one, "is not a valid score." else: two = int(raw_input("Please enter the second number: ")) if two < 0 or two > 100: print "Sorry,", two, "is not a valid score." else: three = int(raw_input("Please enter the third number: ")) if three < 0 or three > 100: print "Sorry,", three, "is not a valid score." else: print "GPA is: ", (one+two+three)/3.0
Obviously, other ways of doing this are quite possible and acceptable.
Here's a possible solution:
choice = int(raw_input("""Please choose from the menu below: [1] The Great Beyond [2] Fretless [3] Losing My Religion [4] It's a Free World, Baby What would you like to listen to? Please enter a number: """)) if choice == 1: print """ I've watched the stars fall silent from your eyes All the sights that I have seen I can't believe that I believed I wished That you could see There's a new planet in the solar system There is nothing up my sleeve """ elif choice == 2: print """ He's got his work and she comes easy They each come around when the other is gone Me, I think I got stuck somewhere in between I wouldn't confide in the Prodigal Son The die has been cast, the battle is won The bullets were blanks, a double aught gun I couldn't admit to a minute of fun """ elif choice == 3: print """ Life is bigger It's bigger than you And you are not me The lengths that I will go to The distance in your eyes Oh no I've said too much I set it up""" elif choice == 4: print """ I was hungry when I said I never got to sleep You go ahead You can get whatever you want There's a feeling in my belly It's the new tomorrow scene It's an interesting job It's the fireworks""" else: print "That's not a valid choice. "
This program was worked out in class on Tuesday by Joel Richardson (Lecture Notes Eleven):
import random good = 0 total = 0 for i in range(10): one = random.randrange(-50, 50) two = random.randrange(-50, 50) answer = raw_input("What is " + str(one) + " + " + str(two) + " ? ") if one + two == int(answer): print "Very good!", good = good + 1 else: print "Wrong, the answer was:", (one + two), total = total + 1 print "(So far you have", good, "out of", total,")" print "Game over, your final score is", good, "out of", total
The only difference between this program and that one was that here we use a loop, as we should.
Solution for this problem was worked out in class on Tuesday (Lecture Notes Eleven):
time = raw_input("Enter the time please: ") hour = int(time[:2]) minute = int(time[3:]) for i in range(15): minute = (minute + 1) % 60 if minute == 0: hour = (hour + 1) % 24 print ("00" + str(hour))[-2:] + ":" + ("00" + str(minute))[-2:]
Don't forget, there's always more than one way to write a program.
We have also discussed the code above in a different context on Wednesday, in class and lab.
Another problem in which the main goal is to practice if statements:
income = float(raw_input("Please enter the income: ")) percent = 0.0 if income > 10000 or income < 4000: print "Sorry the income doesn't sound right." else: if income > 8000: percent = 0.3 else: if income > 6000: percent = 0.2 else: percent = 0.1 print "For an income of", income, "the taxes are " + str(percent * 100) + "%, that is a total of:", (income * percent * 100) / 100.0
So this was a simple problem.
This is a simple looping problem:
word = raw_input("Please enter the word: ") for i in range(len(word)): word = word[-1:] + word[:-1] print i+1, word
Here are two solutions worked out with Joel Richardson:
message = raw_input("Give me the word: ") dictionary = {} for e in message: if dictionary.has_key(e): dictionary[e] = dictionary[e] + 1 else : dictionary[e] = 1 print "I have built the dictionary:" for e in dictionary.keys(): print "Letter " + e + " occurs " + str(dictionary[e]) letter = raw_input("Give me the letter: ") if dictionary.has_key(letter): print "Letter " + letter + " occurs " + str(dictionary[letter]) + " times in " + message else: print "Letter " + letter + " does not occur in " + message print "Again:" count = 0 for e in message: if letter == e: count = count + 1 print count
Here's a solution to this problem:
one = raw_input("Please enter the first word: ") two = raw_input("Please enter the second word: ") result = "" count = 0 for i in range(min(len(one), len(two))): if one[i] == two[i]: result += str(i) + " " count += 1 if result: print one, "and", two, "are identical in", count, "positions:", result else: print one, "and", two, "do not match anywhere"
I choose to implement the "more sophisticated" vending machine.
balance = 0 input = raw_input("Please enter coin: ") while input != 'done': coins = input.split() for coin in coins: if coin == 'nickel': balance += 5 elif coin == 'dime': balance += 10 elif coin == 'cent': balance += 1 elif coin == 'quarter': balance += 25 else: print "Sorry,", coin, "is an unknown denomination, and therefore is not accepted." print "Your current balance is: $" + str(balance/100.0) input = raw_input("Please enter coin: ") print "Thank you for using this program."
It's not that much more sophisticated, but it is a bit more flexible than the original one.
Solution to this problem was basically worked out in class by Sally Wood:
def tokens(line): tokens = line.split() return len(tokens) def nonspace(line): tokens = line.split() sum = 0 for token in tokens: sum = sum + len(token) return sum bob = "" jim = "" tom = "" line = raw_input("> ") while line != 'done': if len(line) > len(bob): bob = line if tokens(line) > tokens(jim): jim = line if nonspace(line) > nonspace(tom): tom = line line = raw_input("> ") print bob print jim print tom
Here's how this program works when you run it and enter the lines by hand, ended by 'done'
.
>>> > I've watched the stars fall silent from your eyes > All the sights that I have seen > I can't believe that I believed I wished > T h a t y o u c o u l d s e e > There's a new planet in the solar system > There is nothing up my sleeve > done There is nothing up my sleeve T h a t y o u c o u l d s e e I've watched the stars fall silent from your eyes >>>
Solution for this problem will be posted Tue May 30, after Homework Six is collected.
Solution for this problem will be posted Tue May 30, after Homework Six is collected.
Solution for this problem will be posted Tue May 30, after Homework Six is collected.
Solution for this problem will be posted Tue May 30, after Homework Six is collected.
Solution for this problem will be posted Tue May 30, after Homework Six is collected.
Solution for this problem will be posted Tue May 30, after Homework Six is collected.
This problem appears in Lecture Notes Eight, for May 18:
due = raw_input("Due: ") pay = raw_input("Pay: ") change = int(100 * float(pay)) - int(100 * float(due)) print change / 25, " quarters" change = change % 25 print change / 10, " dimes" change = change % 10 print change / 5, " nickels" change = change % 5 print change, " cents"
This problem appears in Lecture Notes Eight, for May 18:
sentence = raw_input("Enter sentence: ") sentence = sentence.replace("e", "egge") sentence = sentence.replace("a", "egga") sentence = sentence.replace("i", "eggi") sentence = sentence.replace("o", "eggo") sentence = sentence.replace("u", "eggu") print sentence
But this is not the real (most general) solution. Instead one should use:
sentence = raw_input("Enter sentence: ") translation = "" for e in sentence: if e in "aeiou": translation = translation + "aeiou" + e else: translation = translation + e print translation
In this second solution replace the second "aeiou"
by "egg"
to obtain the original effect.
This was problem Problem Six in the Lab Assignment for May 16 (Lab Notes Four):
one = int(raw_input("Please enter the first number: ")) two = int(raw_input("Please enter the second number: ")) print one, "+", two, "=", one + two print one, "-", two, "=", one - two print one, "*", two, "=", one * two print "avg(" + str(one) + ", " + str(two) + ") = ", (float(one) + two) / 2 print "dist(" + str(one) + ", " + str(two) + ") = ", abs(one - two) print "max(" + str(one) + ", " + str(two) + ") = ", (one + two + abs(one - two)) / 2 print "min(" + str(one) + ", " + str(two) + ") = ", (one + two - abs(one - two)) / 2
This is again a very instructive problem.
It shows how you can calculate the largest of two numbers using only:
abs()
Like some of the problems we've seen in class early in the semester it requires a certain amount of creativity.
This class hopes to help you hone your problem solving skills by demystifying situations like the one above.
This only requires patience, practice and an open mind.
This is a problem we solved in lab, then discussed again in class:
score = float(raw_input("Enter numeric score then press Enter : ")) if score >= 4.0: print "A+" elif score >= 3.85: print "A" elif score >= 3.5: print "A-" elif score >= 3.15: print "B+" elif score >= 2.85: print "B" elif score >= 2.5: print "B-" elif score >= 2.15: print "C+" elif score >= 1.85: print "C" elif score >= 1.5: print "C-" elif score >= 1.15: print "D+" elif score >= 0.85: print "D" elif score >= 0.35: print "D-" else: print ""
Determining the actual cutoffs is the main focus here.
You will see that it requires a bit of patience to determine them.
In addition, we have to do something about F+
(which doesn't exist) so that area also needs some attention.
Other than those, this is a pretty straightforward problem, don't you think?