Pearson My Programing Lab Python lab 6

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

factorial=1 number=int(input("Enter a nonnegative integer:")) while (number>0): (Tab) factorial= factorial*number (Tab) number=number-1 print(factorial)

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.

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

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: (Tab) race_time = input() (Tab) if race_time == "no more races": (Tab) (Tab) break (Tab) list1.append(float(race_time)) print(min(list1))

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 i in range(h+1): (Tab) q += i**2 print(q)

Write a program that creates a pattern like the one below. Let the user inputa nonnegative integer to determine the number of lines of the pattern.

step=int(input("Enter number of lines for pattern:")) for r in range(step): (Tab) print("#",end="") (Tab) for s in range(r): (Tab) (Tab) print(" ",end="") (Tab) print("#")

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": (Tab) if to_buy=="muffin": (Tab) (Tab) if muffins>0: (Tab) (Tab) muffins-=1 (Tab) else: (Tab) (Tab) print("Out of stock") (Tab) if to_buy=="cupcake": (Tab) (Tab) if cupcakes>0: (Tab) (Tab) cupcakes-=1 (Tab) else: (Tab) (Tab) print("Out of stock") (Tab) to_buy=input() print("muffins:", muffins, "cupcakes:", cupcakes)

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.

total = int() numOfGrades = int() grade = str() average = float() grade = input() while grade != "stop": (Tab) numOfGrades = numOfGrades + 1 (Tab) total = total + int(grade) (Tab) average = total/numOfGrades (Tab) grade = input() print (average)

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 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: (Tab) if input("") != id: (Tab) print("This is not your ID number.") (Tab) else: (Tab) print("This is your ID number:", id) (Tab) break


Ensembles d'études connexes

United States Government and Politics

View Set

The history of the Statue of Liberty

View Set

Priorities for the Preoperative Patient

View Set

CH 1 VA State Health and Insurance Exam

View Set

Chapter 11 (The Americas 2500 b.c.e.-1500 c.e.)

View Set

Chapter 1.3 Describing Quantitative Data with Numbers

View Set