[Tutor] Additional help

Ghadir Ghasemi ghasemmg01 at leedslearning.net
Sun Feb 10 21:29:23 CET 2013


Hi guys, I wondered if you knew what I could add to this code so that when the user enters 1 from the menu and then doesn't enter a valid binary number the program should ask them over and over again until valid binary number is entered.
here is the code:

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 > 0:
            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!")


Thanks


More information about the Tutor mailing list