2013-03-04 1749 views
1

我正在使用python中的程序將任意數字基數轉換爲任意數字基數。我的代碼如下如果python中有錯誤,請求用戶重新輸入

num = raw_input("Please enter a number: ") 
base = int(raw_input("Please enter the base that your number is in: ")) 
convers = int(raw_input("Please enter the base that you would like to convert to: ")) 
if (64 < convert < 2 or 64 < base < 2): 
    print "Error! Base must be between 2 and 32." 
    sys.exit() 
symtodig = { 
    '0': 0, 
    '1': 1, 
    '2': 2, 
    '3': 3, 
    '4': 4, 
    '5': 5, 
    '6': 6, 
    '7': 7, 
    '8': 8, 
    '9': 9, 
    'A': 10, 
    'B': 11, 
    'C': 12, 
    'D': 13, 
    'E': 14, 
    'F': 15, 
    'G': 16, 
    'H': 17, 
    'I': 18, 
    'J': 19, 
    'K': 20, 
    'L': 21, 
    'M': 22, 
    'N': 23, 
    'O': 24, 
    'P': 25, 
    'Q': 26, 
    'R': 27, 
    'S': 28, 
    'T': 29, 
    'U': 30, 
    'V': 31, 
    'W': 32, 
    'X': 33, 
    'Y': 34, 
    'Z': 35, 
    'a': 36, 
    'b': 37, 
    'c': 38, 
    'd': 39, 
    'e': 40, 
    'f': 41, 
    'g': 42, 
    'h': 43, 
    'i': 44, 
    'j': 45, 
    'k': 46, 
    'l': 47, 
    'm': 48, 
    'n': 49, 
    'o': 50, 
    'p': 51, 
    'q': 52, 
    'r': 53, 
    's': 54, 
    't': 55, 
    'u': 56, 
    'v': 57, 
    'w': 58, 
    'x': 59, 
    'y': 60, 
    'z': 61, 
    '!': 62, 
    '"': 63, 
    '#': 64, 
    '$': 65, 
    '%': 66, 
    '&': 67, 
    "'": 68, 
    '(': 69, 
    ')': 70, 
    '*': 71, 
    '+': 72, 
    ',': 73, 
    '-': 74, 
    '.': 75, 
    '/': 76, 
    ':': 77, 
    ';': 78, 
    '<': 79, 
    '=': 80, 
    '>': 81, 
    '?': 82, 
    '@': 83, 
    '[': 84, 
    '\\': 85, 
    ']': 86, 
    '^': 87, 
    '_': 88, 
    '`': 89, 
    '{': 90, 
    '|': 91, 
    '}': 92, 
    '~': 93} 

digits = 0 
for character in num: 
assert character in symtodig, 'Found unknown character!' 
value = symtodig[character] 
assert value < base, 'Found digit outside base!' 
digits *= base 
digits += value 


digtosym = dict(map(reversed, symtodig.items())) 

array = [] 
    while digits: 
    digits, value = divmod(digits, convers) 
    array.append(digtosym[value]) 
answer = ''.join(reversed(array)) 


print "Your number in base", base, "is: ", answer 

我的代碼的偉大工程,但是,而是採用

if (64 < convert < 2 or 64 < base < 2): 
print "Error! Base must be between 2 and 32." 
sys.exit() 

ID喜歡帶他們回輸入的基礎變量,而不是退出程序。 我是新來的python,並沒有線索如何做這樣的事情。請幫忙。

回答

2
while True: 
    try: 
     convert = int(raw_input('Convert: ')) 
     base = int(raw_input('Base: ')) 
     if (64 < convert < 2 or 64 < base < 2): 
      print "Error! Base must be between 2 and 32." 
      continue 
     break 
    except Exception, e: 
     print '>> Error: %s' % e 
+0

我會把INT在try /除了還... – 2013-03-04 19:03:18

+0

謝謝,這很有幫助 – chadybear 2013-03-05 00:43:16

+0

非常歡迎。 – 2013-03-05 10:49:20

1

的通用模板:

while True: 
    # get user input 
    # ... 
    if valid_input(): 
     break 

例如:

def get_int(prompt, lo=None, hi=None): 
    while True: 
     try: 
      n = int(raw_input(prompt)) 
     except ValueError: 
      print("expected integer") 
     else: # got integer, check range 
      if ((lo is None or n >= lo) and 
       (hi is None or n <= hi)): 
       break # valid 
      print("integer is not in range") 
    return n 
0

一個簡單的辦法是簡單地在選擇的開頭添加一個循環:它逃脫的情況下,一個有效的選擇,並且在它不正確的情況下進行迭代(而不是sys.exit(),我相信你想避免這一點)。

第一部分可能是:

selected = False 
while not selected: 
    num = raw_input("Please enter a number: ") 
    base = int(raw_input("Please enter the base that your number is in: ")) 
    convert = int(raw_input("Please enter the base that you would like to convert to: ")) 
    if (64 < convert or convert < 2 or 64 < base or base < 2): 
     print "Error! Base must be between 2 and 32." 

    else: 
     selected = True 

另外,請注意,我改變了條件:

if (64 < convert or convert < 2 or 64 < base or base < 2): 
+0

非常感謝!是的,我注意到了。我已將它設置爲32,因爲我的教練要求基礎轉換在2到32公頃之間。 – chadybear 2013-03-05 00:47:50

相關問題