Skip to content
Advertisement

While Loop No Longer Updating Data when added “Press Enter to Exit”


I am a beginner to Python and recently was making a **Discord Rich Prescense** application. The problem is that I was using a While Loop and added a “*Press Enter to Exit*” feature. This made the Rich Prescense stuck on One Quote. I have attached a screenshot of the problem.
from config import credentials
from data import quotes
from pypresence import Presence
import random
import time

def quotegen():
    RPC = Presence(credentials.clientid)
    RPC.connect()

    while True:
        RPC.update(details="Random Quote:", state=random.choice(quotes.quotes))
        i = input("Press Enter to Exit")
        time.sleep(30)
        if not i:
            break

Screenshot of what its supposed to do:

Screenshot

Advertisement

Answer

Using the keyboard module (https://pypi.org/project/keyboard/) you can do it all.

I modified your code to fit your requirements:

import keyboard  # using module keyboard
from config import credentials
from data import quotes
from pypresence import Presence
import random
import time

def quotegen():
    RPC = Presence(credentials.clientid)
    RPC.connect()

    while True:
        RPC.update(details="Random Quote:", state=random.choice(quotes.quotes))
        i = input("Press Enter to Exit")
        time.sleep(30)
        if keyboard.is_pressed('enter'):  # if key 'enter' is pressed 
            break  
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement