f3ell0w
f3ell0w

Reputation: 105

Make the loop work

How can I make the loop work? When I give any input, for example, 1 or 2, nothing happens.

How can I solve this?

import os
while 1:
    os.system('cls')
    print("")
    print("1. Decimal to Binary")
    print("2. Binary to Decimal")
    print("3. Exit")
    choice = input('Input the number: ')
    if choice == "1":
        dec_to_bin()
    elif choice == "2":
        bin_to_dec()
    elif choice == "3":
        break;

def dec_to_bin():
    decimal = input('Input a number: ')
    a =  bin(decimal)[2:]
    print(a)

def bin_to_dec():
    binary = input('Input the binary: ')
    a = int('binary', 2)
    print(a)

Upvotes: 1

Views: 89

Answers (1)

NPE
NPE

Reputation: 500923

Since you are using Python 2, you need to change input() to raw_input(). When you enter 1 at the input() prompt, you get back an int, not a string.

Upvotes: 1

Related Questions