【问题标题】:is there a way to solve the pylance error?有没有办法解决pylance错误?
【发布时间】:2022-02-02 22:05:32
【问题描述】:

每次运行代码时都会出现此错误 (“(”未关闭 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()) ```

【问题讨论】:

  • 而不是randint,导入随机并写'random.randint',另外,你在'story'中还有一个额外的好处

标签: python random import generator pylance


【解决方案1】:

如果你执行你的代码,它会告诉你第一个非例外字符在哪里:

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 ! "
)

【讨论】:

【解决方案2】:
"Finally, all of my {} are ready to go ! " +

您需要删除该行末尾的 +

你需要导入 random 来使用 randint 函数

from random import randint

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 1970-01-01
      • 2020-12-13
      • 2021-06-03
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2021-06-05
      相关资源
      最近更新 更多