[英]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
如果你执行你的代码,它会告诉你第一个非例外字符在哪里:
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
function ,因此在代码顶部添加from random import random
string 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.