Share your repls and programming experiences

← Back to all posts
IS IT DIVISIBLE
Uzalii (423)

This will tell you if the number you enter is divisible by 1-15 this took me some time so i would really appreciate people to upvote me. Enjoy!

Comments
hotnewtop
[deleted]

Simplified code:

x = int(input("Enter in a number"))
for i in range(15)
  if x % i == 0:
    print("Your number " + str(x) + " is divisible by " + str(i))
  else:
    print("Your number " + str(x) + " is NOT divisible by " + str(i))
Uzalii (423)

@TaylorLiang Thx bro would've been nice if i new this before making this code but it will help in the future.

MatthewDoan1 (333)

@TaylorLiang
Super simplified:

n = int(input("Enter in a number"))
for i in range(15):
    if n % i:
        print("Your number " + str(n) + " is NOT divisible by " + str(i))
    else:
        print("Your number " + str(n) + " is divisible by " + str(i))
Uzalii (423)

Thx bro this is a very helpful suggestion I will try it in future thx for the support

[deleted]

@MatthewDoan1 wait, but if n is divisible by i, then it will return 0. You have to do if not n%i

sugarfi (630)

@TaylorLiang More simplified

n = int(input('Type a number '))
[print(f'Your number {n} is {"NOT " * bool(n % i)}divisible by {i}') for i in range(15)]
[deleted]

@sugarfi oh god not code golf.

sugarfi (630)

@TaylorLiang code golf is my favorite sport

[deleted]

@sugarfi opposite code golf is mine

sugarfi (630)

@TaylorLiang that is called code bowling

[deleted]

@sugarfi :0
ive made a text editor in 1 line before, is that code bowling or code golf?

MatthewDoan1 (333)

@TaylorLiang Oops haven't done python in a while

[deleted]

@MatthewDoan1 bruh in almost every language 0 is false and 1 is true

sugarfi (630)

@TaylorLiang golf, because you removed all the unecssary spaces and newlines that make your code readable and pretty.

sugarfi (630)

@TaylorLiang it would only be bowling if you made it in, say, 100000 lines, and made it so you could not shorten it at all or it would break.

MatthewDoan1 (333)

@TaylorLiang Bruh when I typed that i was half asleep and couldn't think straight (and I lke corrected it 4+ times), and plus I use the one language where that isn't true (ahem Java)

[deleted]

@MatthewDoan1 java, more like JOwO

ChezCoder (1605)

enter 1307674368000 in the number input :P

also, you should validate user input. If the user entered a non int (not a number) or a float (decimal) it returns an error.
Here is how its done:

while True:
    try:
        x = int(input("Enter ANY number and this will tell you if it is divisible by 1 -15 REMEMBER TO UPVOTE: "))
        break
    except:
        print("Please enter a number!")
Uzalii (423)

Thx bro this will help a lot in future

Unorthodox (13)

It's pretty cool! I would suggest using a for loop instead of nearly the same statement pasted a bunch of times. You could also skip most of the composite numbers from 1-15, other than 2, 3, and 5. You don't have to do an if statement for 1.

Uzalii (423)

@Unorthodox I realised that if statement for 1 too and it is a bit irrevelant thx for the suggestion on how to make the code better. I will try it in future

[deleted]

just print it is divisible by 1

Uzalii (423)

@Thecrowbar1234 True but i was copying and pasting so i used the first one i wrote to make them all.

Uzalii (423)

Plus this makes it more accurate

mahcarze1 (19)

this is a bit cool, but can you put all of what it is dividable by on 1 line?

Uzalii (423)

You can use it as a minor calculator if u want

Uzalii (423)

Hope you guys enjoy this

Uzalii (423)

i rly apreciate it

Uzalii (423)

And thx for the upvote

Uzalii (423)

and it works with any number if u wanna try it out