-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoops.py
More file actions
32 lines (28 loc) · 1.08 KB
/
Loops.py
File metadata and controls
32 lines (28 loc) · 1.08 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
#Creating different functions to make it more modular
def generate_list(length):
"""
This function runs the loop according to the given length
Creating a list of numbers based on the user given length
"""
#turns out, you can do the logic inside the brackets when creating the list/array
return [i+1 for i in range(length)]
def get_valid_input(prompt):
"""
Function to validate the input, in case user tries to put in a letter
"""
while True:
try:
#return statement here breaks the while True loop
return int(input(prompt))
except ValueError:
print("Invalid Input. Please enter a valid integer")
def Loop():
"""
This function Generates a list of numbers based on user input and prints out the result.
"""
#You can also ask for a prompt while implementing a function
loopLen = get_valid_input("Please enter how many numbers you want to add to the list: \n")
result_list = generate_list(loopLen)
print("Done! This is what your list looks like:\n")
print(result_list)
Loop()