当前位置: 动力学知识库 > 问答 > 编程问答 >

python 2.7 - Removing "None" from the output. Print hexadecimal digits from 0-F

问题描述:

def hex_numb(a): # to print or create a string of hex numbers frm 0-F

 for a in range (a,16):

b=hex(a)

b=b[2]

b=b.upper()

a+=1

print b

Returns 0-F all 16 digits and None after F

网友答案:

A couple of comments:

  1. Why is there an argument (a) to this function? If you always want 0-F, remove the argument, and if you want to specify the value to be returned lose the for loop.
  2. When using a for x in range(n) loop, you don't need to manually increment x (i.e. x += 1) inside the loop.
  3. Your various string manipulations can be done in one go: b = hex(a)[2].upper().
  4. Your function is returning None implicitly (standard Python behaviour when your function doesn't explicitly return), so if you print hex_numb(0) you run the function, printing all the values, then print the return value, None. All of the printing work is done inside the function, just call it: hex_numb(0).
分享给朋友:
您可能感兴趣的文章:
随机阅读: