Chapter 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Associate the average of the numbers from 1 to n (where n is a positive integer value) with the variable avg.

avg = (1+n)/2

Given a variable bridge_players, write a statement that increases its value by 4.

bridge_players = bridge_players+4

Fibonacci numbers are a sequence of integers, starting with 1, where the value of each number is the sum of the two previous numbers, e.g. 1, 1, 2, 3, 5, 8, etc. Write a function called fibonacci that takes a parameter, n, which contains an integer value, and have it return the nth Fibonacci number. (There are two ways to do this: one with recursion, and one without.)

def fibonacci(n): if n<=0: return 0 if n<=2: return 1 else: return fibonacci(n-1)+fibonacci(n-2)

At one college, the tuition for a full-time student is $8,000 per semester. It hasbeen announced that the tuition will increase by 3 percent each year for the next 5 years. Write a program with a loop that displays the projected semester tuition amount for the next 5 years.The program should print out the result in the formIn 1 year, the tuition will be $8002.3.In 2 years, the tuition will be $8103.2.In 3 years, ...In 4 years, ...In 5 years, ...(If, for example, the tuition would cost 8002.3 dollars in one year, etc.)

def tuition_increase(current_tuition): past_tuition=current_tuition for i in range(1,6): if i == 1: new_tuition=past_tuition*1.03 print("In 1 year, the tuition will be $"+str(new_tuition)+".") past_tuition=new_tuition else: new_tuition=past_tuition*1.03 print("In "+str(i)+" years, the tuition will be $"+str(new_tuition)+".") past_tuition=new_tuition tuition_increase(8000)

Write a loop that reads strings from standard input, where the string is either "duck" or "goose". The loop terminates when "goose" is read in. After the loop, your code should print out the number of "duck" strings that were read.

ducks = 0 strings = input() while strings != "goose": ducks += 1 strings = input() print(ducks)

You want to know your grade in Computer Science, so write a program that continuously takes grades between 0 and 100 to standard input until you input "stop", at which point it should print your average to standard output.

grades=input() counter=0 sum=0 while grades != "stop": sum+=int(grades) counter +=1 grades=input() print(sum/counter)

Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and associates that value with total. Thus your code should associate 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 with total. Use no variables other than k and total.

k=0 total=0 while k<50: k+=1 total+=k*k

Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q. For example, if h is 19, you would assign 30 to q because the perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16 and 30==1+4+9+16.

i=1 q=0 while ((i*i)<h): q=(i*i)+q i+=1

Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the number you compute with the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36,.

i=1 q=0 while i*i<k: i+=1 while i*i<m: q+=1 i+=1

In this exercise, use the following variables: i,lo, hi, and result. Assume that lo and hi each are associated with an int and that result refers to 0. Write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result. Your code should not change the values associated with lo and hi. Also, just use these variables: i,lo, hi, and result.

i=lo result=0 while i<=hi: result=result+i i+=1

Associate True with the variable is_ascending if the list numbers is in ascending order (that is, if each element of the list is greater than or equal to the previous element in the list). Otherwise, associate False with is_ascending

is_ascending=True for i in range(1,len(numbers)): if numbers[i]<numbers[i-1]: is_ascending=False

Given a positive integer n, assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)

is_prime = True for m in range(2,n): if n%m == 0: is_prime = False

Use two variables k and total to write a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus, your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.

k=0 total=0 #so while k<50: k+=1 total+=k*k

You're a swimmer, and you want to compare all of your race times to find the fastest one. Write a program that continuously takes race times as doubles from standard input, until the input is "no more races," at which point it should print out the time of your fastest race.

list1 = [] while True: race_time = input("") if race_time == "no more races": break list1.append(float(race_time)) print(min(list1))

Write a program that creates a pattern like the one below. Let the user input a nonnegative integer to determine the number of lines of the pattern. # ## ## ## ## #For example, if the user enters 3, the pattern should look like this: # ## #

num = int(input("Enter number of lines for pattern:")) for i in range(0,num): print("#", end="") for j in range(0,i): print(" ", end="") print("#")

Given a variable profits, write a statement that increases its value by a factor of 10.

profits = profits*10

Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the first h perfect squares, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q. For example, if h is 4, you would assign 30 to q because the first 4 perfect squares (starting with 1) are: 1, 4, 9, 16 and 30==1+4+9+16.

q=0 for k in range(1,h+1): q+=k*k

Assume there is a variable, h already associated with a positive integer value. Write the code necessary to count the number of perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Assign the sum you compute to a variable q For example, if h is 19, you would assign 4 to q because there are perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16.

q=0 n=1 while n*n<h: q+=1 n+=1

An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ..., the distance is 2 while in the sequence 6, 12, 18, 24, ..., the distance is 6. Given the positive integer distance and the positive integer n, associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance. For example, if distance is 2 and n is 10, then sum would be associated with 25 because 1+3+5+7+9 = 25.

sum=0 for i in range(1,n+1,distance): sum+=i

Write a statement that increments total by the value associated with amount. That is, add the value associated with amount to that associated with total and assign the result to total.

total=(total + amount)

You work for a bakery that sells two items: muffins and cupcakes. The number of muffins and cupcakes in your shop at any given time is stored in the variables muffins and cupcakes, which have been defined for you.Write a program that takes strings from standard input indicating what your customers are buying ("muffin" for a muffin, "cupcake" for a cupcake). If they buy a muffin, decrease muffins by one, and if they buy a cupcake, decrease cupcakes by 1. If there is no more of that baked good left, print ("Out of stock").Once you are done selling, input "0", and have the program print out the number of muffins and cupcakes remaining, in the form "muffins: 9 cupcakes: 3" (if there were 9 muffins and 3 cupcakes left, for example).

to_buy=input() while to_buy != "0": if to_buy=="muffin": if muffins>0: muffins-=1 else: print("Out of stock") if to_buy=="cupcake": if cupcakes>0: cupcakes-=1 else: print("Out of stock") to_buy=input() print("muffins:", muffins, "cupcakes:", cupcakes)

Given a variable n refers to a positive int value, use two additional variables, k and total to write a for loop to compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus your code should put 1*1*1 + 2*2*2 + 3*3*3 +... + n*n*n into total. Use no variables other than n, k, and total.

total=0 for k in range(1,n+1): total = total + k**3

Given that n refers to a positive int use a while loop to compute the sum of the cubes of the first n counting numbers, and associate this value with total. Use no variables other than n, k, and total.

total=0 k=1 while k<=n: total+=k**3 k+=1

In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of n is the product of all the nonnegative integers from 1 to n. For example:7! = 1 x 2 x 3 x 4 x 5 x 6 x 7 = 5,040 Write a program that lets the user enter a nonnegative integer and then uses a loop to calculate the factorial of that number. Print the factorial to standard output.

user_integer = int( input("Enter a nonnegative integer:")) while user_integer < 1: user_integer = int(input( "Please enter a positive number: ")) factorial = 1 for current_number in range( 1, user_integer + 1 ): factorial = factorial * current_number print(factorial)

You have a unique ID number, which is represented by the variable id, containing a string of numbers. Write a program that continuously takes strings to standard input. If the string is not your ID number, print "This is not your ID number." If it is, print "This is your ID number: " followed by the number, and terminate the loop.

while True: s = input().strip() if s == id: print("This is your ID number: " + s) break print("This is not your ID number.")


संबंधित स्टडी सेट्स

What is a primate II: Prosimians, Tarsiers, and NWM

View Set

Functions of the parts of an egg

View Set

Chapter 33: Abuse in the Family and Community

View Set

Conflict management com4462 Study Guide

View Set

Neuromuscular and Nervous Systems

View Set

6th Grade Geography - Mayan, Incan and Aztec Civilizations

View Set