Question: fix my code for me import random # Generate random bubble size def generate _ bubble _ size ( ) : return random.randint ( 0

fix my code for me
import random
# Generate random bubble size
def generate_bubble_size():
return random.randint(0,9)
# Displays ASCII characters instead of numbers
def display_bubbles(bubbles):
ascii_chars =['.',':',';','-','=','+','*','%','#','@']
display =''.join(ascii_chars[size] for size in bubbles)
print(display)
# Function to apply rules to update bubble sizes
def update_bubbles(bubbles):
new_bubbles = bubbles[:]
for b in range(len(bubbles)):
# Rule 1: If a bubble is size 9, it bursts and becomes size 0
if bubbles[b]==9:
new_bubbles[b]=0
# Rule 2: If a bubble is size 2, it stays the same size
if bubbles[b]==2:
new_bubbles[b]=2
# Rule 3: If a bubble is size 1-3, it has a 50% to grow by 1
elif 1<= bubbles[b]<=3:
if random.random()<0.5: # 50% chance to grow
new_bubbles[b]+=1
# Rule 4: If a bubble is size 6-8, it has a 20% chance to shrink by 1
if 6<= bubbles[b]<=8:
if random.random()<0.2: # 20% chance to shrink
new_bubbles[b]-=1
# Rule 5: If a bubble is adjacent to a size 9 bubble, it grows by 1
if (b >0 and bubbles[b-1]==9) or (b < len(bubbles)-1 and bubbles[b+1]==9):
new_bubbles[b]= min(new_bubbles[b]+1,9)
# Rule 6: If bubble is adjacent to a size 9 bubble in next gen, it shrinks by 1
if new_bubbles[b]==9:
bubbles[b]-=1
# Rule 7: If bubble is size 3, it has a 20% chance to become size 8
if bubbles[b]==3:
if random.random()<0.2: # 20% to become 8
new_bubbles[b]=8
return new_bubbles
# Input for number of bubbles and generations
def main():
while True:
num_bubbles = int(input("Enter the number of bubbles (0-20): "))
num_generations = int(input("Enter the number of generations (0-20): "))
if 0<= num_bubbles <=20 and 0<= num_generations <=20:
break
else:
print("Please enter valid numbers within range.")
# Initialize bubbles
bubbles =[generate_bubble_size() for _ in range(num_bubbles)]
# Simulate generations
for generation in range(num_generations):
print(f"Generation {generation +1}:")
display_bubbles(bubbles)
bubbles = update_bubbles(bubbles)
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!