u/duelBooleans

string.translate(str.maketrans) not working to take out punctuation?

greeting = ["Hello, my name is Caine! What's your name?: ".upper(), "Welcome, I am Caine! What's your name?: ".upper(),
        "Hi!! I'm Caine! What should I call you?: ".upper()]


name = input(random.choice(greeting)).title()


nameStripped = name.translate(str.maketrans('', '', string.punctuation))


for i in nameList:
    if i in nameStripped:
        userName = i 


justNameWelcome = [f"Nice to meet you, {userName}!".upper(), f"{userName}... That's a wonderful name!".upper(),
              f"I've always thought that {userName} is a good name!".upper()]
ntmyNameWelcome = [f"It's nice to meet you too, {userName}!".upper(), f"The sentiment is mutual, {userName}!".upper(),
                f"I appreciate the sentiment, {userName}!".upper()]
hruNameWelcome = [f"I'm doing well, {userName}! Thank you for asking!".upper(), f"I appreciate you asking! Thank you, {userName}, I'm doing well!".upper(),]


niceToMeetYou = ["Nice To Meet You", "Pleasure To Meet You"]
howAreYou = ["How Are You", "How Are You Doing", "Are You Well"]


if len(name.split()) > 1:
    if nameStripped in howAreYou:
        print(random.choice(hruNameWelcome))
    elif nameStripped in niceToMeetYou:
        print(random.choice(ntmyNameWelcome))
    else:
        print(random.choice(misunderstandings))


elif len(name.split()) == 1:
    print(random.choice(justNameWelcome))


else:
    print(random.choice(blankInputs))
    nameStripped = input(random.choice(greeting)).title()

Thank you in advance, I've been stuck here for about two days now,,

I've copy and pasted the suggested way of taking out the punctuation in the name string, that way it can recognize certain phrases in the other lists. Right now, as its only at the very start, I only have it set to notice if the beginning output has either a phrase akin to "How are you" or "Nice to meet you". These are both set in lists, and the if-else statement checks to see if the name input has more than one word, and will check for the phrasing in there. It's not registering that the punctuation has been removed, even when setting the new string to a new variable.

IE:

input Hi! I'm Jon! How are you?

output: I'm doing well, Jon!

Instead it gives one of the misunderstanding outputs. It works fine if I only use one word with ending punctuation, but it seems like if there is more than one word or punctuation in the middle, it can't remove it.

Is it in the wrong spot, or am I using the wrong method for this situation?

reddit.com
u/duelBooleans — 9 days ago

Hi, and thank you in advance!

I have a project I'm working on (an Eliza style chatbot, if context helps), and I have a problem I can't seem to find an answer to. I'm trying to take the input for a name, while also watching out for other phrases in that input. I only want to print their name when I use the {name} variable when using an f-string.

Tl;dr, how do you take a word from an inputted string if you don't know where that word will be in the string?

reddit.com
u/duelBooleans — 15 days ago

I have another, slightly more aggravating question. I have two while loops that take an input, and stores it into a list. That part works just fine, it's when it comes to the, quite frankly, massive if-elif statement.

currentMajorEmotions = []
currentSecondaryEmotion = ""
feeling = ""


while len(currentMajorEmotions) != 1:
    emotion = input("Which of these emotions are you feeling? Choose one: ").capitalize()


    if emotion not in majorEmotionTypes:
        print("Not a valid entry")
    else:
        currentMajorEmotions.append(emotion)



while len(currentMajorEmotions) != 2:
    emotion = input("Number 2? If you are not feeling one, simply hit enter: ").capitalize()


    if emotion in majorEmotionTypes:
        currentMajorEmotions.append(emotion)


    if emotion == "":
        print("You have left the question blank.")
        emotionYN = input("Was this intentional?: ").capitalize()
        if emotionYN == "Y":
            currentMajorEmotions += " "
            break
        
        while emotionYN != "Y":
            if emotionYN == "":
                emotionYN = input("May not leave this blank. Are you feeling a second emotion?: ").capitalize()
            elif emotionYN != "Y" and emotionYN != "N":
                emotionYN = input("Invalid input. Are you feeling a second emotion?").capitalize()
            elif emotionYN == "N":
                print("You have changed your mind")
                time.sleep(1.4)
                print("That's ok.")
                emotionYN = input("Are you feeling a second emotion?: ").capitalize()
                if emotionYN == "N":
                    currentMajorEmotions += " "
                    break



if "Sad" and " " in currentMajorEmotions:
    feeling = "Negative"


elif "Scared" and " " in currentMajorEmotions:
    feeling = "Negative"


elif "Mad" and " " in currentMajorEmotions:
    feeling = "Negative"


elif "Happy" and " " in currentMajorEmotions:
    feeling = "Positive" # Etc Etc

I testing the entire if statement to see if there were any incorrect outputs, and there were several. They do give out the correct type of output('Negative', 'Positive', 'Neutral'), but the wrong one. It's quite a few of them, and with the while loops working the way they're supposed to, I think the problem is in the if statement. The only problem is I have zero idea where to start looking since they're all connected to the same list. Any help is very appreciated!

reddit.com
u/duelBooleans — 16 days ago

I'm pretty new to Python, and I'm trying to make a rudimentary "therapy chatbot" (something a little more in depth than Eliza, made more so as practice of simple concepts). I'm trying to have the user input an emotion, and then pop up with some text that it wasn't found in the majorEmotionTypes list, and then try again. I know my code is pretty far from what I want, but I'll be going back later to edit a few things.

The main issue is that it can't find 'i' in the majorEmotionTypes list. Any help would be deeply appreciated!

majorEmotionTypes = ["Sad", "Scared", "Mad", "Happy", "Disgusted", "Surprised"]
currentMajorEmotions = []
capCurrentMajorEmotions = []


for i in range(1, 3):
    currentMajorEmotions.append(input("Which of these emotions are you feeling?:
").capitalize())
    if i not in majorEmotionTypes:
        currentMajorEmotions.remove(i)
        print("Not a valid entry")
        currentMajorEmotions.append(input("Try again: ").capitalize())
reddit.com
u/duelBooleans — 17 days ago