CH4 ALGORITHM QUIZ

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

Write a loop that calculates the total of the following series of numbers: 1/30+2/29+3/28+⋯30/1

CODE: # Display a total for number in range sum=0.0 for number in range(1,31): sum += number/(31-number) print('1/30 + 2/29 + 3/28 +...30/1=',sum) PRINTS: 1/30 + 2/29 + 3/28 +...30/1= 93.84460105853213

Write code that prompts the user to enter a positive nonzero number and validates the input.

CODE: age=int(input('Enter your age: ')) # get age while age <=0 : # validation print('ERROR: Age is invalid!' #invalid data message '\nAge is a positive nonzero number') age=int(input('Enter your age: ')) PRINTS: Enter your age: 0 ERROR: Age is invalid! Age is a positive nonzero number Enter your age: -4 ERROR: Age is invalid! Age is a positive nonzero number Enter your age: 5

Write a while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user if he or she wishes to perform the operation again. If so, the loop should repeat, otherwise it should terminate.

CODE: calories=0 another = 'y' print('Find the total calories consumed for breakfast and dinner.') while another =='y': breakfast= int(input('Enter total calories consumed for breakfast: ')) dinner=int(input('Enter total calories consumed for dinner: ')) calories= breakfast + dinner print('For both meals, the total calories consumed:',calories) another=input('Calculate another set of calories? Enter y for yes): ') print() PRINTS: Find the total calories consumed for breakfast and dinner. Enter total calories consumed for breakfast: 1500 Enter total calories consumed for dinner: 765 For both meals, the total calories consumed: 2265 Calculate another set of calories? Enter y for yes): y

Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered.

CODE: expenses=10 # adds up total of ten input values entered month=0.0 # accumulator variable. print('Enter',expenses,'(min) monthly expenses to calculate' 'the\ntotal amount you spend each month on these expenses.') for sum in range(expenses): amount=float(input('\tEnter monthly expense amount: ')) month=month + amount print('\n\tYour TOTAL EXPENSE AMOUNT is:$ ', format(month,',.2f'),' a month',sep='') PRINTS: Enter 10 (min) monthly expenses to calculate the total amount you spend each month on these expenses. Enter monthly expense amount: 31.45 Enter monthly expense amount: 13 Enter monthly expense amount: 15 Enter monthly expense amount: 53 Enter monthly expense amount: 15 Enter monthly expense amount: 153 Enter monthly expense amount: 156 Enter monthly expense amount: 11 Enter monthly expense amount: 154 Enter monthly expense amount: 45 Your TOTAL EXPENSE AMOUNT is:$ 646.45 a month

Write a for loop that displays the following set of numbers: 0, 10, 20, 30, 40, 50...1000

CODE: for number in range(0,1001,10): # increments of ten print(number,' ',end='') PRINTS: 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500 510 520 530 540 550 560 570 580 590 600 610 620 630 640 650 660 670 680 690 700 710 720 730 740 750 760 770 780 790 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 1000

Write a set of nested loops that display 10 rows of # characters. There should be 15 # characters in each row.

CODE: hashtag=15 for symbol in range(10): for columns in range(hashtag): print('# ', end='') print() PRINTS: # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Write code that prompts the user to enter a number in the range of 1 through 100 and validates the input.

CODE: score=int(input('Test Score entered must be a number(1-100)' '\nEnter test score: ')) # get a test score while score < 1 or 100 < score: #validates input data print('SCORE IS NOT VALID') print('Score must be between 1 and 100') score=int(input('Enter test score: ')) PRINTS: Test Score entered must be a number(1-100) Enter test score: 0 SCORE IS NOT VALID Score must be between 1 and 100 Enter test score: -8 SCORE IS NOT VALID Score must be between 1 and 100 Enter test score: 100

Write a while loop that lets the user enter a number. The number should be multiplied by 10, and the result assigned to a variable named product. The loop should iterate as long as product is less than 100.

CODE: product =1 while product < 100 : packages= int(input('Enter number of packages: ')) product = packages * 10 print('Total in stock:',product) PRINTS: Enter number of packages: 5 Total in stock: 50 Enter number of packages: 10 Total in stock: 100

Rewrite the following statements using augmented assignment operators. a. x = x + 1 b. x = x * 2 c. x = x / 10 d. x = x - 100

a. x + = 1 b. x * = 2 c. x / = 10 d. x - = 100


Kaugnay na mga set ng pag-aaral

8.3 Elements of a Valid Contract

View Set

Declaration Of Independence Review Eng3 - 1/27

View Set

New English File. Beginner. Unit 3 A

View Set

Project/Service Management 2015 - 2019

View Set

Solid State Drive vs. Hard Disk Drive

View Set

Chapter 10 understanding individual Behavior

View Set

BPS Ch 10 MC, BPS Ch 5 MC, BPS Ch 6 MC, BPS Ch 7 MC, BPS Ch 8 MC, BPS Ch 9 MC, BPS Ch 11 MC

View Set