Gibt es eine bessere Möglichkeit, eine der Zahlen zu ändern, um in der Division zu schweben? Ich habe derzeit 1 Schleife, die meine Gleichung nach '/' scanne und zweite, die Nummer nach '/' nehmen und ändern sie in float. Wie kann ich es verbessern?Gibt es eine bessere Möglichkeit, die Zeichenfolge für die Auswertung zu ändern?
#!/usr/bin/env python2.7
import Tkinter as tk
main = tk.Tk()
main.title('Calculator')
def insert_variable(i):
"""Inserts the user defined sign and puts it on the end the entry widget"""
END = len(calc_entry.get())
calc_entry.insert(END,i)
def calculate():
''' deletes all characters in the entry and inserts evaluation of the equation '''
equation = calc_entry.get()
try:
calc_entry.delete(0, len(equation)) # deletes all characters in the entry
for i in range(len(equation)-1):# loop for checking for special signs
if equation[i] == '/': #if there is '/' sign change one of numbers to float
for j in range(i+1,len(equation)): # loop for number of digits after '/' sign
if equation[j] == '.': # if there is dot go ones more
pass
else:
try:
int(equation[j])# if there is something other than digit go to exception
except ValueError:
equation = equation[:i+1] + str(float(equation[i+1:j])) + equation[j:]# change number after/to float and join all strings
break
if equation[i] == '^': # if there is^sign change it for '**'
equation = equation[:i] +'**'+ equation[i+1:]
print equation
calc_entry.insert(0, str(round(eval(equation), 3))) # evaluates (calculates) the equation after loop
except SyntaxError:
calc_entry.insert(0,'<ERROR>')
except ZeroDivisionError:
calc_entry.insert(0,'ERROR DIVISION BY 0')
calc_entry = tk.Entry(main) # creates an entry
calc_entry.grid(row =1, columnspan = 6)
bEquate = tk.Button(main, text="=", command = calculate)
bEquate.grid(row=5, column=3)
bDivision = tk.Button(main, text="/", command = lambda : insert_variable("/"))
bDivision.grid(row=3, column=5)
main.mainloop()
Was passiert, wenn ich sqrt()
Funktion haben, die sqrt Zeichen zur letzten Nummer gibt? Wie kann ich es in die calculate()
Funktion implementieren?
sqrtChr = unichr(0x221A)
def insert_sqrt():
"""inserts sqrt sign"""
global sqrtChr
END = len(calc_entry.get())
equation = calc_entry.get() # takes the whole user's equation from the entry
for i in range(-1,-END-1,-1): # loop that goes from -1,-2,-3 to end-1
if i == -END: # if there are no exceptions like '.' or special sign, inserts sqrt character at beginning of string in the entry
calc_entry.insert(0,sqrtChr)
elif equation[i] == '.': # if there is a dot in equation go to next character in equation
pass
else:
try: # if there is a sign insert sqrt character after it and break loop
int(equation[i])
except ValueError:
calc_entry.insert((END+i+1),sqrtChr)
break
was versuchen Sie zu tun? kannst du 'split ('/')' oder sogar direkt 'eval (" 1/2 ")' benutzen (du kannst Text filtern, um nur einige Zeichen zu verwenden und du könntest kein Problem mit unsicherem Code haben). – furas
BTW: ''2^3^4'.replace ('^',' ** ')' – furas
Ich machte einen Rechner, der Gleichung wie "2 + 2/2 + 2" ergibt 5. Mit 'split()' es wird nicht funktionieren, Danke für 'ersetzen' Funktion. – ProsTedi