Chapter 3 Programming Exercises

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

3.3 Modify the guessing-game program 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. Use I'm out of guesses, and you cheated and Hooray, I've got it in X tries as your final output.

# Modify the code below: import random smaller = int(input("Enter the smaller number: ")) larger = int(input("Enter the larger number: ")) myNumber = random.randint(smaller, larger) count = 0 while True: count += 1 userNumber = int(input("Enter your guess: ")) if userNumber < myNumber: print("Too small") elif userNumber > myNumber: print("Too large") else: print("You've got it in", count, "tries!") break -- # Modify the code below: import math smaller = int(input("Enter the smaller number: ")) larger = int(input("Enter the larger number: ")) count = 0 print("") while True: count += 1 userNumber = (smaller + larger) // 2 print("%d %d" % (smaller, larger)) print("Your number is %d" % userNumber) answer = input("Enter =, <, or >: ") if answer == "=": print("Hooray, I've got it in %d tries!" % count) break elif smaller == larger: print("I'm out of guesses, and you cheated") break elif answer == "<": larger = userNumber - 1 else: smaller = userNumber + 1

3.1 Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is an equilateral triangle. - Use The triangle is equilateral. and The triangle is not equilateral. as your final outputs.

# Request the input firstSide = int(input("Enter the first side: ")) secondSide = int(input("Enter the second side: ")) thirdSide = int(input("Enter the third side: ")) print("") if firstSide == secondSide == thirdSide: print("The triangle is equilateral.") else: print("The triangle is not equilateral.")

3.8 The greatest common divisor of two positive integers, A and B, is the largest number that can be evenly divided into both of them. Euclid's algorithm can be used to find the greatest common divisor (GCD) of two positive integers. You can use this algorithm in the following manner: Compute the remainder of dividing the larger number by the smaller number. Replace the larger number with the smaller number and the smaller number with the remainder. Repeat this process until the smaller number is zero. The larger number at this point is the GCD of A and B. Write a program that lets the user enter two integers and then prints each step in the process of using the Euclidean algorithm to find their GCD.

#Request input small = int(input("Enter the smaller number: ")) large = int(input("Enter the larger number: ")) #Calculate for i in range(1, small+1): if (small % i == 0) and (large % i == 0): gcd = i print("\nThe greatest common divisor is {}" .format(gcd))

3.7 Teachers in most school districts are paid on a schedule that provides a salary based on their number of years of teaching experience. For example, a beginning teacher in the Lexington School District might be paid $30,000 the first year. For each year of experience after this first year, up to 10 years, the teacher receives a 2% increase over the preceding value. Write a program that displays a salary schedule, in tabular format, for teachers in a school district. The inputs are: (1) Starting salary (2) Annual percentage increase (3) Number of years for which to print the schedule Each row in the schedule should contain the year number and the salary for that year

#Request inputs salary = float(input("Enter the starting salary: $")) percent = float(input("Enter the annual % increase: ")) years = int(input("Enter the number of years: ")) print("") #Setup Chart print("Year Salary") print("-------------") #Calculation for i in range(1, years + 1): print(i, "\t", end="") print("%.2f" % salary) salary = salary * (1 + percent / 100)

3.2 Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square of one side equals the sum of the squares of the other two sides. Use The triangle is a right triangle. and The triangle is not a right triangle. as your final outputs.

#Request the triangle sides a = float(input("Enter the first side: ")) b = float(input("Enter the second side: ")) c = float(input("Enter the third side: ")) print("") #Compute the sides if ((a**2 == b**2 + c**2) or (a**2 + b**2 == c**2) or (a**2 + c**2 == b**2)): print("The triangle is a right triangle.") else: print("The triangle is not a right triangle.")

3.5 A local biologist needs a program to predict population growth. The inputs would be: The initial number of organisms, as an int The rate of growth (a real number greater than 1), as a float The number of hours it takes to achieve this rate, as an int A number of hours during which the population grows, as an int For example, one might start with a population of 500 organisms, a growth rate of 2, and a growth period to achieve this rate of 6 hours. Assuming that none of the organisms die, this would imply that this population would double in size every 6 hours. Thus, after allowing 6 hours for growth, we would have 1000 organisms, and after 12 hours, we would have 2000 organisms. Write a program that takes these inputs and displays a prediction of the total population.

import math def prediction(): initialNumber = int(input("Enter the initial number of organisms: ")) rateGrowth = float(input("Enter the rate of growth [a real number > 1]: ")) while(rateGrowth<1): print("Invalid.. growth rate.") rateGrowth = float(input("Enter the rate of growth [a real number > 1]: ")) numberHours = int(input("Enter the number of hours to achieve the rate of growth: ")) totalHours = int(input("Enter the total hours of growth: ")) print("") tp=initialNumber hours = 1 while (hours < totalHours): tp *= rateGrowth hours += numberHours print("The total population is " + str(int(tp))) prediction()


Kaugnay na mga set ng pag-aaral

PRINCIPLES OF PERIPHERAL BLOOD FILMS

View Set

Midterm Project Information System

View Set