0% found this document useful (0 votes)
7 views

Program 3

Uploaded by

kavyadlncsahai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Program 3

Uploaded by

kavyadlncsahai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Thinking Process

1. Input Handling:
– Start by prompting the user to input values for a, b, and c.
– These values will be taken as input and can be assumed to be numeric
(integers or floats).
2. Comparison Logic:
– To determine which of the three values is the greatest, you can use
conditional statements (if-elif-else).
– Compare a with b and c to determine if a is the greatest.
– If a is not the greatest, compare b with c to check if b is the greatest.
– If neither a nor b is the greatest, then c must be the greatest.
3. Output:
– After determining the greatest value, print the result.

Python Code Implementation


Here’s how the program might look:
# Step 1: Input Handling
a = float(input("Enter the value of a: "))
b = float(input("Enter the value of b: "))
c = float(input("Enter the value of c: "))

# Step 2: Comparison Logic


if a >= b and a >= c:
greatest = a
elif b >= a and b >= c:
greatest = b
else:
greatest = c

# Step 3: Output the greatest value


print(f"The greatest value among a, b, and c is: {greatest}")

Explanation
• Input Handling: The input() function is used to take inputs from the user, and
float() is used to convert these inputs to numeric values that can handle both
integers and decimals.
• Comparison Logic: The if-elif-else structure allows for a straightforward
comparison among the three values.
• Output: The print() function outputs the result, showing the greatest value.
The code is functional and follows a straightforward logic for finding the greatest of three
values. However, there are some potential improvements that could be made:
1. Error Handling:
• Add input validation to ensure that the user enters valid numbers. If invalid input is
provided, the program should handle it gracefully.

2. Code Readability:
• Although your code is already readable, adding comments to explain the purpose of
each block can be beneficial, especially for more complex code or for those reading
it for the first time.

3. Using Built-in Functions:


• Instead of using conditional statements to determine the greatest number, you
could use Python’s built-in max() function to simplify the code.

4. Variable Naming:
• Consider using more descriptive variable names to improve clarity, especially if the
variables represent something specific in the context of your application.

5. User Experience:
• Adding more descriptive prompts can help users understand what the program is
expecting as input.

6. Code Structure:
• Wrapping the logic in a function could make the code more modular and reusable.
Here’s a revised version incorporating these suggestions:
def get_number_input(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Invalid input. Please enter a numeric value.")

# Step 1: Input Handling with Error Checking


a = get_number_input("Enter the value of the first number (a): ")
b = get_number_input("Enter the value of the second number (b): ")
c = get_number_input("Enter the value of the third number (c): ")

# Step 2: Determine the greatest value using max() function


greatest = max(a, b, c)

# Step 3: Output the greatest value


print(f"The greatest value among the numbers you entered is:
{greatest}")

Key Improvements:
• Error Handling: Added a get_number_input function that checks for valid input
and prompts the user again if invalid input is provided.
• Using max(): Simplified the logic for finding the greatest value using the built-in
max() function.
• Functionality: Encapsulated the input handling in a function to make the code
cleaner and more modular.
You can create a simple graphical user interface (GUI) for this program using the tkinter
library in Python. Below is a version of your code with a basic GUI implementation.
import tkinter as tk
from tkinter import messagebox

def get_number_input(entry):
try:
return float(entry.get())
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a numeric
value.")
return None

def find_greatest():
a = get_number_input(entry_a)
b = get_number_input(entry_b)
c = get_number_input(entry_c)

if a is not None and b is not None and c is not None:


greatest = max(a, b, c)
messagebox.showinfo("Result", f"The greatest value among the
numbers you entered is: {greatest}")

# Set up the main window


root = tk.Tk()
root.title("Find the Greatest Number")

# Create and place labels and entries for inputs


tk.Label(root, text="Enter the value of the first number
(a):").grid(row=0, column=0, padx=10, pady=5)
entry_a = tk.Entry(root)
entry_a.grid(row=0, column=1, padx=10, pady=5)

tk.Label(root, text="Enter the value of the second number


(b):").grid(row=1, column=0, padx=10, pady=5)
entry_b = tk.Entry(root)
entry_b.grid(row=1, column=1, padx=10, pady=5)

tk.Label(root, text="Enter the value of the third number


(c):").grid(row=2, column=0, padx=10, pady=5)
entry_c = tk.Entry(root)
entry_c.grid(row=2, column=1, padx=10, pady=5)
# Create and place the button to calculate the greatest number
tk.Button(root, text="Find Greatest",
command=find_greatest).grid(row=3, columnspan=2, pady=10)

# Start the GUI event loop


root.mainloop()

How it works:
1. Input Fields: Three entry fields are created for the user to input values for a, b, and
c.
2. Error Handling: When the user clicks the “Find Greatest” button, the program tries
to convert the input values to floats. If the conversion fails, an error message is
displayed.
3. Find Greatest: If all inputs are valid, the program uses the max() function to find
the greatest number and displays the result in a message box.
I think so, these changes should make the code better. But, here the thing the GUI is very
basic, now let’s improve it a bit To create a more modern and sophisticated user interface,
we can use the tkinter library with the ttkbootstrap extension, which offers modern
themes and widgets similar to those found in contemporary applications. Below is an
example of how to enhance the UI using ttkbootstrap.
First, you’ll need to install ttkbootstrap:
pip install ttkbootstrap

Here’s the updated code with a modern UI:


import tkinter as tk
from tkinter import messagebox
from ttkbootstrap import Style

def get_number_input(entry):
try:
return float(entry.get())
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a numeric
value.")
return None

def find_greatest():
a = get_number_input(entry_a)
b = get_number_input(entry_b)
c = get_number_input(entry_c)

if a is not None and b is not None and c is not None:


greatest = max(a, b, c)
messagebox.showinfo("Result", f"The greatest value among the
numbers you entered is: {greatest}")
def clear_inputs():
entry_a.delete(0, tk.END)
entry_b.delete(0, tk.END)
entry_c.delete(0, tk.END)

# Set up the main window


style = Style(theme="cosmo") # You can change the theme to
"superhero", "darkly", etc.
root = style.master
root.title("Find the Greatest Number")
root.geometry("400x250")
root.resizable(False, False)

# Create and place labels and entries for inputs


ttk.Label(root, text="Enter the value of the first number
(a):").grid(row=0, column=0, padx=10, pady=10, sticky="W")
entry_a = ttk.Entry(root, width=30)
entry_a.grid(row=0, column=1, padx=10, pady=10)

ttk.Label(root, text="Enter the value of the second number


(b):").grid(row=1, column=0, padx=10, pady=10, sticky="W")
entry_b = ttk.Entry(root, width=30)
entry_b.grid(row=1, column=1, padx=10, pady=10)

ttk.Label(root, text="Enter the value of the third number


(c):").grid(row=2, column=0, padx=10, pady=10, sticky="W")
entry_c = ttk.Entry(root, width=30)
entry_c.grid(row=2, column=1, padx=10, pady=10)

# Create and place the button to calculate the greatest number


button_frame = ttk.Frame(root)
button_frame.grid(row=3, columnspan=2, pady=20)

find_button = ttk.Button(button_frame, text="Find Greatest",


command=find_greatest, bootstyle="primary")
find_button.grid(row=0, column=0, padx=10)

clear_button = ttk.Button(button_frame, text="Clear",


command=clear_inputs, bootstyle="secondary")
clear_button.grid(row=0, column=1, padx=10)

# Start the GUI event loop


root.mainloop()

Features of the Modern UI:


1. ttkbootstrap Theme: The interface uses ttkbootstrap, which provides modern
themes (cosmo, superhero, darkly, etc.) that give the application a contemporary
look and feel.
2. Enhanced Widgets: The buttons and other widgets have a modern design, with
support for different styles (e.g., primary, secondary).
3. Improved Layout: The interface is clean, with consistent padding and spacing,
making it more user-friendly.

You might also like