import math a=input("please input a:\n") b=input("please input b:\n") c=input("please input c:\n") #*********begin******** #********end********* else: print("this is not triangle")请补充程序。输入三角形的3条边,判断是否组成三角形。若能,计算三角形面积并输出。
时间: 2023-08-18 14:09:44 浏览: 137
好的,以下是 Python 代码实现:
```python
import math
a = float(input("please input a:\n"))
b = float(input("please input b:\n"))
c = float(input("please input c:\n"))
# 判断是否为三角形
if a + b > c and a + c > b and b + c > a:
# 计算半周长
p = (a + b + c) / 2
# 计算面积
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
print("This is a triangle, and the area of the triangle is:", area)
else:
print("This is not a triangle")
```
这段代码会根据用户输入的三条边长,判断是否为三角形,如果是三角形则计算并输出三角形的面积,如果不是则输出提示信息。
相关问题
import math a=eval(input("")) b=eval(input("")) c=eval(input("")) s=(a+b+c)/2 ar=sqrt(s*(s-a)*(s-b)*(s-c)) print("%.2f",ar)
As an AI language model, I do not have a physical form or personal beliefs, opinions, or emotions. Therefore, I cannot provide any information or response to the term "null" as it can have different meanings in various contexts. Can you please provide more context or a specific question to assist me better?
给下面的代码加上计算器窗口界面,要有显示器和键盘,并分析出生成后的代码信息处理流程: import math class Calculator: def init(self): self.result = 0 def add(self, num): self.result += num def sub(self, num): self.result -= num def mul(self, num): self.result *= num def div(self, num): if num == 0: print("Error: Cannot divide by zero!") else: self.result /= num def getResult(self): return self.result def clear(self): self.result = 0 def main(): calculator = Calculator() while True: print("Please select an operation:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") print("5. Clear") print("0. Exit") choice = int(input("Enter your choice: ")) if choice == 0: break elif choice == 1: num = float(input("Enter a number: ")) calculator.add(num) print("Result:", calculator.getResult()) elif choice == 2: num = float(input("Enter a number: ")) calculator.sub(num) print("Result:", calculator.getResult()) elif choice == 3: num = float(input("Enter a number: ")) calculator.mul(num) print("Result:", calculator.getResult()) elif choice == 4: num = float(input("Enter a number: ")) calculator.div(num) print("Result:", calculator.getResult()) elif choice == 5: calculator.clear() print("Result:", calculator.getResult()) else: print("Invalid choice. Please try again.") if name == 'main': main()
以下是加上计算器窗口界面后的代码,包括显示器和键盘界面的设计:
```python
import math
import tkinter as tk
class Calculator:
def __init__(self):
self.result = 0
def add(self, num):
self.result += num
def sub(self, num):
self.result -= num
def mul(self, num):
self.result *= num
def div(self, num):
if num == 0:
self.result = "Error: Cannot divide by zero!"
else:
self.result /= num
def getResult(self):
return self.result
def clear(self):
self.result = 0
class CalculatorGUI:
def __init__(self, master):
self.master = master
master.title("Calculator")
self.result_label = tk.Label(master, text="0", width=20, font=("Arial", 20))
self.result_label.grid(row=0, column=0, columnspan=4)
self.create_buttons()
self.calculator = Calculator()
def create_buttons(self):
self.button1 = tk.Button(self.master, text="1", width=5, height=2, command=lambda: self.button_click(1))
self.button2 = tk.Button(self.master, text="2", width=5, height=2, command=lambda: self.button_click(2))
self.button3 = tk.Button(self.master, text="3", width=5, height=2, command=lambda: self.button_click(3))
self.button4 = tk.Button(self.master, text="4", width=5, height=2, command=lambda: self.button_click(4))
self.button5 = tk.Button(self.master, text="5", width=5, height=2, command=lambda: self.button_click(5))
self.button6 = tk.Button(self.master, text="6", width=5, height=2, command=lambda: self.button_click(6))
self.button7 = tk.Button(self.master, text="7", width=5, height=2, command=lambda: self.button_click(7))
self.button8 = tk.Button(self.master, text="8", width=5, height=2, command=lambda: self.button_click(8))
self.button9 = tk.Button(self.master, text="9", width=5, height=2, command=lambda: self.button_click(9))
self.button0 = tk.Button(self.master, text="0", width=5, height=2, command=lambda: self.button_click(0))
self.button_add = tk.Button(self.master, text="+", width=5, height=2, command=lambda: self.operation_click("+"))
self.button_subtract = tk.Button(self.master, text="-", width=5, height=2, command=lambda: self.operation_click("-"))
self.button_multiply = tk.Button(self.master, text="*", width=5, height=2, command=lambda: self.operation_click("*"))
self.button_divide = tk.Button(self.master, text="/", width=5, height=2, command=lambda: self.operation_click("/"))
self.button_clear = tk.Button(self.master, text="C", width=5, height=2, command=lambda: self.clear_click())
self.button_equals = tk.Button(self.master, text="=", width=5, height=2, command=lambda: self.equals_click())
self.button1.grid(row=3, column=0)
self.button2.grid(row=3, column=1)
self.button3.grid(row=3, column=2)
self.button_add.grid(row=3, column=3)
self.button4.grid(row=2, column=0)
self.button5.grid(row=2, column=1)
self.button6.grid(row=2, column=2)
self.button_subtract.grid(row=2, column=3)
self.button7.grid(row=1, column=0)
self.button8.grid(row=1, column=1)
self.button9.grid(row=1, column=2)
self.button_multiply.grid(row=1, column=3)
self.button_clear.grid(row=4, column=0)
self.button0.grid(row=4, column=1)
self.button_equals.grid(row=4, column=2)
self.button_divide.grid(row=4, column=3)
def button_click(self, number):
current = self.result_label.cget("text")
if current == "0":
self.result_label.config(text=str(number))
else:
self.result_label.config(text=current + str(number))
def operation_click(self, operation):
current = self.result_label.cget("text")
self.calculator.result = float(current)
self.operation = operation
self.result_label.config(text="0")
def clear_click(self):
self.calculator.clear()
self.result_label.config(text="0")
def equals_click(self):
current = self.result_label.cget("text")
self.calculator.result = float(current)
if self.operation == "+":
self.calculator.add(self.calculator.getResult())
elif self.operation == "-":
self.calculator.sub(self.calculator.getResult())
elif self.operation == "*":
self.calculator.mul(self.calculator.getResult())
elif self.operation == "/":
self.calculator.div(self.calculator.getResult())
self.result_label.config(text=str(self.calculator.getResult()))
if __name__ == '__main__':
root = tk.Tk()
calculator_gui = CalculatorGUI(root)
root.mainloop()
```
这个代码为用户提供了一个计算器界面,包括数字键、运算符键、清除键、等于键和显示器。当用户点击数字键时,数字将添加到显示器上。当用户点击运算符键时,当前数字将被存储并清除显示器。当用户点击等于键时,计算器将执行相应的操作并将结果显示在显示器上。如果用户点击清除键,那么计算器的结果将被重置为零。整个程序的控制流程是由按钮的事件处理程序控制的,这些事件处理程序与 Calculator 类中定义的方法相对应。
阅读全文