In: Computer Science
(+30) Write a python program that generates four random between -50 and +50 using python’s random number generator (RNG) see the following code (modify as needed ) import random a = 10 # FIX b =100 # FIX x = random.randint(a,b) # (1) returns an integer a <= x <= b # modify a and b according to the lab requirement print('random integer == ', x) and displays 1. the four integers 2. The maximum integer 3. The minimum integer 4. The number of even integers (if x %2 == 0 then x is even) 5. The number of odd integers 6. The number of integers greater than -25 but less than 25 7. The number of positive integers 8. The number of negative integers 9. The average of the smallest and largest integers 10. The average of all four integers EXAMPLE OUTPUT If the RNG generated the four integers 22 -8 17 -5 then display 1. The integers are 22 -8 17 -5 2. The maximum integer is 22 3. The minimum integer is -8 4. The number of even integers is 2 5. The number of odd integers is 2 6. The number of integers greater than -25 but less than 25 is 4 7. The number of positive integers 2 8. The number of negative integers 2 9. The average of the smallest and largest integers 7.0 10. The average of the four integers is 6.5
my work: # importing "random" for random operations import random # importing mean() from statistics import mean a=random.randint(-50,50) b=random.randint(-50,50) c=random.randint(-50,50) d=random.randint(-50,50) print("1st random number is "+str(a)) print("2nd random number is "+str(b)) print("3rd random number is "+str(c)) print("4th random number is "+str(d)) list1=[a,b,c,d] print("The maximum integer: ",max(list1)) print("The minimum integer: ",min(list1)) odds = [] # list to keep track of odd evens = [] # list to keep track of evens # even and odds calculations if a % 2 == 0: evens.append(a) else: odds.append(a) if b % 2 == 0: evens.append(b) else: odds.append(b) if c % 2 == 0: evens.append(c) else: odds.append(c) if d % 2 == 0: evens.append(d) else: odds.append(d) print ("evens are: " , evens) print ("odds are: " , odds) number1=[] if a > -25 & a < 25: number1.append(a) if b > -25 & b < 25: number1.append(b) if c > -25 & c < 25: number1.append(c) if d > -25 & d < 25: number1.append(d) print("The number of integers greater than -25 but less than 25: ", number1) positive = [] negtive = [] if a >= 0: positive.append(a) else: negtive.append(a) if b >= 0: positive.append(b) else: negtive.append(b) if c >= 0: positive.append(c) else: negtive.append(c) if d >= 0: positive.append(d) else: negtive.append(d) print("positive integers are: ", positive) print("negative integers are: ", negtive) avg_s_m = (max(list1) + min(list1)) / 2 print("average of the smallest and largest integers: ", avg_s_m) avg = mean(list1) print("average of all four integers: ", avg)
getting error. line 20 evens.append(a) ^ IndentationError: expected an indented block. please help
if you have any doubts, please give me comment...
# importing "random" for random operations
import random
# importing mean()
from statistics import mean
a = -50
b = 50
list1 = []
for i in range(4):
num = random.randint(a, b)
list1.append(num)
print("The integers are "+ ' '.join(map(str, list1)))
minimum = min(list1)
maximum = max(list1)
print("The maximum integer is", minimum)
print("The minimum integer is", maximum)
even_count = 0
odd_count = 0
for num in list1:
if num%2==0:
even_count += 1
else:
odd_count += 1
print("The number of even integers is",even_count)
print("The number of odd integers is",odd_count)
count_range = 0
for num in list1:
if num>-25 and num<25:
count_range += 1
print("The number of integers greater than -25 but less than 25 is", count_range)
positive = 0
negative = 0
for num in list1:
if num>=0:
positive += 1
else:
negative += 1
print("The number of positive integers", positive)
print("The number of negative integers", negative)
print("The average of the smallest and largest integers", ((minimum+maximum)/2))
print("The average of the four integers is",mean(list1))
Let me know if you have any errors/clarifications. Thank you