Question

In: Computer Science

PYTHON PROGRAM QUESTION 1 Briefly describe how a while loop works QUESTION 2 Compare and contrast  a...

PYTHON PROGRAM

QUESTION 1

  1. Briefly describe how a while loop works

QUESTION 2

Compare and contrast  a while statement and an  if statement ?

QUESTION 3

What is a loop body?

QUESTION 4

What is the value of variable num after the following code segment?

num = 10;

while  num > 0:
  num = num -1;

QUESTION 5

What is the value of variable num after the following code segment is executed?

var num = 1

while  num >10:

  num = num -1

QUESTION 6

Why the loop does not terminate?

n = 10
answer = 1
while n > 0:
answer = answer + n
    n = n + 1
print(answer)

QUESTION 7

Convert the following for loop to a while loop.

total = 0
for i in range(10):
total = total + i

print(total)

QUESTION 8

Write a while loop to display from 0 to 100, step up 5 each time.

0, 5, 10, 15, 20, ... , 100

Solutions

Expert Solution

Q1-
A while loop works as follows
there is a precondition which is checked before entering the loop, if that is true, then the while loop body is executed, then counter is increased for the variable the while loop is working, then again the precondition is checked and loop exits when the condition gets failed.

Q2- While statement is a loop which executed the body of while loop till the expression is true, whereas an if statement checks if an expression is true or false, and if it is true then it runs the code inside if block, and it is executed only once unlike while statement.

Q3-
A loop body is a group of statements which are executed on repeat basis until the loop condition gets false.

Q4-
value of num after the code is executed is 0 because at the start we have num=10 and then it enters while loop which decrements num by 1 in every iteration untill num>0 so when num becomes 0 it exits the loop so at the end of program num=0

Q5-value of num after code segment is executed is 1 as while condition is never true so while loop never executes.

Q6- Loop never terminates because the condition is n>0 which is always true because inside loop we are incrementing n by 1 in every iteration and at the start of loop n=10 so it will always be >0 which is why loop never terminates.

Q7-
for loop to while loop
total=0
i=0
while(i<10):
total=total+i
print total

Q8-
i=0
while(i<=100):
print i
i=i+5
this will print from 0 to 100 incrementing by 5 everytime.

if you like the answer please provide a thumbs up.


Related Solutions

1. Write Python program that use a while loop to determine how long it takes for...
1. Write Python program that use a while loop to determine how long it takes for an investment to double at a given interest rate. The input will be an annualized interest rate, and the output is the number of years it takes an investment to double. Note: The amount of initial investment can be any positive value, you can use $1. 2. Write a function def DoubleInvest(initialInvest) to perform the same functionalities as question1. Make sure to include you...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Design a Python program with a While loop that lets the user enter a number. The...
Design a Python program with a While loop that lets the user enter a number. The number should be multiplied by 10 and the result stored in a variable named "product". The loop should repeat as long as the product variable < 10. Anything not completely obvious to a newbie should be commented on as in line comments. Should be modulated and repeatable as well as there should be an invocation of the main routine at the end of the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the number is equal to 99, display "Ninety-nine", otherwise display "no-match". If the number is equal to 75, display "Seventy-five", otherwise display "no-match".
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
1. Relate the importance of keeping records. 2. Compare and contrast receipts and invoices. 3.Briefly describe...
1. Relate the importance of keeping records. 2. Compare and contrast receipts and invoices. 3.Briefly describe the difference between cash and accrual methods of record keeping. 4.Briefly discuss the fundamental purpose of accounting principles in effective recordkeeping. 5. Design a human resource management system for a very small employer with 10 employees or less. 6. The if–then construct is often used to describe a business process. For example, “if the person is a Member, then enrollment proceeds. “   This is...
Describe how a negative feedback loop works in biology.
Describe how a negative feedback loop works in biology.
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks students to enter 3 valid grades and then calculate the average and print it. if the user enters the invalid grade you should print the error message and get the new grade. Hint1: each time your program ask the following question: "Do you want to calculate another average?" and if the answer to this question is "Y" or "y" then you should continue. Hint2:...
Multiples of 2 and 3: write a c++ program Using a while loop, write a program...
Multiples of 2 and 3: write a c++ program Using a while loop, write a program that reads 10 integer numbers. The program shall count how many of them are multiples of 2, how many are multiples of 3, and how many are NOT multiples of either 2 or 3. The output should be similar to the one shown below.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT