Share your repls and programming experiences

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

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!

Commentshotnewtop
roylatgnail (840)

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 (429)

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

MatthewDoan1 (334)

@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 (429)

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

roylatgnail (840)

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

sugarfi (481)

@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)]
sugarfi (481)

@TaylorLiang code golf is my favorite sport

roylatgnail (840)

@sugarfi opposite code golf is mine

sugarfi (481)

@TaylorLiang that is called code bowling

roylatgnail (840)

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

MatthewDoan1 (334)

@TaylorLiang Oops haven't done python in a while

roylatgnail (840)

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

sugarfi (481)

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

sugarfi (481)

@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 (334)

@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)

ChezCoder (1199)

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 (429)

Thx bro this will help a lot in future

ipastrano (204)

Hi! I looked at your code, I think that you could have used a loop to test more numbers like this(i don't know python, so I will do pseudocode

loop until you reach the number
  If number mod index equals 0
     print divisible by index
else
print not divisible by index
Uzalii (429)

I will try this in future. Thx for the support

ipastrano (204)

@Uzalii :D Pseudocode isn't a language, it's just a way to map out your code. :D

Uzalii (429)

what are u talking about ;)

RomanFlorintsev (13)

@Uzalii LOL, here
https://en.wikipedia.org/wiki/Pseudocode
Pseudocode isn't python, it's just planning code out.

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 (429)

@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

Thecrowbar1234 (143)

just print it is divisible by 1

Uzalii (429)

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

Uzalii (429)

Plus this makes it more accurate

mahcarze1 (18)

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

Uzalii (429)

You can use it as a minor calculator if u want

Uzalii (429)

Hope you guys enjoy this

Uzalii (429)

i rly apreciate it

Uzalii (429)

And thx for the upvote

Uzalii (429)

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