
xxxxxxxxxx
#How Many Sweets in the Jar - www.101computing.net/how-many-sweets-in-the-jar/
import turtle
import random
numberOfSweets = random.randint(30,100)
myPen = turtle.Turtle()
myPen.hideturtle()
myPen.speed(0)
window = turtle.Screen()
window.bgcolor("#69C5FF")
#Draw Jar
myPen.penup()
myPen.goto(-120,100)
myPen.pensize(4)
myPen.color("#000000")
myPen.pendown()
myPen.goto(-100,100)
myPen.goto(-100,-100)
myPen.goto(100,-100)
myPen.goto(100,100)
myPen.goto(120,100)
#Add Sweets
for sweet in range(0,numberOfSweets):
x = random.randint(-85,85)
y = random.randint(-95,80)
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
myPen.penup()
myPen.goto(x,y)
myPen.pendown()
myPen.fillcolor(color)
myPen.color(color)
myPen.begin_fill()
myPen.circle(10)
myPen.end_fill()
#Collect guesses from different users
guesses=[]
while True:
name=input("Enter your name (or 'x' to exit):")
if name=="x":
break
else:
number = int(input("How many sweets in the jar?"))
guesses.append([name,number])
#Print all guesses:
print ("----- List of guesses -----")
for guess in guesses:
print(guess[0] + ": " + str(guess[1]))
#Print actual numbe rof sweets in the jar
print ("----- Actual number of sweets in the jar -----")
print(str(numberOfSweets) + " sweets")
#Find out who has the closest guess and display their name
print ("----- And the nearest guess is -----")
#Complete the code here to find out who has the nearest guess
#...
task_alt