-1

I am trying to determine if the input number is divisible by 2 or 3 or by both or not divisible by any one of them. The problem when the input is 0, it's supposed divisible by both, but when I enter zero I get nothing.

b=float(input("enter the number to check: "))
while b :
 if b%2==0 and b%3==0 :
    print("this number divisable by 2 and 3")
    b=float(input("enter the number to check: "))
 elif b%2==0 and b%3!=0 :
    print("this number divisable by 2 only")
    b=float(input("enter the number to check: "))
 elif b%3==0 and b%2!=0 :
    print("this number divisable by 3 only")
    b=float(input("enter the number to check: "))
 else :
    print("it's not divisable by any one of 2 and 3")
    b=float(input("enter the number to check: ")) 
khelwood
  • 55,782
  • 14
  • 81
  • 108

2 Answers2

0

Use different variable instead of b. While Loop doesnt work when you set the b is 0.

Try it, it may help.

b=float(input("enter the number to check: "))
while True:
 if b%2==0 and b%3==0 :
    print("this number divisable by 2 and 3")
    b=float(input("enter the number to check: "))
 elif b%2==0 and b%3!=0 :
    print("this number divisable by 2 only")
    b=float(input("enter the number to check: "))
 elif b%3==0 and b%2!=0 :
    print("this number divisable by 3 only")
    b=float(input("enter the number to check: "))
 else :
    print("it's not divisable by any one of 2 and 3")
    b=float(input("enter the number to check: ")) 
fatih_koca
  • 68
  • 1
  • 9
0

Fast answer:

Now it goes like this: enter image description here

Just to check any number (including 0 and 0.0) is divisible by 2 and/or 3, you should remove your 'while b:' instruction. But I believe you entered these inputs to check numbers in loop... So you want to exit if input is empty, but that will raise error during converting to float. So, assuming minimal effort to improve this, my advice is to approach that this way:

  b=input("enter the number to check: ")
  while str(b): #this will check if your number as string is missing
    b = float(b)
    if b%2==0 and b%3==0 :
      print("this number divisable by 2 and 3")
      b=input("enter the number to check: ")
    elif b%2==0 and b%3!=0 :
      print("this number divisable by 2 only")
      b=input("enter the number to check: ")
    elif b%3==0 and b%2!=0 :
      print("this number divisable by 3 only")
      b=input("enter the number to check: ")
    else:
      print("it's not divisable by any one of 2 and 3")
      b=input("enter the number to check: ")

Of course, there are a lot other ways how you can improve it...

First just ask once for input if that happens for every if statement, just right after it ends, and show for yourself when condition failed and program ended correctly:

  b=input("enter the number to check: ")
  while str(b): # You want 
    b = float(b)
    if b%2==0 and b%3==0 :
      print("this number divisable by 2 and 3")
    elif b%2==0 and b%3!=0 :
      print("this number divisable by 2 only")
    elif b%3==0 and b%2!=0 :
      print("this number divisable by 3 only")
    else:
      print("it's not divisable by any one of 2 and 3")
    b=input("enter the number to check: ")
  print("program ended")

There are possible further improvements, like optimization of statements, checking results without double calculations, byte flag check for division by 2, sum of str for big number divided by 3 and so on, if you're interested how I would approach it, feel free to make request in comment :)

@Edit: As 'requested' in comments by @MartijnPjeters, more valid code I would consider as valid, even when there the slowest component there is user requested for input :P

  while True:
    try:
      b = int(input("enter the number to check: "))
    except ValueError:  #Alternative: b=input... b.isdigit() then int(b)
      print("That is not integer, exiting loop")
      break
    divisible_by_2 = b^1
    divisible_by_3 = not b%3
    if divisible_by_2 and divisible_by_3:
      print("this number divisable by 2 and 3")
    elif divisible_by_2:
      print("this number divisable by 2 only")
    elif divisible_by_3:
      print("this number divisable by 3 only")
    else:
      print("it's not divisable by any one of 2 and 3")
    
  print("program ended")
Guaz
  • 34
  • 2
  • `input()` returns a string, always, so there is no point in using `str(b)` here. – Martijn Pieters May 10 '22 at 09:00
  • 1
    Also redundant: testing for `and b%3!=0` and `and b%2!=0` in the second and third branches of the `if ... elif ... else` decision tree. If the first branch (`b%2==0 and b%3==0`) is false, you already *know* that at least one or the other is not true. Just use `if b % 2 == 0 and b % 3 == 0: ....`, `elif b % 2 == 0: ....`', `elif b % 3 == 0: ...`, `else: ....`. – Martijn Pieters May 10 '22 at 09:02
  • Finally: I'd use `while True:`, and *at the top of the loop* put `b = input("Enter the number to check: ")`, then use `if not b: break` to exit the loop. That way you only need to use a single `input()` line. Also see [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658) – Martijn Pieters May 10 '22 at 09:05
  • i also used this while b and b=0 : and it's working – mahmoud monir May 10 '22 at 13:17
  • but i didn't understand theoritcal base of python of dealing with zero in while b: as a boolen logic and accept numbers >1 which suppose don't exist in boolen logic – mahmoud monir May 10 '22 at 13:19
  • @mahmoudmonir: see [Boolean truth testing](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) in the Python documentation for the standard types. Number types equal to zero are false, and so are empty containers, the `None` singleton object and `False`. – Martijn Pieters May 10 '22 at 14:29
  • @mahmoudmonir Actually it isn't mapping of numbers > 1 are equal to False, I would say numbers != 0 equals to True, so only number == 0 is False... Try and type 0.5 for example. There are some kind of objects which are converted by default to False, some of these are: `0`; `0.0`; `empty container (list, set, dictionary)`; `False (itself)`; – Guaz May 11 '22 at 04:19
  • @MartijnPieters Yea, I am aware of that, as mentioned `,,Of course, there are a lot other ways how you can improve it...''`. Where `str(b)` is just meant to show what we will be testing in while statement in obvious way :) – Guaz May 11 '22 at 04:24