-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
79 lines (64 loc) · 1.99 KB
/
Copy pathproject.py
File metadata and controls
79 lines (64 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def greet(name: str) -> str:
"""
Returns a friendly greeting based on the user's name.
If the name is empty or contains only spaces, a generic greeting is returned.
"""
name = str(name).strip()
if not name:
return "Hello!"
return f"Hello, {name}! Nice to meet you."
def add(a, b):
"""
Adds two values and returns the result.
Works with numbers and strings, depending on user input.
"""
return a + b
def analyze_list(values):
"""
Analyzes a list and returns:
- total number of items
- number of unique items
- whether the list contains a None value
If input is not iterable, it will be treated as a single-element list.
"""
if values is None:
seq = []
else:
try:
seq = list(values)
except TypeError:
seq = [values]
return {
"count": len(seq),
"unique": len(set(seq)),
"has_none": any(x is None for x in seq),
}
def main():
"""
A small interactive demo that runs when the program is executed.
Demonstrates greeting, addition, and list analysis based on user input.
"""
print("=== Simple Interactive Program ===")
# Greeting section
name = input("Enter your name: ").strip()
print(greet(name))
print("\n--- Addition Example ---")
# Addition section
try:
x = float(input("Enter first number: "))
y = float(input("Enter second number: "))
print("Result:", add(x, y))
except ValueError:
print("Invalid number! Skipping addition.")
print("\n--- List Analysis ---")
raw = input("Enter values separated by commas: ").strip()
if raw:
items = [
item.strip() if item.strip() != "None" else None for item in raw.split(",")
]
else:
items = []
result = analyze_list(items)
print("List summary:", result)
if __name__ == "__main__":
main()