我想同时分配2个变量,它们在从另一个文件导入的函数中

时间:2019-06-13 11:05:42

标签: python python-3.x syntax-error python-3.7

我想同时分配2个变量,它们在从另一个文件导入的函数中。

THIS IS THIS FUNCTION
def counter(init=[0]):
    init[0] += 1
    return init[0]
_____________________________________________
THIS IS ANOTHER SCRIPT


from Include.new_var import counter

first_letter = []

second_letter = []


def license_plate():
    global first_letter, second_letter

    characters = string.ascii_uppercase

    prices_for_letters = dict(zip(characters, range(1, len(characters) + 1)))

    print(prices_for_letters)

实际结果:

counter(first_letter), counter(second_letter) = random.choice(list(prices_for_letters.items()))
    ^
SyntaxError: can't assign to function call

预期结果:

new variable

我想自动生成新的变量,该变量将存储随机选择的值。

2 个答案:

答案 0 :(得分:1)

请问您以后打算做什么?在这里,您只是尝试调用该函数两次。

也请提供预期的结果,等等。

但是:

  1. 首先,选择项只会返回一项-您希望获得两项。
  2. 第二,只需将choice语句放入函数调用中:
counter(random.choice(list(prices_for_letters.items())))
counter(random.choice(list(prices_for_letters.items()))) 

答案 1 :(得分:1)

Assumptions:
1) Your function name seems to imply that you want to create a license plate.
2) Going through your profile, I see that you are from Poland so i assume you are trying to create a random license generator for vehicles in Poland.
3) A quick google search reveals that Poland license plates are 7 characters long with 2 or 3 initial chars followed by numbers.

def counter(some_char, draft_plate= []):
    draft_plate.append(str(some_char))
    return draft_plate

def license_plate():
    final_license_plate = []
    characters = string.ascii_uppercase
    numbers = range(0,10)
    prices_for_letters = dict(zip(characters, range(1, len(characters) + 1)))
    prices_for_numbers = dict(zip(numbers, range(1, len(numbers)+1)))
    numofchars = random.randint(2,3)
    for i in range(0,numofchars):
        counter(random.choice(list(prices_for_letters.keys())), final_license_plate)
    final_license_plate.append(' ')
    for j in range(numofchars,7):
        counter(random.choice(list(prices_for_numbers.keys())), final_license_plate)
    print(final_license_plate)

license_plate()