Как вывести "Error" при делении на 0 в калькуляторе?
Использую библиотеку tkinter, в консольном калькуляторе знаю как выводить нужный текст при делении на 0, а здесь не могу понять что нужно написать, что-бы в окне ввода писало "Error!"
from tkinter import *
window = Tk()
window.title('Калькулятор')
window.resizable(width=False, height=False)
window.geometry('270x260+200+200')
entry = Entry(window, bg='gray', fg='white', width=19, font=('Time new romance', 18))
entry.place(x=10,y=10 )
def input_into_entry(sumbol):
entry.insert(END, sumbol)
def clear():
entry.delete(0, END)
def count_result():
text = entry.get()
result=0
if "+" in text:
split_text = text.split("+")
first = split_text[0]
second = split_text[1]
result = float(first) + float(second)
if "-" in text:
split_text = text.split("-")
first = split_text[0]
second = split_text[1]
result = float(first) - float(second)
if "*" in text:
split_text = text.split("*")
first = split_text[0]
second = split_text[1]
result = float(first) * float(second)
if "/" in text:
split_text = text.split("/")
first = split_text[0]
second = split_text[1]
result = float(first) / float(second)
clear()
entry.insert(0, result)
Ответы (2 шт):
Автор решения: Вова Ханов
→ Ссылка
Если я правильно Вас понял, вам нужно вместо ошибки выводить "Error!". Воспользуйтесь try:
try:
result = float(first) / float(second)
except:
print("Error!")
Автор решения: krabnerab
→ Ссылка
оберни в try-except
try:
...
except ZeroDivisionError:
clear()
entry.insert(0, "Деление на 0!")
вот тебе еще "не простенький" калькулятор
from tkinter import *
import math
window = Tk()
window.title('Калькулятор')
window.resizable(width=False, height=False)
window.geometry('700x600')
entry = Entry(window, bg='gray', fg='white', width=20, font=('Arial', 24), justify='right')
entry.place(x=20, y=20, width=650, height=70)
def input_into_entry(symbol):
entry.insert(END, symbol)
def clear():
entry.delete(0, END)
def count_result():
text = entry.get()
try:
result = eval(text)
clear()
entry.insert(0, str(result))
except ZeroDivisionError:
clear()
entry.insert(0, "Деление на 0!")
except SyntaxError:
clear()
entry.insert(0, "Ошибка ввода!")
except:
clear()
entry.insert(0, "Ошибка!")
buttons = [
'C', '(', ')', '/', 'sin', 'cos',
'7', '8', '9', '*', 'sqrt', 'log',
'4', '5', '6', '-', 'pi', 'e',
'1', '2', '3', '+', '^', 'x!',
'0', '.', '=', '', '', ''
]
x = 20
y = 120
button_width = 100
button_height = 60
for button in buttons:
if button == '=':
Button(window, text=button, command=count_result, font=('Arial', 18), bg='orange').place(
x=x, y=y, width=button_width, height=button_height)
elif button == 'C':
Button(window, text=button, command=clear, font=('Arial', 18), bg='lightgray').place(
x=x, y=y, width=button_width, height=button_height)
elif button == '^':
Button(window, text=button, command=lambda b='**': input_into_entry(b), font=('Arial', 18)).place(
x=x, y=y, width=button_width, height=button_height)
elif button == 'sin':
Button(window, text=button, command=lambda: input_into_entry('math.sin('), font=('Arial', 16)).place(
x=x, y=y, width=button_width, height=button_height)
elif button == 'cos':
Button(window, text=button, command=lambda: input_into_entry('math.cos('), font=('Arial', 16)).place(
x=x, y=y, width=button_width, height=button_height)
elif button == 'sqrt':
Button(window, text=button, command=lambda: input_into_entry('math.sqrt('), font=('Arial', 16)).place(
x=x, y=y, width=button_width, height=button_height)
elif button == 'log':
Button(window, text=button, command=lambda: input_into_entry('math.log('), font=('Arial', 16)).place(
x=x, y=y, width=button_width, height=button_height)
elif button == 'pi':
Button(window, text=button, command=lambda: input_into_entry('math.pi'), font=('Arial', 16)).place(
x=x, y=y, width=button_width, height=button_height)
elif button == 'e':
Button(window, text=button, command=lambda: input_into_entry('math.e'), font=('Arial', 16)).place(
x=x, y=y, width=button_width, height=button_height)
elif button == 'x!':
Button(window, text=button, command=lambda: input_into_entry('math.factorial('), font=('Arial', 16)).place(
x=x, y=y, width=button_width, height=button_height)
elif button:
Button(window, text=button, command=lambda b=button: input_into_entry(b), font=('Arial', 18)).place(
x=x, y=y, width=button_width, height=button_height)
x += 110
if x > 600:
x = 20
y += 70
window.mainloop()