The task of determining the rightmost digit of the binary record number

def binar(): b=int(input()) b=bin(b) num=[] num=b.split ln=len(b) g=int(b[ln-1]) if g==1: print ("Yes") elif g==0: print ("No") a=int(input()) for i in range(a): binar() 

Code as an image from the original question

Closed due to the fact that off-topic participants Kromster , Air , nick_n_a , Let's say Pie , Dmitry Kozlov 30 Oct '18 at 22:36 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, nick_n_a, let's say Pie
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Questions asking for help with debugging (“why does this code not work?”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. - Kromster
  • There are no problems, the code works, the question is whether it can be made shorter. Thanks for the answer. - Pasha
  • one
    The code should be given directly in the question, and not on third-party resources, and certainly not in pictures - Kromster
  • 2
    Also check out the rules for code inspection: www.stackoverflow.com/tags/inspection-code/info - Kromster
  • The second part of the question was asked precisely to solve this problem. (I asked how to do better, and you blame for not doing better. What is the point?) - Pasha

3 answers 3

If you need to determine the least significant digit of a number, then in my opinion, the easiest way is to find out the parity of the number. Obviously, all even numbers have the least significant digit 0, and odd numbers have 1

 a=int(input()) if a % 2 == 1: # нечётное print("Yes") else: # чётное print("No") 
  • 2
    Even shorter: print("Yes" if a % 2 else "No") - gil9red
  • print ("yes" if a% == 1 else "no")? - Pasha
  • @Pasha, this is SyntaxError: invalid syntax , and I'm SyntaxError: invalid syntax about print("Yes" if a % 2 else "No") , you can print("Yes" if a % 2 == 1 else "No") , but this is not shorter :) - gil9red
  • 3
    @Pasha in one line print("Yes" if int(input()) % 2 else "No") - Andrey

Without any checks if:

 num = 2 print("This number is"+" not"*(num&1)+" odd") 
    1. num=b.split is not a call to the split method, it is writing the method itself to the variable num . To call a function, method, or object with the ability to call ("callable") you need parentheses: num = b.split() . Be careful, the absence of brackets when calling can lead to errors. You do not have an error due to the fact that this value is then not used in any way (see clause 2).
    2. Dead code : you do not use the value of the variable num . Feel free to throw out two lines.
    3. If there is a string with a binary representation of a number, simply take the last character, and check if it is equal to the string "1" without converting the character to an integer. The last character can be obtained at index -1 (respectively, the penultimate - at index -2, etc.). If a symbol has two states ("1" or "0"), then if the symbol is not equal to "1", then it is automatically equal to "0", so you can simply use else instead of elseif .

       b=int(input()) b=bin(b) g=b[-1] if g=="1": print ("Yes") else: print ("No") 
    4. You can check the value of the low-order bit without converting a number to a string in a binary representation — via bitwise operations (see, for example, tproger.ru: About bitwise operations ):

       b = int(input()) if b & 1: print ("Yes") else: print ("No")