16.6k views
2 votes
Short Python Code for 20 pts.

Get a positive integer from the user, and check to see if that number is or is not divisible by 5 random integers (from 1 - 10). If the user typed in a negative number - do nothing, however if they did type a positive number, report back to the user if their number is divisible by the 5 random numbers generated.

**HINT: You need to use modulus (%) for the project**

Example output of program:

10 is divisible by 2

10 is not divisible by 6

10 is not divisible by 4

10 is divisible by 2

10 is divisible by 1

User Tyriek
by
7.0k points

1 Answer

0 votes

import random

# get user input

num = int(input("Enter a positive integer: "))

# check if user input is positive

if num > 0:

# generate 5 random numbers between 1 and 10

rand_nums = random.sample(range(1, 11), 5)

# check if num is divisible by each of the random numbers

for i in rand_nums:

if num % i == 0:

print(f"{num} is divisible by {i}")

else:

print(f"{num} is not divisible by {i}")

This code prompts the user to enter a positive integer, and then checks if the input is greater than 0. If it is, it generates 5 random numbers between 1 and 10 using the 'random.sample()' function. It then uses a for loop to iterate through the list of random numbers, and for each number, it checks if the user's input is divisible by the current number by using the modulus operator ('%'). If the input is divisible by the current number, it prints a message saying so, otherwise, it prints a message saying the input is not divisible by the current number.

User Tdpu
by
8.1k points