2013-03-30 74 views
1

如何使循环正常工作?当我给出任何输入时,例如1或2,没有任何反应。使循环正常工作

我该如何解决这个问题?

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) 
+2

你使用的是什么版本的Python? – Blender

+1

这不是你的主要问题,但'a = int('binary',2)'应该是'a = int(binary,2)'(不带引号)。你正在解析一个变量,而不是字符串文字。 – ApproachingDarknessFish

+0

我使用的是2.7.3版本 – f3ell0w

回答

1

由于您使用Python 2,您需要更改input()raw_input()。当您在input()提示处输入1时,将返回int,而不是字符串。