Computer Science Homework
# Assignment 2.1 - combining concepts.py
# By _____, _____, and _____
# 10 points
# this is a partner assignment (group size 1-3)
# being able to take 2 (or more) separate programs that you have found or solved
# and combining them into a single working program is an important skill
# rule - begin with your assignment: formula from last week (or a new formula if you prefer)
# rule - add to this: a functioning "do you want to use this again [yes/no] feature"
# rule - add to this: only accepting "yes"/"no" responses and otherwise re-asking
# challenge - also include a menu of multiple formula options
# sub challenge - ...and only accept legal choices from this menu of options
# challenge - there is no limit to how complex your formulae may be
# note - it is OK to "copy" from examples we've seen ("copy" means "use as guidance"),
# feel free to refer to the example files in this chapter
# correctly assembling together features from those example files is your challenge here
# this is a good time to think about "compehensive testing"...
# be sure your code works for "yes", "no", "illegal response"...
# most simple example (contains complex formula & repitition)
print ("version 1")
print ('includes small error - a "no" response ends the program, "yes" OR ANY OTHER RESPONSE continues the program')
counter = 1
playing = True
while playing:
print ("this will solve the distance formula for 2 points: (x1, y1) & (x2, y2)")
x1 = float(input("please enter x1: "))
y1 = float(input("please enter y1: "))
x2 = float(input("please enter x2: "))
y2 = float(input("please enter y2: "))
ans = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** .5
print ("the distance between those 2 points is",ans)
ans = input("want to play again? enter yes or no: ")
if ans == "no":
playing = False
print ("done, thank you.")
print ("\n \n \n")
# more simple example (contains simple formula, repitition, & only accepts yes/no)
# this is somewhat ugly - we've "pasted" "menu choices" and "play again" together...
# correctly. It works but it isn't easy to read
print ("version 2a - fully functioning")
playing = True
while playing:
print ("this will solve the distance formula for 2 points: (x1, y1) & (x2, y2)")
x1 = float(input("please enter x1: "))
y1 = float(input("please enter y1: "))
x2 = float(input("please enter x2: "))
y2 = float(input("please enter y2: "))
ans = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** .5
while True:
response = input("do you want to play again (yes or no)?: ").lower()
if (response == "yes") or (response == "no"):
break
else:
print ("illegal response, try again")
if response == "no":
print ("thank you, bye")
playing = False
print ("\n \n \n")
# more elegant example
# this combines our functionalities in a more clean and elegant way...
# than the previous example, although it is not as direct a "copy/paste"...
# as the previous example
print ("version 2b - fully functioning")
while True:
answer = input("would you like to solve the distance formula (yes/no)? ").lower()
if answer == "yes":
x1 = float(input("please enter x1: "))
y1 = float(input("please enter y1: "))
x2 = float(input("please enter x2: "))
y2 = float(input("please enter y2: "))
ans = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** .5
if answer == "no":
break
print ("thank you, bye")
print ("\n \n \n")
# complex example (contains complex formula, repitition, & only accepts yes/no)
print ("version 3 - fully functioning")
while True:
goal=int(input("what # prime would you like me to find? "))
counter = 0
testnum = 1
while counter < goal:
testnum = testnum + 1
answer = "prime"
for divtest in range (2,testnum):
if testnum % divtest == 0:
answer = "not prime"
if answer == "prime":
counter = counter + 1
print (testnum,"is prime number",counter)
invalid = True
while invalid:
response = input("would you like to do this again: yes or no? ")
if response == "yes" or response == "no":
invalid = False
else:
print ("illegal response, please try again")
if response == "no":
print ("thanks, bye.")
break
print ("\n \n \n")
# most complex example (contains menu of formulae, repition, and only accepts correct choices)
# draft - partially complete
# notice how this drafting process helps us understand how a large, complex program
# can be built from simple parts
print ("version 4 - partial draft - using break")
# this "partial draft" is an example of a well-organized
# drafting process that can make creating large and complex code more simple
while True:
print ("would you like to calculate a distance, a midpoint, a square root, or stop")
response = input("please enter DIST, MID, ROOT, or STOP: ").lower()
if response == "dist" or response == "mid" or response == "root" or response =="stop":
invalid = False
if response == "stop":
break
if response == "dist":
print ("do dist") #insert dist formula code here
if response == "mid":
print ("do mid") #insert midpoint formula code here
if response == "root":
print ("do root") #insert root code here
print ("thanks, bye")
print ("\n \n \n")
# most complex example (contains menu of formulae, repition, and only accepts correct choices)
print ("version 4 - final draft - using break")
while True:
print ("would you like to calculate a distance, a midpoint, a square root, or stop?")
response = input("please enter DIST, MID, ROOT, or STOP: ").lower()
if response == "dist" or response == "mid" or response == "root" or response =="stop":
invalid = False
if response == "stop":
print ("thanks, bye")
break
if response == "dist":
print ("this will solve the distance formula for 2 points: (x1, y1) & (x2, y2)")
x1 = float(input("please enter x1: "))
y1 = float(input("please enter y1: "))
x2 = float(input("please enter x2: "))
y2 = float(input("please enter y2: "))
ans = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** .5
print ("the distance between those 2 points is",ans)
if response == "mid":
print ("This program will evaluate the midpoint formula using two points (x1,y1) and (x2,y2)")
x1 = float(input("what is the value of x1? "))
y1 = float(input("what is the value of y1? "))
x2 = float(input("what is the value of x2? "))
y2 = float(input("what is the value of y2? "))
x3 = (x1 + x2) / 2
x3 = round(x3, 2)
y3 = (y1 + y2) / 2
y3 = round(y3, 2)
print ("\n")
print ("the midpoint is approximately (", x3, ",", y3, ")")
if response == "root":
num = float(input("what number would you like me to find the square root of? "))
testing = 0
while testing * testing < num:
testing += .001
print (round(testing, 2))
Price $ 1.00