Learn Python – NOTES 31 – 40

Program 31 -Break and Continue

# break – example
print(“The break instruction:”)
for i in range(1, 6):
if i == 3:
break
print(“Inside the loop.”, i)
print(“Outside the loop.”)

# continue – example
print(“\nThe continue instruction:”)  #NOTE: Puts a space between

for i in range(1, 6):
if i == 3:
continue
print(“Inside the loop.”, i)
print(“Outside the loop.”)

Program 32 – Kind of a Bubble Sort one Step at a Time

largest_number = -99999999 # Initialize variable
counter = 0 # Initialize counter

while True:
number = int(input(“Enter a number or type -1 to end program: “))
if number == -1:
break
counter += 1
if number > largest_number:
largest_number = number
# Does not keep all numbers in memory
# only a single number after evaluation

if counter != 0:
print(“The largest number is”, largest_number)
else:
print(“You haven’t entered any number.”)

Program 33 -Same as Program 32, but using CONTINUE

largest_number = -99999999
counter = 0

number = int(input(“Enter a number or type -1 to end program: “))

while number != -1:
if number == -1:
continue
counter += 1

if number > largest_number:
largest_number = number
number = int(input(“Enter a number or type -1 to end program: “))

if counter:
print(“The largest number is”, largest_number)
else:
print(“You haven’t entered any number.”)

Program 34 – Practice program for BREAK

my_word = “xxx”
sentence = “”

print (“xxx to quit\n”)

while True:
one_word = input(“Enter a word: “)
if one_word == “xxx”:
print (“\n” + sentence)
break
sentence = sentence + ” ” + one_word

Program 35 – Practice program for CONTINUE

REDO LABS previous lab (3.1.2.10) AND previous lab (3.1.2.11)
I DO NOT GET CONTINUE AT ALL

Program 36 -Import Sys and Exit and While

import sys

i = 1
while i < 5:
     print(i)
     i += 1
else:
     sys.exit()
     print(“else:”, i)

 

for i in range(5):
print(i)
else:
i+=1
print(“else:”, i)

print (“\nSecond Test\n”)

i = 111
for i in range(2, 1):
print(i)
else:
print(“else:”, i)

Program 37 -Temperature Conversion Program

import sys
MyExit = “Exit”

while MyExit != “X”:
    MyTitle = “Temperature Conversion Program”
    MyInstructions = “Enter C if convertion from C to F.\n”
    MyInstructions = MyInstructions + “Enter F if converting from F to C\n”
    MyInstructions = MyInstructions + “Enter X to Exit”
    print (“\n”,MyTitle,”\n”)
    print (MyInstructions+”\n”)
    MyPath=input(“Enter C or F: “)
    if MyPath == “C”:
       StartTemp=int(input(“Enter temperature: “))
       MyOutput = (StartTemp*1.8)+32
       ResultPath=”F”
    elif MyPath == “F”:
       StartTemp=int(input(“Enter temperature: “))
       MyOutput = (StartTemp-32) * (5/9)
       ResultPath=”C”
    else:
       sys.exit()
    print(StartTemp, MyPath, “is the same as”,MyOutput,ResultPath)

Program 38 – A look at Printing

  • name = input("Enter your name: ") # Your Name
  • print("Hello, " + name + ". Nice to meet you!") #Hello, XXXX. Nice to meet you!
  • print("Hello, ",name ,". Nice to meet you!") # Hello, XXXX . Nice to meet you!
  • print(\nPress Enter to end the program.”)
           input
    ()
           print
    (“THE END.”)
  • num_1 = input(“Enter the first number: “) # Enter 12
    num_2 = input(“Enter the second number: “) # Enter 21
    print(num_1 + num_2) # the program returns 1221

     
  • You can also multiply (* ‒ replication) strings, e.g.:
    my_input = input(“Enter something: “) # Example input: hello
    print(my_input * 3) # Expected output: hellohellohello

Program 39 -using f to format and braces to create dictionary

start_hour = int(input(“Enter the starting hour (0-23): “))
start_minute = int(input(“Enter the starting minute (0-59): “))
duration_minutes = int(input(“Enter the duration in minutes: “))

total_minutes = start_hour * 60
total_minutes = total_minutes + start_minute
total_minutes = total_minutes + duration_minutes
# OR total_minutes = start_hour * 60 + start_minute + duration_minutes

print(total_minutes)
print(total_minutes/60)
end_hour = total_minutes // 60 # Drops the remainder
print(end_hour)
end_minute = total_minutes % 60 # Leaves only the remainder
print(str(end_minute)+”\n”)
print(f”The event will end at {end_hour % 24}:{end_minute:02d}”)
# print({end_hour % 24}:{end_minute:02d}) # THIS GENERATES AN ERROR
# print(f{end_hour % 24}:{end_minute:02d}) # THIS GENERATES AN ERROR
print(“{end_hour % 24}:{end_minute:02d}”) # {} PRINTS EVERYTHING IN QUOTES
print(f”{end_hour % 24}:{end_minute:02d}”) # {} CREATES A DICTIONARY

Program 40 – True False ==    !=    <=     >=

n = int(input(“Enter a number:”))
print(n>=100)