I want to create a function in which system takes a random number from the given range and tell us about it's properties like odd-even number, prime-composite number, positive-negative number. It would be good if it's in Python3
I have although created function but somehow its showing some errors during compiling
import random
A = eval(input("Enter the Range from: "))
B = eval(input("Enter the Range to: "))
num = random.randint(A,B)
if num>=1:
print(num,"is positive number")
if num%2==0:
print(num,"is even number")
else:
print(num,"is odd number")
if num >= 1:
for i in range(2,num 1):
if num%2i==0:
print(num,"is a composite number")
break
else:
print(num,"is a prime number")
elif num==0 or num==1:
print(num,"is neither prime nor composite")
else:
print(num,"is a prime number")
elif num==0:
print(num,"is neither positive nor negative")
print(num,"is neither even nor odd")
print(num,"is neither prime nor composite")
else:
print(num,"is negative number")
CodePudding user response:
try this:
import random
A = int(input("Enter the Range from: "))
B = int(input("Enter the Range to: "))
def number_properties(start, end):
# Generate a random number from the given range
number = random.randint(start, end)
# Check if the number is odd or even
if number % 2 == 0:
odd_even = "even"
else:
odd_even = "odd"
# Check if the number is prime or composite
if number < 2:
prime_composite = "neither prime nor composite"
elif number == 2:
prime_composite = "prime"
else:
for i in range(2, number):
if number % i == 0:
prime_composite = "composite"
break
else:
prime_composite = "prime"
# Check if the number is positive or negative
if number < 0:
positive_negative = "negative"
elif number == 0:
positive_negative = "neither positive nor negative"
else:
positive_negative = "positive"
# Print the properties of the number
print(f"The number {number} is {odd_even}, {prime_composite}, and {positive_negative}.")
number_properties(A, B)
CodePudding user response:
When you check composite number, the syntax is incorrect because of 2i. You probably just want to put i like this.
if num >= 1:
for i in range(2,num 1):
if num%i==0:
print(num,"is a composite number")
break
else:
print(num,"is a prime number")