MPL 4

Ace your homework & exams now with Quizwiz!

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, on ton a single line and separated by a single space, the sum of all the even integers read and the sum of all the odd integers read.

POSITIVE = 0 n = int() even_sum = int() odd_sum = int() while True: n=int(input()) if n <=0: break elif n%2==0: even_sum = even_sum + n else: odd_sum = odd_sum + n print (even_sum, odd_sum)

Create a program that will draw a right triangle. Your program needs to ask the user how big the base of the triangle needs to be. The program will then use this input and a loop to draw the triangle.

base_size = int() base_size = int(input("Enter the base of the triangle: ")) for r in range(base_size): print (('*'+' ')*(r+1))

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.

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

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=k+1 total= (total+(k*k))

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=float() total = float() for k in range(51): total = total + (k*k) print (total)

Create a program that will calculate how much money you will save. Your program needs to ask the user how many months they wish to save for, how many weeks during a month they wish to save and how much money they are willing to save with each weekly paycheck. It will then total the amount saved for each month and in total.

monthly_total = int() current_savings = int() months = int() num_Months = int(input("How many months are you saving for: \n")) for counter in range(num_Months): num_Weeks = int(input("How many weeks are you saving this month: \n")) for weeks in range(num_Weeks): savings_amount = int(input("Enter the amount of savings this week: $\n")) monthly_total += savings_amount print ("This month's saving: $ ",format(monthly_total,'.1f')) current_savings += savings_amount monthly_total = 0 print ("Your current savings: $ ",format(current_savings, '.1f'))

Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line.

n = str() land = int() air = int() water = int() n = input() while True: if n == "land": land +=1 n=input() elif n == "air": air +=1 n=input() elif n == "water": water +=1 n=input() elif n =="xxxxx": print (("land:")+str(land)) print (("air:")+str(air)) print (("water:")+str(water)) break

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.

n= input() while n != id: print ("This is not your ID number.") n = input() print ("This is your ID number:",n)

Given this data, write a loop and any necessary code that reads the data and stores the total payroll of all employees into the variable total. Note that you will have to add up the numbers worked by each employee and multiply that by that particular employee's pay rate to get the employee's pay for the week-- and sum those values into total.

num_employees = int() paid_in_cents = int() hours_paid = int() employee_total = int() total = int() num_employees = int(input("Enter number of employees: \n")) for counter in range(num_employees): paid_in_cents = int(input("How much do you pay this employee, in cents: \n")) for counter in range(0,5): hours_paid = int(input()) employee_total += hours_paid * paid_in_cents total += employee_total employee_total = 0 print (total)

Write some code that repeatedly reads a value from standard input into the variable response until at last a Y or y or N or n has been entered.

response = str() while True: response=input("Enter a number: ") if (response=='y' or response =='Y' or response=='N' or response=='n'): break

Write a loop that reads positive integers from standard input, printing out those values that are even, each on a separate line. The loop terminates when it reads an integer that is not positive.

sum = int() num = int() while True: num=int(input()) if num<=0: break else: if (num % 2 ) == 0: print (num)

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": numOfGrades = numOfGrades + 1 total = total + int(grade) average = total/numOfGrades grade = input() print (average)

At one college, the tuition for a full-time student is $8,000 per semester. It has been 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.

tuition = 8000.00 for counter in range (1,2): tuition=tuition * 1.03 print ("In",counter, "year, the tuition will be $"+str(tuition)+".") new_tuition = tuition * 1.03 for counter in range (2,6): print ("In",counter,"years, the tuition will be $"+str(new_tuition)+".") new_tuition = new_tuition*1.03

Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each on a separate line, The loop terminates when it reads an integer that is not positive.

while True: num=int(input()) if num>100: print (num) elif num<=0: break

Write some code that repeatedly reads a value into the variable n until a number between 1 and 10 (inclusive) has been entered.

while True: n=int(input("Enter a number: ")) if(n>=1 and n<=10): break


Related study sets

C839 - Intro to Cryptography - Pre-Assessment & Vocabulary

View Set

Econ 120 Chapter 8 Homework Examples

View Set

MGT 370: Chapter 07 Assignment: Designing Adaptive Organizations

View Set

MGMT 3600 EXAM 2 chapters 4,5,&6.

View Set

15th CH. 41: INTESTINAL AND RECTAL DISORDERS

View Set

Med Surg - Chapter 55 - Care of Patients with Stomach Disorders

View Set