History, asked by pshikha373, 6 months ago

We can also generate a random number from a specified range by using the
the help of this block.
Place the blocks in the Script area as
shown in Figure 9.5 and change the
values.
forever
SO
for 0.5 secs
Say
pick random
Click on the block and you will see the
sprite speaking random numbers
between 1 to 50.
pack random 1 to 10 DO
from the Operators block category. Let us see how the sprite itself chooses the random numbers will
Figure 9.5: Script for Sprite to Say Random Numbers​

Answers

Answered by Anonymous
1

Answer:

Write a python statement(s) that prints a random integer between 9 and 15 (including 9 and 15).

There are a couple of ways to do this one.

import random

# 1) use randrange but remember that you must use an upper

# limit that is one more than the highest random number you

# want to generate

print random.randrange(9,16)

# 2) Use random() and generate a number between 0 and 1

# you are multiplying by 7 becuase you need to generate seven

# different values (9,10,11,12,13,14,15). This gets us a number

# between 0 and 6, and then we need to add 9 to "shift"

# the numbers down the number line.

print int(random.random() * 8) + 9

Write a python program that prints the average of the red, green, and blue, values of the pixel at coordinate (37, 49) in an image named image.jpg.

Here's one solution using sunset.jpg from the gallery.

import pygame

pic = pygame.image.load("/home/stlawu/ehar/t/Harcourt/gallery/sunset.jpg")

pixel = pic.get_at((37,49))

avg = (pixel.r + pixel.g + pixel.b)/3.0

print avg

Write a python program that prints the total number of pixels in an image named image.jpg.

Here's a solution using sunset.jpg from the gallery.

import pygame

pic = pygame.image.load("/home/stlawu/ehar/t/Harcourt/gallery/sunset.jpg")

print pic.get_width() * pic.get_height()

What is the output of the following python program? First try and predict the output without the computer. Check your answer by typing it in and running it.

for i in range(1,6):

for j in range(i):

print "*",

print

Similar questions