#John Bible 11/22/21
#What I learned was how to use loops containing compound conditional expressions, nested loops, and how to change loop iterations.
#What I had problems with was task 2 character art but I overcame it by watching a video.

# Does nothing, but syntactically valid
def useless_function():
pass

# Does nothing, but syntactically valid
for i in range(10):
pass

# Does nothing, but syntactically valid
if (5 < 6):
pass

# Find the index of the first occurrence of 611 in an arbitrarily long list
# Define a long list
lst = [976, 618, 812, 898, 804, 689, 611, 630, 467, 411, 648, 931, 618, 425, 328, 196, 56, 615, 458, 166, 115, 118, 22, 585, 213, 855, 615, 478, 234, 360, 981, 174, 993, 627, 911, 385, 219, 577, 540, 49, 164, 109, 893, 727, 687, 709, 971, 781, 802, 701, 789, 421, 863, 44, 573, 91, 140, 338, 36, 102, 502, 723, 134, 336, 940, 969, 649, 598, 413, 307, 929, 951, 993, 436, 149, 280, 907, 733, 713, 268, 29, 946, 102, 277, 352, 449, 527, 201, 566, 643, 118, 587, 717, 358, 542, 223, 846, 244, 83, 268]
# Find the index (without break)
index = 0
iteration_count = 0
for num in lst:
if (num == 611):
found_at = index
index = index + 1
iteration_count = iteration_count + 1
print("Without using a break, 611 was found at index:", found_at, "using", iteration_count, "iterations")
# Find the index (with break)
index = 0
iteration_count = 0
for num in lst:
if (num == 611):
found_at = index
break # adding a break to improve efficiency
index = index + 1
iteration_count = iteration_count + 1
print("Using a break, 611 was found at index:", found_at, "using", iteration_count, "iterations")

```
Without using a break, 611 was found at index: 6 using 100 iterations
Using a break, 611 was found at index: 6 using 6 iterations
```

# Prompt the user for a positive number
while True:
x = int(input("Enter a positive number: "))
if (x > 0):
break #x is positive, break the loop

# Print the odd elements in a list
lst = [14, 6, 5, 10, 6, 9, 33, 103, 21, 55]
for num in lst:
# Skip all even numbers
if (num % 2 == 0):
continue
print(num)

```
5
9
33
103
21
55
```

# [ ] The following program tests if `num` is prime or not, use a `break` statement to improve its efficiency
# and reduce the number of necessary iterations
#This will use a break statement to improve efficiency
# Compare the number of necessary iterations with and without the `break` statement
# Use the following numbers for the comparison:
num = 45345
#num = 11579
#num = 948240
#num = 128093
#num = 519937
#num = 694394
prime = True # assume num is prime unless proven otherwise
iteration_count = 0
for i in range(2, num):
if num % i == 0:
prime = False;
break
iteration_count = iteration_count + 1
# Display results
if prime:
print(num, "is prime")
else:
print(num, "is NOT prime")
print("Total number of iterations:", iteration_count)

```
45345 is NOT prime
Total number of iterations: 1
```

# [ ] Write a program to prompt the user for an odd number; use an infinite loop and a `break` statement.
#This will prompt the user for an odd number; use an infinite loop and a `break` statement.
while True:
x = int(input("Enter an odd number: "))
if (x%2!=0):
print("Thank you!")
break

```
Thank you!
```

# [ ] Modify the following program to display numbers that are divisible by 7 along with their square roots.
# HINT: Use a `continue` statement in the loop
#This will display numbers that are divisible by 7 along with their square roots
from math import sqrt
for num in range(1, 100):
if num%7!=0:
continue
print(num,"Square root:",sqrt(num))

```
7 Square root: 2.6457513110645907
14 Square root: 3.7416573867739413
21 Square root: 4.58257569495584
28 Square root: 5.291502622129181
35 Square root: 5.916079783099616
42 Square root: 6.48074069840786
49 Square root: 7.0
56 Square root: 7.483314773547883
63 Square root: 7.937253933193772
70 Square root: 8.366600265340756
77 Square root: 8.774964387392123
84 Square root: 9.16515138991168
91 Square root: 9.539392014169456
98 Square root: 9.899494936611665
```

# list of lists
table = [[5, 2, 6], [4, 6, 0], [9, 1, 8], [7, 3, 8]]
for row in table:
for col in row:
# print the value col followed by a tab
print(col, end = "\t")
# Print a new line
print()

```
5 2 6
4 6 0
9 1 8
7 3 8
```

# Generate a staircase character art
# Size controls the number of steps
def char_art(steps):
for row in range(steps):
for col in range(steps):
if(col <= row):
print("[]", end = "")
print()
# Generate a staircase with 6 steps
char_art(6)

```
[]
[][]
[][][]
[][][][]
[][][][][]
[][][][][][]
```

# [ ] Write a program to display the sum of each row in the table
#This will display the sum of each row in the table
table = [[5, 2, 6], [4, 6, 0], [9, 1, 8], [7, 3, 8]]
for row in table:
x=sum(row)
print(row,x)
# Print a new line
print()

```
[5, 2, 6] 13
[4, 6, 0] 10
[9, 1, 8] 18
[7, 3, 8] 18
```

# [ ] Complete the function `generate_star` so it displays a star of variable `size` using "*"
# For size = 5 the star should look like:
# * *
# * *
# *
# * *
# * *
#This will display a star of variable `size` using "*"
def generate_star(size):
x=0
y=4
for row in range(size):
for col in range(size):
if row==x and col==y:
print("*",end="")
x=x+1
y=y-1
elif row==col:
print("*",end="")
else:
print(end=" ")
print()
# Display star
generate_star(5)

```
* *
* *
*
* *
* *
```

lst = [102, 34, 55, 166, 20, 67, 305]
# Nesting a compound conditional in a for loop
largest = 0
for num in lst:
# test if num is even and greater than largest
if((largest < num) and (num % 2 == 0)):
largest = num
print("Largest even number in the list is: ", largest)

```
Largest even number in the list is: 166
```

# Ages of 100 people
ages = [86, 38, 30, 19, 29, 6, 95, 22, 23, 82, 39, 73, 30, 98, 5, 68, 57, 34, 35, 81, 54, 77, 29, 75, 83, 14, 88, 7, 8, 32, 93, 76, 42, 1, 32, 70, 70, 3, 34, 52, 44, 41, 7, 77, 73, 97, 34, 13, 33, 54, 8, 82, 21, 55, 72, 41, 34, 98, 72, 73, 24, 55, 50, 63, 38, 92, 43, 68, 52, 68, 69, 51, 19, 24, 35, 55, 74, 47, 8, 19, 69, 12, 96, 96, 11, 30, 97, 73, 22, 25, 19, 85, 37, 68, 39, 76, 73, 18, 45, 42]
# Initial count
children = 0
teens = 0
young_adults = 0
adults = 0
# Nesting compound conditionals within a for loop
for age in ages:
if(age <= 12):
children = children + 1
elif((age >= 13) and (age <= 19)):
teens = teens + 1
elif((age >= 20) and (age <= 30)):
young_adults = young_adults + 1
elif(age > 30):
adults = adults + 1
print("Number of children: ", children)
print("Number of teens: ", teens)
print("Number of young_adults: ", young_adults)
print("Number of adults: ", adults)

```
Number of children: 11
Number of teens: 7
Number of young_adults: 12
Number of adults: 70
```

# Prompt the user for a number between 50 and 60
# Using infinite loop and break
while True:
x = int(input("Enter a number between 50 and 60: "))
if ((x >= 50) and (x <= 60)):
print("Thank you!")
break

```
Thank you!
```

# [ ] Complete the following program to count the number of even positive numbers, odd negative numbers, and zeros in `lst`
# then print the results
#This will count the number of even positive numbers, odd negative numbers, and zeros in `lst` and then print the results
lst = [9, 0, -2, -4, -5, 2, -15, 6, -65, -7]
even_positives = 0
odd_negatives = 0
zeros = 0
#TODO: Count even_positives, odd_negatives, and zeros
for x in lst:
if (x>0) and (x%2!=1):
even_positives= even_positives+1
elif (x<0) and (x%2!=0):
odd_negatives=odd_negatives+1
elif x==0:
zeros=zeros+1
print("Number of even positives is",even_positives)
print("Number of odd negatives is",odd_negatives)
print("Number of zeros is",zeros)

```
Number of even positives is 2
Number of odd negatives is 4
Number of zeros is 1
```

# [ ] Write a program to count the number of punctuation marks (. , ? ! ' " : ;) in `s`
#This will count the number of punctuation marks (. , ? ! ' " : ;) in `s`
s = "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth." # Sherlock Holmes (by Sir Arthur Conan Doyle, 1859-1930)
p=0
for x in s:
if (x==".") or (x==","):
p= p+1
elif (x=="?") or (x=="!"):
p=p+1
elif (x=="'") or (x=='"'):
p=p+1
elif (x==":") or (x==";"):
p=p+1
print("Amount of times punctuation was used is",p,"times.")

```
Amount of times punctuation was used is 4 times.
```

# [ ] Write a program to prompt the user for an odd positive number; use an infinite loop and a `break` statement.
#This will prompt the user for an odd positive number until they give one using an infinite loop and a break statement
while True:
x = int(input("Enter an odd positive number: "))
if ((x >=0) and (x%2!=0)):
print("Thank you!")
break

```
Thank you!
```