Question: Create a program that will draw the angle bisectors of a triangle drawn by the user in three different colors.
Solution:
Import mathfrom graphics import *
win=GraphWin("Area of Rectangle", 1000, 700)
win.setBackground("white")
p1=win.getMouse()
p1.draw(win)
p2=win.getMouse()
p2.draw(win)
rect=Rectangle(p1,p2)
rect.draw(win)
p1x=p1.getX()
p1y=p1.getY()
p2x=p2.getX()
p2y=p2.getY()
length = abs(p2x-p1x)
height=abs(p2y-p1y)
area = length * height
message = Text(Point(500,30), "The area of the rectangle is "+str(area)+" pixels. Click anywhere to close.")
message.draw(win)
win.getMouse()
win.close()
The following program finds the sum of the first n integers, where n is specified by the user:
def main():
n=eval(input("number?"))
for i in range(n+1):
Sum=Sum+i
print(Sum)
If run, this program would produce an error. What line of code is missing from the program?
Answer:
The line [Sum=0] should appear before the for loop. It initializes the variable Sum so that the for loop has somewhere to start on its first iteration.
Write a program that prints the following:
*
**
***
****
*****
******
def main():
for i in range(6):
for m in range(i):
print("*", end="")
print()
Extra Loopy
Using loops and two print statements, have Python print out the following square:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Answer:
def star(n):
for star in range(n):
print("*",end=" ")
def main():
count=eval(input("What is the size of the square?"))
for i in range(count):
star(count)
print()
Write a program to draw a house (triangle above a square) based on user input.
from graphiccs import *
win = GraphWin ("Click five points in the shape of a house starting with the top and going clockwise" )
def main( )
p1=win.getMouse( )
p1.draw(win)
p2=win.getMouse( )
p2.draw(win)
p3=win.getMouse( )
p3.draw(win)
p4=win.getMouse( )
p4.draw(win)
p5=win.getMouse( )
p5.draw(win)
triangle = Polygon(p1, p2, p5)
triangle.draw(win)
square = Rectangle(p2, p4)
square.draw(win)
Write a program with two print statements to yeild the following:
***
**
*
*
**
***
Answer:
import math
for i in range (-3, 4):
a = int(math.fabs(i))
for j in range (0,a)
print("*", end = "")
print()
Questions:
Give the results of the following two questions and explain the difference between 4 and "four".
4 * 3 =
"four" * 3 =
Answer:
4 * 3 = 12
"four" * 3 = 'fourfourfour'
4 is a number and is represented by int in Python
"four" is a text and is called string in Python
A certain CS Professor gives 5-point quizzes that are graded on the scale 5-A, 4-B, 3-C, 2-D, 1-F, 0-F. Write a program that accepts a quiz score as an input and prints out the corresponding grade.
def main():
grades = ["F","F", "D", "C", "B", "A"]
grade=eval(input("enter score on quiz"))
print("my grade is", grades[grade])
Harder and Similar:
A certain CS professor gives 100-point exams that are graded on the scale 90-100:A, 80-89:B, 70-79:C, 60-69:D, <60:f. Write a program that accepts an exam score as input and prints out the corresponding grade.
def main():
grades = ["F","F","F","F","F","F", "D", "C", "B", "A"]
n=eval(input("enter score on exam"))
grade=(n//10)
print("my grade is", grades[grade])
How can you make a square using a numeric value entered by a user, while also telling the area of that given square?
def main():
count = eval(input("Size of Square? "))
print()
area(count)
print()
for i in range(count):
star(count)
print()
def star(anyNumber):
for i in range(anyNumber):
print("*", end=" ")
def area(count):
area=count**2
print("Area of Square =", area)
main()
Q: Make a program to calculate the cost per square inch of a circular pizza.
A: import math
def Diameter():
d = eval(input("Diameter of the pizza (in inches):"))
return d
def Price():
p = eval(input("Price of pizza:"))
return p
def Area(d):
r = d/2
a = math.pi*r**2
return a
def main():
d = Diameter()
p = Price()
a = Area(d)
z = p/a
print(z, "is the cost per square inch of a", d, "inch pizza")
# Have the user input the side-lengths for a square and have Python draw the square in blue and inscribe a red circle in the square that touches all four sides.
from graphics import*
def main():
Side=eval(input("Please enter the length of one side of the square:"))
win=GraphWin()
win.setCoords(-10, -10, 10, 10)
Square=Rectangle(Point(-Side/2, Side/2), Point(Side/2, -Side/2))
Square.setFill("blue")
Square.draw(win)
Circ=Circle(Point(0,0), Side/2)
Circ.setFill("red")
Circ.draw(win)
input("Press Enter to close window")
win.close()
Question
Create a pythageorian calculator. Ask user to input two sides of the traingle and it should output the hypotanous. Use only three lines of code.
Answer- Trick is not to import math and use the exponential of .5
side1=eval(input("what's side 1"))
side2=eval(input("what's side 2"))
print ((side1**2+side2**2)**.5)
Factor
In a math class, the teacher wants to know all the factors (excluding 1 and the number itself) of a given number (the number is less than 100). Your job is to make a Python program to help him find all the factors when given a specific number. Your output should include the number of factors as well as the factors.
def main():
a=eval(input("Please enter a number (less than 100):"))
sum=0
print("the factors are:")
for i in range(2,a):
if a%i==0:
print(i,end=" ")
sum=sum+1
print(end="\n")
print("the number of factors is:", sum)
main()