189k views
0 votes
Write an expression that continues to bid until the user enters 'n'.

Sample output with inputs: 'y' 'y' 'n'
I'll bid $7!
Continue bidding? I'll bid $15!
Continue bidding? I'll bid $23!
Continue bidding?
1 import random
2 random.seed (5)
3
4 keep going
5 next bid = 0
6
7 while Your solution goes here
8 next bid next bid + random.randint(1, 10)
9 print(' ll bid x ' (next bid))
10 print('cohtinue bidding?', end-
11 keep going input
ACTIVITY 53.3. While loop: Insect growth
Given positive integer num_insects, write a while loop that prints that number doubled up to, but without exceeding 100. Follow each number with a space.
Sample output with input:
8 8 16 32 64 1 nu
1 insects = int(input()) Must be
2
3 Your solution goes here

User Farvardin
by
4.6k points

1 Answer

4 votes

Answer:

1)

Add this while statement to the code:

while(keep_going!='n'):

#the program keeps asking user to continue bidding until the user enter 'n' to stop.

2)

Here is the code for ACTIVITY 53.3. While loop: Insect growth:

num_insects = int(input())

while num_insects <= 100:

print(num_insects, end=' ')

num_insects = num_insects * 2

Step-by-step explanation:

Here is the complete code for 1)

import random

random.seed (5)

keep_going='-'

next_bid = 0

while(keep_going!='n'):

next_bid = next_bid + random.randint(1, 10)

print('I\'ll bid $%d!' % (next_bid))

print('continue bidding?', end='')

keep_going = input()

Here the the variable keep_going is initialized to a character dash keep_going='-'

The statement keep_going = input() has an input() function which is used to take input from user and this input value entered by user is stored in keep_going variable. while(keep_going!='n'): is a while loop that keeps iterating until keep_going is not equal to 'n'. print('continue bidding?', end='') statement prints the continue bidding? message on the output screen. For example the user enters 'y' when this message appears on screen. So keep_going = 'y' . So the operation in this statement next_bid = next_bid + random.randint(1, 10) is executed in which next_bid is added to some randomly generated integer within the range of 1 to 10 and print('I\'ll bid $%d!' % (next_bid)) prints the result on the screen. Then the user is prompted again to continue biffing. Lets say user enters 'n' this time. So keep_going = 'n'. Now the while loop breaks when user enters 'n' and the program ends.

2)

num_insects = int(input()) #the user is prompted to input an integer

while num_insects <= 100: #the loop keeps iterating until value of num_insects exceeds 100

print(num_insects, end=' ') #prints the value of num_insects

num_insects = num_insects * 2 #the value of num_insects is doubled

For example user enters 8. So

num_insects = 8

Now the while loop checks if this value is less than or equal to 100. This is true because 8 is less than 100. So the body of while loop executes:

print(num_insects, end=' ') statement prints the value of num_insects

So 8 is printed on the screen.

num_insects = num_insects * 2 statement doubles the value of num_insects So this becomes:

num_insects = 8 * 2

num_insects = 16

Now while loop checks if 16 is less than 100 which is again true so next 16 is printed on the output screen and doubled as:

num_insects = 16 * 2

num_insects = 32

Now while loop checks if 32 is less than 100 which is again true so next 32 is printed on the output screen and doubled as:

num_insects = 32 * 2

num_insects = 64

Now while loop checks if 64 is less than 100 which is again true so next 64 is printed on the output screen and doubled as:

num_insects = 64 * 2

num_insects = 128

Now while loop checks if 128 is less than 100 which is false so the program stops and the output is:

8 16 32 64

The programs along with their output is attached.

Write an expression that continues to bid until the user enters 'n'. Sample output-example-1
Write an expression that continues to bid until the user enters 'n'. Sample output-example-2
User Niran
by
5.2k points