-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnames.py
More file actions
38 lines (26 loc) · 894 Bytes
/
names.py
File metadata and controls
38 lines (26 loc) · 894 Bytes
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
from generals import is_valid_number, text_to_number
def main():
while True:
n = input("What's range? ")
if not is_valid_number(n):
print("Error: please enter a valid number.")
continue
n = text_to_number(n)
if n <= 0:
print("Error: number must be greater than 0.")
continue
break
# Write names to file
with open("names.txt", "a") as file: #Use "w" if you want to overwrite
for _ in range(n):
name = input("What's your name? ")
file.write(name + "\n")
file.close()
# Read names from file
with open("names.txt", "r") as file:
names = [line.strip() for line in file if line.strip()]
for name in sorted(names):
print(f"hello, {name}")
print(f"The full list is: {names}")
if __name__ == "__main__":
main()