<jupyter_start><jupyter_text>**Loops / Repetition / Iteration**repeat a fixed pattern many times2 types of loops in Python*   for (fixed number of times)*   while (variable number of times, 0 or more)<jupyter_code># for (range)
for i in range(10): # loop condition
  print("Good morning!") # loop body
# for (print 1 to n)
for i in range(0, 5): # range(5), if i < 5
  print(i)
  # i auto increment by 1
# for (sum 1 to n) i.e. 1 + 2 + 3 + ... + n
total = 0
n = 100
for i in range(1, n+1):
  # total = total + i # 0 + 1, 1 + 2, 3 + 3, ...
  total += i 
print(total)
# for (iterable)
# iterate over string

# for (with step)
# all odd numbers between 1 to 10 inclusive
#for i in range(1, 11):
#  if i % 2 == 1: # odd
#    print(i, end=' ')

for i in range(1, 11, 2):
  print(i, end=' ')
# for (count down)
for i in range(9, 0, -2):
  print(i, end=' ')
# while (print 1 to n)
i = 1 # initialise
n = 10
while i <= n: # loop condition
  print(i, end=' ') # loop body
  i = i + 1 # update loop control
  # i += 1 
# while (sum 1 to n)
i = 1
n = 10
total = 0
while i <= n:
  total = total + i # total += i
  i = i + 1         # i += 1
print(total)
# while (guessing game)
import random

# initialise
lucky = random.randint(1,100)
print(lucky)
running = True

# loop until guess is correct
while running:
  # get guess
  guess = int(input("Enter your guess (1-100): "))
  if guess == lucky: # you are lucky
    print("BINGO!")
    running = False
  elif guess < lucky:
    print("Guess higher")
  else: # guess > lucky  
    print("Guess lower")<jupyter_output>38
Enter your guess (1-100): 50
Guess lower
Enter your guess (1-100): 25
Guess higher
Enter your guess (1-100): 37
Guess higher
Enter your guess (1-100): 44
Guess lower
Enter your guess (1-100): 40
Guess lower
Enter your guess (1-100): 39
Guess lower
Enter your guess (1-100): 38
BINGO!<jupyter_text>**Exercise: Be positive v1**Write a program to prompt the user for x positive integers, one at a time. Compute and output the sum of all x integers entered.Enhance your program to also compute and output the average of the x positive integers.<jupyter_code># initialise 
x = int(input("Enter x: "))
total = 0

# loop for x positive integers
for i in range(x):
  num = int(input("Enter positive integer: "))
  total += num # total = total + num7

# display results  
print(total)
print(total / x)<jupyter_output>Enter x: 3
Enter positive integer: 7
Enter positive integer: 8
Enter positive integer: 9
24
8.0<jupyter_text>**Exercise: Be positive v2**Write a program to prompt the user for x positive integers, one at a time, until the user terminates with -1. Compute and output the sum of all x integers entered.Enhance your program to also the largest and smallest numbers entered.<jupyter_code># assume valid positive range is 1 to 100
# initialise
total = 0
count = 0
highest = 0
lowest = 101

# loop until -1
num = int(input("Enter positive number: "))
while num != -1:
  if num > 0:
    total += num
    count += 1
    if num > highest:
      highest = num
    if num < lowest:
      lowest = num  
  num = int(input("Enter positive number: "))    

# display results
print(total)
print(f"Highest: {highest}")
print(f"Lowest: {lowest}")

# Functions 
# A function is a reusable subprogram that performs a well defined task

# Examples: print(), input(), range(n), randint(low,high)

def menu():
  print("(1) Add")
  print("(2) Update")
  print("(3) Delete")
  print("(4) Search")
  print("(0) Quit")

option = ""
while option != '0':
  menu()
  option = input("Enter option: ")
print("Bye")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12776069331842327, 'fraction_numerical': 0.03885937936818563, 'mean_word_length': 3.511979823455233, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '604567', 'license': 'CC0-1.0', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1222, 'n_words': 503, 'path': 'computing_sh/Python03_Loops.ipynb', 'repo_name': '4wqre/cp4all', 'size': '3577'}