Chapter 3 Fundamentals Of Phython Lambert Flashcards ionicons-v5-c

A for loop is convenient for

running a set of statements a predictable number of times

By default, the while loop is an

entry-controlled loop.

Modify the guessing-game program of Section 3.5 so that the user thinks of a number that the computer must guess. The computer must make no more than the minimum number of guesses, and it must prevent the user from cheating by entering misleading hints. (Hint: Use the math.log function to compute the minimum number of guesses needed after the lower and upper bounds are entered.)

import mathsmaller = int(input("Enter the smaller number: "))larger = int(input("Enter the larger number: "))count = 0maxGuesses = round(math.log(larger - smaller + 1, 2))while True: count += 1 print(smaller, larger) yourNumber = (smaller + larger) // 2 print("Your number is", yourNumber) answer = input("Enter =, <, or >: ") if answer == "=": print("Hooray, I've got it in", count, "tries!") break elif count == maxGuesses: print("I'm out of guesses, and you cheated!") break elif answer == "<": larger = yourNumber - 1 else: smaller = yourNumber + 1

Write a program that receives a series of numbers from the user and allows the user to press the enter key to indicate that he or she is finished providing inputs. After the user presses the enter key, the program should print the sum of the numbers and their average.

count = 0theSum = 0.0data = input("Enter a number or just enter to quit: ")while data != "": number = float(data) theSum += number count += 1 data = input("Enter a number or just enter to quit: ")print("The sum is", theSum)average = (theSum / count)print("The average is,", average)