有没有办法解决pylance错误?

is there a way to solve the pylance error?

我每次 运行 我的代码都会收到这个错误 ( "(" 未关闭 Pylance [4,9] ) 我真的找不到任何解决方案,请帮帮我 :( 我真的不知道该怎么做我刚开始学习 py,我真的很想继续学习所以请谁成为我的救星:)

import copy

story = (
    "I can't believe it's already {} ! " +
    "I can't wait to put on my {} and visit every {} in my nighborhood. " +
    "This year, I am going to dress up as (a) {} with {} {} . " +
    "Before I {} , I make sure to grab my {} {} to hold all of my {}. " +
    "Finally, all of my {} are ready to go ! " +
)


word_dict = {
    'Holiday noun':['Holiday'],
    'Noun':['cosplay','custom outfit','halloween clothes'],
    'Place nouns':['corner','place','street'],
    'Person':['Frankenstein','Princess','Queen','Cop'],
    'Adjective':['creepy','shiny','lovely'],
    'Body part (plural)':['arms','eye lashes','glasses'],
    'Verb':['go','go out','wolk out'],
    'Adjective':['little','big','colored','beautiful'],
    'Noun':['bag','handbag','carryall'],
    'Food':['candies','sweets','bonbons'],
    'Plural noun':['things','stuff','gear'],
}

def get_word(type, local_dict):
    words = local_dict[type]
    cnt = len(words)-1
    index = randint(0, cnt)
    return local_dict[type].pop(index)

def create_story():
    local_dict = copy.deepcopy(word_dict)
    return story.format(
        get_word('Holiday noun',local_dict),
        get_word('Noun',local_dict),
        get_word('Place nouns',local_dict),
        get_word('Person',local_dict),
        get_word('Adjective',local_dict),
        get_word('Body part (plural)',local_dict),
        get_word('Verb',local_dict),
        get_word('Adjective',local_dict),
        get_word('Noun',local_dict),
        get_word('Food',local_dict),
        get_word('Plural noun',local_dict),
    )

print("Story 1: ")
print(create_story())
print()
print("Story 2: ")
print(create_story()) ```
"Finally, all of my {} are ready to go ! " +

您需要删除该行末尾的 +

并且您将需要导入 random 以使用 randint 函数

from random import randint

如果你执行你的代码,它会告诉第一个 non-excepted 字符在哪里:

python .\nietchez.py
  File "C:\Users\nietchez\source\nietchez.py", line 9
    )
    ^
SyntaxError: invalid syntax

所以 ) 之前有问题。去掉+符号,同时,添加随机库:

import copy
from random import randint

story = (
    "I can't believe it's already {} ! " +
    "I can't wait to put on my {} and visit every {} in my nighborhood. " +
    "This year, I am going to dress up as (a) {} with {} {} . " +
    "Before I {} , I make sure to grab my {} {} to hold all of my {}. " +
    "Finally, all of my {} are ready to go ! "
)

此外,您甚至不需要 + 两行之间:

story = (
    "I can't believe it's already {} ! "
    "I can't wait to put on my {} and visit every {} in my nighborhood. "
    "This year, I am going to dress up as (a) {} with {} {} . 1"
    "Before I {} , I make sure to grab my {} {} to hold all of my {}. "
    "Finally, all of my {} are ready to go ! "
)

变量story的末尾有多余的+运算符,所以您必须将其删除。此外,您的代码不会从模块 random 导入 randint 函数,因此请在代码顶部添加 from random import random 字符串。