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每次,使其隨機。
添加回答
舉報