Syntax Check fast gut
Verfasst: Donnerstag 4. April 2024, 20:26
Hallo alle zusammen,
habe letzte Woche dieses calculator Mini Programm geschrieben.. läuft soweit alles gut calculator geht gut nur die Syntax controlle muckt herum wenn ich
Eingebe. Endergebnis der Kalkulation stimmt , aber etwas fehlt nur was?
Hier mein Code example
Über Hilfe freue ich mich danke Gruß liana
habe letzte Woche dieses calculator Mini Programm geschrieben.. läuft soweit alles gut calculator geht gut nur die Syntax controlle muckt herum wenn ich
Code: Alles auswählen
S=" hello " + Str(c)
Eingebe. Endergebnis der Kalkulation stimmt , aber etwas fehlt nur was?
Hier mein Code example
Code: Alles auswählen
# a=10 : b=20 : c=30 : s="Hello" # #GO and valid syntax
# a=10 : b=20 : c=30 : s="Hello" : s=c+b ' #GO but invalid syntax
# a=10 : b=20 : c=30 : s="Hello " + str(c) #GO but syntax not valid
import tkinter as tk
from tkinter import filedialog
variables = {} # Initialize variables dictionary
def calculate():
line = entry.get()
statements = line.split(":")
for statement in statements:
parts = statement.split("=")
var_name = parts[0].strip()
var_value = eval(parts[1].strip(), variables.copy())
variables[var_name] = var_value
output_text.delete(1.0, tk.END) # Clear previous output
output_text.insert(tk.END, variables.get('s', 'Invalid input'))
def syntax_check():
line = entry.get()
statements = line.split(":")
valid_syntax = True
for statement in statements:
parts = statement.split("=")
var_name = parts[0].strip()
# Check if it's a string assignment
if len(parts) == 2 and parts[1].strip().startswith('"') and parts[1].strip().endswith('"'):
continue
# Check if it's a string assignment with concatenation of a variable value
if len(parts) == 2 and '+' in parts[1].strip():
operands = parts[1].strip().split('+')
for operand in operands:
operand = operand.strip()
if operand.startswith('"') and operand.endswith('"'):
continue
elif operand in variables:
if not isinstance(variables[operand], str):
variables[operand] = str(variables[operand]) # Convert to string
continue
else:
valid_syntax = False
break
if not valid_syntax:
break
# Check if it's a numerical assignment with multiplication of two variables (a*b)
if len(parts) == 2 and '*' in parts[1].strip():
operands = parts[1].strip().split('*')
for operand in operands:
operand = operand.strip()
if not operand.isdigit() and not operand.replace('.', '', 1).isdigit():
if operand not in variables:
valid_syntax = False
break
if not valid_syntax:
break
if valid_syntax:
output_text.delete(1.0, tk.END)
output_text.insert(tk.END, "Syntax is valid")
else:
output_text.delete(1.0, tk.END)
output_text.insert(tk.END, "Invalid syntax")
def clear_input():
entry.delete(0, tk.END)
def load_file():
file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt"), ("Python files", "*.py")])
if file_path:
with open(file_path, 'r') as file:
input_text = file.read()
entry.delete(0, tk.END)
entry.insert(tk.END, input_text)
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt"), ("Python files", "*.py")])
if file_path:
with open(file_path, 'w') as file:
file.write(entry.get())
# Create Tkinter window
window = tk.Tk()
window.title("Expression Calculator")
# Input box with yellow background
entry = tk.Entry(window, width=50, bg="yellow")
entry.pack(pady=10)
# Syntax Check Button
syntax_check_button = tk.Button(window, text="Syntax Check", command=syntax_check)
syntax_check_button.pack()
# Calculate Button
calculate_button = tk.Button(window, text="Calculate", command=calculate)
calculate_button.pack()
# Clear Input Button
clear_input_button = tk.Button(window, text="Clear Input", command=clear_input)
clear_input_button.pack()
# Load File Button
load_file_button = tk.Button(window, text="Load File", command=load_file)
load_file_button.pack()
# Save File Button
save_file_button = tk.Button(window, text="Save File", command=save_file)
save_file_button.pack()
# Output box
output_text = tk.Text(window, height=5, width=50)
output_text.pack(pady=10)
window.mainloop()