对不起,因为我对编程有点新意。基本上,我被分配到"分析"并且产生三角形的图像,其中用户指定两侧的长度和它们之间的角度的大小,程序运行并找到第三侧的长度以及另外两个角度(使用余弦定律)。然后我必须有所有边长度的文本输出,所有角度测量(可选),并打印出我的龟打印出来的区域"这是你的三角形\ n它的面积为x平方像素&# 34;在图像中也是如此。 此外,三角形的质心必须为(0,0)。这就是我到目前为止所做的:
import math
from turtle import*
print("This program will draw a custom triangle.")
firstside=float(input("Enter length of first side (between 10 and 400 pixels): "))
secondside=float(input("Enter length of second side (between 10 and 400 pixels): "))
includedangle=float(input("Enter the measure of the included angle in degrees (between 1 and 179): "))
print("Side lengths and angle measures:\n")
solvedthirdside=float(math.sqrt(firstside**2+secondside**2-(2*firstside*secondside*math.cos(includedangle))))
cage=Screen(); #Create screen object named cage
cage.setup(500,500) #set screen size to 500x500 pixels
另外,我也使用乌龟图片。
我真的很挣扎。一些帮助将不胜感激! 请帮忙!谢谢!
答案 0 :(得分:0)
这还没有完成,但现在应该更容易处理。
我已将其彻底考虑到功能中并进行了大量评论;它应该很容易理解。希望有所帮助!
import math
import turtle
def get_float(prompt):
"""
Prompt for input and return it as a floating-point number
"""
while True:
try:
return float(input(prompt))
except ValueError:
pass
def third_side(a, b, C):
"""
Given a triangle defined by two sides and
the angle between them (in degrees),
return the length of the third side
"""
return math.sqrt(a**2 + b**2 - 2. * a * b * math.cos(math.radians(C)))
def get_sides():
"""
Prompt for a triangle as two sides and an angle,
calculate the length of the third side,
and return the three side lengths
"""
side1 = get_float("Length of first side (pixels, in 10..400)? ")
side2 = get_float("Length of second side (pixels, in 10..400)? ")
angle = get_float("Included angle (degrees, in 1..179)? ")
return side1, side2, third_side(side1, side2, angle)
def get_angle(opp, left, right):
"""
Given three sides of a triangle,
return the angle opposite the first side (in degrees)
"""
cos_opp = (left**2 + right**2 - opp**2) / (2. * left * right)
return math.degrees(math.acos(cos_opp))
def get_angles(a, b, c):
"""
Given three sides of a triangle,
return the three angles (in degrees)
"""
return get_angle(a, b, c), get_angle(b, c, a), get_angle(c, a, b)
def main():
print("This program will draw a custom triangle.\n")
side1, side2, side3 = get_sides()
angle1, angle2, angle3 = get_angles(side1, side2, side3)
print(
"\n"
"Sides and angles:\n"
"a = {side1:>5.1f} A = {angle1:>5.1f}\n"
"b = {side2:>5.1f} B = {angle2:>5.1f}\n"
"c = {side3:>5.1f} C = {angle3:>5.1f}\n"
.format(
side1=side1, side2=side2, side3=side3,
angle1=angle1, angle2=angle2, angle3=angle3
)
)
# Your code continues here!
if __name__=="__main_":
main()