# # while loop - practice # # Practice # # Preliminary notes: - Presumed knowledge - Use only the studied material # (1) Write a program that receives non-negative # integers one after another. # The iteration stops when the number -1 is entered. # The program calculates how many integers # are smaller than 100, and prints the result. # 1 num = int(input("PLease enter a non-negative integer: ")) counter = 0 while num != -1: if num < 100: counter = counter + 1 print(counter) num = int(input("PLease enter a non-negative integer: ")) print("Number of integers smaller than 100:", counter) # 2 while True: num = int(input("PLease enter a non-negative integer: ")) if num == -1: STOP THE LOOP else: print("bamba") # (2) Write a program that receives a non-empty string s # and then a series of additional non-empty strings. # The iteration ends when an empty string is entered. # The program prints the number of strings # other than s that are equal to s. s = input("Please input any string: ") otherS = input("Please input another string: ") counter = 0 while otherS != "": if s == otherS: counter = counter + 1 otherS = input("Please input another string: ") print("Number of strings equal to first string: ", counter) # (3) Two chefs have made it to the final # round of the international chefs # championship. The audience chooses # the winner. Write a program that # receives the votes, either 1 for # the first chef, or 2 for the second. # The iteration ends when 0 is entered. # The program prints the number # representing the winner. vote = input("Please enter your vote, 1 or 2: ") counter1 = 0 counter2 = 0 while vote != "0": if vote == "1": counter1 = counter1 + 1 else: counter2 = counter2 + 1 vote = input("Please enter your vote, 1 or 2: ") if counter1 > counter2: print(1) elif counter2 > counter1: print(2) else: print("TIE") # (4) The newspaper "Read Me" has a gift # for new subscribers: either a book # for those over 50 year old, # or a movie ticket for all other # subscribers. Write a program that # receives the ages of new subscribers. # The iteration ends when the number -1 # is entered. The program prints for each # subscriber her/his gift: "book" or "movie". # The program also prints the number # of subscribers who were given books # and the number of those who were # given movie tickets. age = int(input("Please enter age: ")) over50 = 0 notOver50 = 0 while age != -1: if age > 50: print("BOOK") over50 = over50 + 1 else: print("TICKET") notOver50 = notOver50 + 1 age = int(input("Please enter age: ")) print("Number of books:", over50) print("Number of tickets:", notOver50) # (5) Write a program that receives # non-negative integers one after another. # The iteration ends when the number -1 # is entered. The program calculates # the sum of odd integers, and prints # the result. num = int(input("Please enter a non-negative integer: ")) sumNums = 0 while num != -1: if num % 2 == 1: sumNums = sumNums + num num = int(input("Please enter a non-negative integer: ")) print("Sum is:", sumNums) # (6) Write a program that receives a number # greater than 50 and finds how many # integers whose ones digit is 5 sum up to # a number smaller than the received number. # The program prints the calculated sum # and the number of integers. 51 5 + 15 + 25 num = float(input("Please enter any number: ")) sumNums = 0 intEndingWith5 = 5 counter = 0 while sumNums + intEndingWith5 < num: sumNums = sumNums + intEndingWith5 intEndingWith5 = intEndingWith5 + 10 counter = counter + 1 print(counter, intEndingWith5, sumNums) print("Number of integers:", counter) print("Sum of integers:", sumNums) # (7) Write a program that receives non-negative # integers one after another. The iteration ends # when -1 is entered. The program finds the # average of integers greater than 5. num = int(input("Please enter a non-negative integer: ")) sumNums = 0 counter = 0 while num != -1: if num > 5: counter = counter + 1 sumNums = sumNums + num print(counter, sumNums) num = int(input("Please enter a non-negative integer: ")) print("Average is", sumNums / counter) # (8) Write a program that receives # non-negative integers one after another. # The iteration ends when -1 is entered. # The program finds the biggest even # integer. num = int(input("please enter a non-negative integer: ")) maxNum = -1 while num != -1: if num % 2 == 0: if num > maxNum: maxNum = num num = int(input("please enter a non-negative integer: ")) print("Maximal ever integer: ", maxNum) # (9) Write a program that compares between # the prices of two cottage brands in # branches of a supermarket chain. # The program iteratively receives pairs of # brand prices in all branches. The iteration # ends when the string 'q' is entered as # the price of the first cottage in a pair. # The program prints the number of branches # in which the price of the first brand was # bigger than the price of the second brand. price1 = input("Please enter brand 1 price: ") counter = 0 while price1 != 'q': price1 = float(price1) price2 = float(input("Please enter brand 2 price: ")) if price1 > price2: counter = counter + 1 price1 = input("Please enter brand 1 price: ") print("Number found: ", counter) # loop before input proceed = True counter = 0 while proceed: price1 = input("Please enter brand 1 price: ") if price1 == 'q': proceed = False continue price1 = float(price1) price2 = float(input("Please enter brand 2 price: ")) if price1 > price2: counter = counter + 1 print("Number found: ", counter) # (10) Write a program that checks the efficiency # of a doctor-s work in a certain day. # The program iteratively receives # for all patients checked by the doctor # both their ids and the number of checkup # minuets. This data is received in # id-minutes pairs. The iteration ends when # the string 'stop' is entered as the id. # The program finds the patient whose checkup # was the shortest (you may assume there was # only one such patient), and prints her/his # id and that number of minuets. # with continue proceed = True minMinuets = 60 * 24 while proceed: patientID = input("Please enter patient's id: ") if patientID == 'stop': proceed = False continue minutes = int(input("Please enter number of minuets: ")) if minutes < minMinuets: minMinuets = minutes idMin = patientID print(idMin, minMinuets) # with break minMinuets = 60 * 24 while True: patientID = input("Please enter patient's id: ") if patientID == 'stop': break minutes = int(input("Please enter number of minuets: ")) if minutes < minMinuets: minMinuets = minutes idMin = patientID print(idMin, minMinuets) (11) Write a program that implements the following dice game. In each round two cubes whose faces display the numbers 1–6 are rolled. Following each roll the sum of number on the upper faces is calculated. The game ends when in three consecutive rolls the sum is 12. The program should include the following line: while (True) When the program ends it prints the number of tosses, not counting the last three tosses. 4 5 6 6 -> round = 1 2 5 6 6 -> round = 1 6 6 -> round = 2 1 4 -> round = 0 6 6 -> round = 1 6 6 -> round = 2 6 6 -> round = 3 ---> break import random rounds = 0 rolls = 0 while True: dice1 = random.randint(1, 6) dice2 = random.randint(1, 6) rolls = rolls + 1 if dice1 + dice2 == 12: rounds = rounds + 1 print(dice1, dice2, rounds) if rounds == 3: break else: rounds = 0 print(dice1, dice2, rounds) print("Number of rolls", rolls - 3)