开发者

Syntax Error that I can't find

I'm pulling my hairs here...

import os
import random
MAC = (0,0,":",0,0,":")
chars 开发者_StackOverflow= ('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e')
a = 5
while (a <= 17):
  if a == 6 | a == 9 | a == 12 | a == 15:
    MAC.append(":")
  else:
    MAC.append(chars(random.randomint(0,15))

  a += 1
print MAC

What am I doing wrong?! The indentation is correct, just as the example shows. Any help is welcomed. EDIT: OH CRAP, forgot to show what the error is..

File "macchanger.py", line 12 a += 1 ^ SyntaxError: invalid syntax


You're looking for the logical or (or) and not the bitwise or (|).

Chars is not a function, but an array. Use array subscripts to get at values. (i.e. chars[rand] and not chars(rand).

You're missing a parenthesis after the append random char line.

You also can't append to tuples, need to make MAC a list.

The random module also has no 'randomint' function. You're looking for randint.


edit: This can also replace your entire function if you're looking to use Python's generators:

MAC = ':'.join(['%02x'%c for c in [0, 0]+[random.randint(0,255) for d in range(4)]])


Try

import os
import random

def makeMAC(pre=None):
    pre = [int(i) for i in pre] if pre else []
    for byte in range(len(pre), 6):
        pre.append(random.randint(0,255))
    return ':'.join('%02x'%(i) for i in pre)

print makeMAC([0,0])

returns

00:00:cd:c1:7d:c0


  1. missing closing parenthese in

    MAC.append(chars(random.randomint(0,15))

  2. MAC needs to be list since you're appending, tuples are immutable

  3. random.randint (and not random.randomint)


There are several issues:

  • The one the compiler tells you about - mismatches parens at the line above (as always, the exact error message would have helped)
  • You're using | (the bitwise or) when you want or (the logical or). Note that you can do this check better, by using a in <tuple of values>.
  • You can't .append to a tuple (or otherwise mutate it), use a list.
  • You should simply use for a in range(5, 18) instead of counting manually in a while loop.
  • You're calling the chars tuples - indexing uses square backets (chars[...]). Note that you just use random.choice.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜