[Tutor] python help!

Ghadir Ghasemi ghasemmg01 at leedslearning.net
Sat Feb 9 20:04:58 CET 2013


Hi guys can you tell me what is wrong with the second part of this code(elif choice == '2'). When I type something other than 0-255, it correctly asks again for an input but when I enter a number from 0-255 it does nothing :

def show_menu():
 print("=======================")
 print("1-binary to denary")
 print("2-denary to binary")
 print("3-exit")
 print("=======================")


while True:
    show_menu()

    choice = input("please enter an option: ")

    if choice == '1':
        binary = input("Please enter a binary number: ")
        denary = 0
        place_value = 1

        for i in binary [::-1]:
                    denary += place_value * int(i)
                    place_value *= 2

        print("The result is",denary)
   

    elif choice == '2':
         denary2 = int(input("Please enter a denary number: "))
         remainder = ''
         while denary2 not in range(0,256):
             denary2 = int(input("Please enter a denary number: "))
             continue
         while denary2 in range(0,256):
             
            remainder = str(denary2 % 2) + remainder
            denary2 >>= 1
         print("The result is",remainder)
        
   
 

    elif choice == '3':
     break


    elif choice not in '1' or '2' or '3':
        print("Invalid input-try again!")


More information about the Tutor mailing list