1 回答
TA贡献1789条经验 获得超10个赞
作为修复缩进之前的解决方案,请尝试rej在函数内部移动Reject():
def Reject():
rej = random.choice(["Hey", "Hello", "What's up"]) +", thanks for reaching out!
This " + random.choice(["idea", "concept", "project"]) + " was " \
+ random.choice(["unique,", "interesting,", "creative,", "solid,", "clever,"]) +
" although it doesn't quite fit our vision right now. " \
"We appreciate the submission and look forward to receiving more from you in the
future."
time.sleep(2)
keyboard.type(rej)
print(rej)
虽然这给了我一个 EOL 错误,但这与您发布的内容相同,或者您也可以这样说:
rej = random.choice(["Hey", "Hello", "What's up"]) +", thanks for reaching out! This " + random.choice(["idea", "concept", "project"]) + " was " + random.choice(["unique,", "interesting,", "creative,", "solid,", "clever,"]) + " although it doesn't quite fit our vision right now. We appreciate the submission and look forward to receiving more from you in the future."
有什么区别,它全部在一行中,而您在多行中使用,为此我建议使用三引号和f字符串来动态插入变量,例如:
def Reject():
rej = f'''{random.choice(["Hey", "Hello", "What's up"])}, thanks for reaching out!
This {random.choice(["idea", "concept", "project"])} was {random.choice(["unique,", "interesting,", "creative,", "solid,", "clever,"])} although it doesn't quite fit our vision right now.
We appreciate the submission and look forward to receiving more from you in the future.'''
time.sleep(2)
keyboard.type(rej)
print(rej)
从代码开始到正常代码块结束的所有内容都只会运行一次,而您的代码rej处于这一点之间,并且只会执行一次,因此这解释了为什么它在整个过程中保持不变mainloop(),所以为了使其在每次单击按钮时正确随机化,您必须将其移至函数内部。因此,每次调用该函数都会运行rej每次,使其随机。
添加回答
举报