Midterm Exam 2 Review Spring 2004: Form Letter
Comments, answers, and questions go here:
(Link back to Sp2004 Midterm 2 Review)
I'll take a stab at this....how off am i?
if input.find("spouse")<>-1:
if gender=="M":
file.write("And I'm sure that your wife would agree":\n")
if gender=="F":
file.write("And I'm sure that your husband would agree":\n")
if input.find("dog" cat") <>-1:
file.write("This is a particularly attractive offer for "+pet" owners!")
Summar Shoaib
| Syntax isn't quite right, Summar. What's the ":\n" for? Try running it in JES. Mark Guzdial |
how about now? I think its better although the last line keeps giving me an error...
if input.find("spouse")<>-1:
if gender=="M":
file.write("And I'm sure that your wife would agree")
if gender=="F":
file.write("And I'm sure that your husband would agree")
if input.find("dog" or "cat") <>-1:
file.write("This is a particularly attractive offer for "+pet" owners!")
And imagine the indents are there....for some reason when I put them down they still show the lines as being aligned with each other.
Summar Shoaib
| See the FAQ on how to make indents work. No, you don't want to use .find at all. Mark Guzdial |
Repeating Paul's Question: how do you get JES to show what the function actually writes?
| Open the file "formletter.txt" in the JES directory. Mark Guzdial |
def formletter(gender,lastName,city,eyeColor, spouse, pet):
file = open("formletter.txt","wt")
file.write("Dear ")
if gender=="F":
file.write("Ms. "+lastName+":\n")
if gender=="M":
file.write("Mr. "+lastName+":\n")
file.write("I am writing to remind you of the offer ")
file.write("that we sent to you last week. Everyone in ")
file.write(city+" knows what an exceptional offer this is!")
if spouse==y
if gender==m
file.write(“and I’m sure that your wife would agree”)
if spouse==y
if gender==f
file.write(“And I’m sure your husband would agree”)
file.write("(Especially those with lovely eyes of "+eyeColor+"!)")
file.write(“This is an especially attractive offer for ‘+pet+’ owners”)
file.write("We hope to hear from you soon.\n")
file.write("Sincerely,\n")
file.write("I.M. Acrook, Attorney at Law")
file.close()
this isnt working. I am getting a sytnax error on if spouse==y
whats the problem?
Amelia Cipolla
Im getting the same error on if spouse==y! argh
Summar Shoaib
I think I might have it....?
def formletter(gender,lastName,city,eyeColor,spouse,pet):
file = open("formletter.txt","wt")
file.write("Dear")
if gender=="F":
file.write("Ms. "+lastName+":\n")
if gender=="M":
file.write("Mr. "+lastName+":\n")
file.write("I am writing to remind you of the offer ")
file.write("that we sent to you last week. Everyone in")
file.write("city+ knows what an exceptional offer this is!")
file.write("(Especially those with lovely eyes of "+eyeColor+"!)")
if spouse=="Y":
if gender=="M":
file.write("And I'm sure that your wife would agree")
if gender=="F":
file.write("And I'm sure that your husband would agree")
file.write("This is a particularly attractive offer for "+pet+" owners!")
file.write("We hope to hear from you soon.\n")
file.write("Sincerely,\n")
file.write("I.M. Acrook, Attorney at Law")
file.close()
Summar Shoaib
absolutely, that is interesting...
def formletter(gender,lastName,city,eyeColor, spouse):
file = open("formletter.txt","wt")
file.write("Dear ")
if gender=="F":
file.write("Ms. "+lastName+":\n")
if gender=="M":
file.write("Mr. "+lastName+":\n")
file.write("I am writing to remind you of the offer ")
file.write("that we sent to you last week. Everyone in ")
file.write(city+" knows what an exceptional offer this is!")
file.write("(Especially those with lovely eyes of "+eyeColor+"!)")
if spouse == "Y":
if gender == "M":
file.write("And I'm sure that your wife would agree.\n")
if gender == "F":
file.write("And I'm sure that your husband would agree.\n")
file.write("We hope to hear from you soon.\n")
file.write("Sincerely,\n")
file.write("I.M. Acrook, Attorney at Law")
file.close()
is this OK?
no more
My main question is with the gender and the spouse. I know that they have to correspond. I'm just a little confused about the order. Wouldn't it be... if the gender is this, and if the spouse is this.... then. Because you know for sure that they have a gender. But you don't know if they have a spouse. So, first comes gender. Then, would come the Y or N. If there wasn't a Y or N there, then the loop would not be complete.
Yet, this also works the other way around. Spouse could come first, then gender. If the spouse is this (Y or N) and if the gender is this, then... But, the spouse being either a Y or an N corresponds to gender. So, should there be more of a relationship?
Also, what about if there is no spouse?
I am, of course, assuming, that this form letter was written before the laws in California passed!
So: does it matter?
About the pet section are we assuming that their will always be an input of cat or dog or are we suppose to take out the line "This is a particularly attractive offer for "+pet+" owners!" if the pet input is left blank.
is there any review sessions with the ta's?
Yes. There is a review session tonight at 8 PM in Physics L2. There's also one tomorrow night in the same room. We can also shorten the code a little by eliminating the "if" statements when we add the line about the pets. Any guesses as to how? Stephanie Weitzel
When i try to run my code, i get an error that says "A local or global name could not be found. You need to define the function or variable before you try to use it in any way." but i have all the variables defined.
def formletter(gender,lastName,city,eyeColor, spouse, pet):
file = open("formletter.txt","wt")
file.write("Dear ")
if gender=="F":
file.write("Ms. "+lastName+":\n")
if gender=="M":
file.write("Mr. "+lastName+":\n")
file.write("I am writing to remind you of the offer ")
file.write("that we sent to you last week. Everyone in ")
file.write(city+" knows what an exceptional offer this is!")
file.write("(Especially those with lovely eyes of "+eyeColor+"!)")
if spouse == "Y":
if gender == "M":
file.write("And I'm sure that your wife would agree.\n")
if spouse=="Y":
if gender=="F":
file.write("And I'm sure that your husband would agree." )
if pet=="dog" or "cat":
file.write("This is a particularly attractive offer for "+pet+" owners!")
file.write("We hope to hear from you soon.\n")
file.write("Sincerely,\n")
file.write("I.M. Acrook, Attorney at Law")
file.close()
Nicole Maser
Why is it with the plus signs that you cant have one before city but you do have one after? (line 10: city+) Is it because it is at the beginning of the line or what?
I dont think that you need that :\n after the last name....
def formletter(gender,lastName,city,eyeColor,spouse):
file = open("formletter.txt","wt")
file.write("Dear ")
if gender=="F":
file.write("Ms. "+lastName+":\n")
if gender=="M":
file.write("Mr. "+lastName+":\n")
file.write("I am writing to remind you of the offer ")
file.write("that we sent to you last week. Everyone in ")
file.write(city+" knows what an exceptional offer this is!")
file.write("(Especially those with lovely eyes of "+eyeColor+"!)")
if spouse =="Y":
if gender =="M":
file.write("And I'm sure that your wife would agree.")
if gender =="F":
file.write("And I'm sure that your husband would agree.")
file.write("We hope to hear from you soon.\n")
file.write("Sincerely,\n")
file.write("I.M. Acrook, Attorney at Law")
file.close()
Amy Howard
Amy, just a note, you forgot the pet part. ;)
Nevertheless, Amy's code is correct! Yay! Just a little tip for those who are still not understanding... Y means yes. As in, if you say Y where spouse is, then you will either get "...sure...wife agree" if the gener is M (as in the husband is receiving the letter) OR you would get "...sure..husband would agree..." if gener is F (as in the wife is receiving the letter). I know that the relationship between husband and wife initially really confused me, so just thought I'd help! Oh, and also, if there is anything except for Y (as in N for no, or even dsfhdkjfkjshd), then this for loop would not go through.
poof #2
FYI, if you dont want to use nested IF statements you can just use if (spouse=="Y") and (gender=="M"):
The same for female, just change gender to "F".
For those that like to save one more line of code...and please correct me if i'm wrong
Thomas Sobeck
| Exactly right! People tend to find "and" and "or" more confusing than additional lines of code, though, but you should do it however makes the most sense to you. Mark Guzdial |
Link to this Page