0

I'm really, really new to programming and have been working on this code for the past two months. I understand if it overall isn't functioning efficiently, but I'm just trying to answer the question I'm stuck on.

I have a variable named user_text. On screen, an input box appears that allows users to answer the questions when they appear. I want to save each answer they type for each question into a list ex. "On a scale of 1 to 5, 1 being not at all and 5 being all the time: Are you experiencing any pain?" User_text = 1 "On a scale of 1 to 5, 1 being not at all and 5 being all the time: Have you ever contimplated suicide?" User_text = 4 "On a scale of 1 to 5, 1 being not at all and 5 being all the time: Do you ever have trouble sleeping?" User_ text = 3

print(answers) = [1,4,3]

However, I run into two issues when I try to implement this into my code. One, it doesn't save the input at all, returning a blank list. Two, it saves input, but only from one of those responses so it would just save the answer 4 not 1, and 3. Three, It messes up my current_dialogue_index skipping questions.

I want to have the list of user input to utilize later in the program depending on the added up scores.

If anyone can provide any help at all, that would be greatly appreciated. I'm learning as I go and I'm more of an artist then a programmer, so explaining in basic terms is also appreciated. Thank you to anyone who takes the time to help me!

Here's my code:

#Dialogue
messages = ["Hello and welcome to Dr. Collins' office. (Press the 'Enter' key to continue).",
            "I presume you have an appointment with us today?",
            "To use our tele-health system click on the input box below when prompted.",
            "When the input box is green, you may begin to type your response.",
            "Now, please hold a moment. Dr. Collins will be with you shortly."
            ]
intro = ["Hello there, my name is Dr. Collins. What is your name?",
             "Hello",
             ]
questions =  ["On a scale of 1 to 5, 1 being not at all and 5 being all the time: Are you experiencing any pain?",
             "On a scale of 1 to 5, 1 being not at all and 5 being all the time: Have you ever contimplated suicide?",
             "On a scale of 1 to 5, 1 being not at all and 5 being all the time: Do you ever have trouble sleeping?",
             "On a scale of 1 to 5, 1 being not at all and 5 being all the time: Do you ever have trouble concentrating?",
             "On a scale of 1 to 5, 1 being not at all and 5 being all the time: Do you struggle with poor appetite or overeating?"
             ]

Final_Messages = ["Thank you for you time today.",
                  "I'm going to review your questions.",
                  "Your prescription will print shortly.",
                  "When you received your prescription, you may click the 'Finish' button."
                 ]

answers = []

doctor_images = [pygame.image.load('DoctorFrown.png').convert(),
                 pygame.image.load('DoctorSmile.png').convert(),
                 pygame.image.load('DoctorGlitch.png').convert()
                 ]
secretary_images = [pygame.image.load('Secretary.png').convert(),
                   pygame.image.load('secretaryGlitch.png').convert()]
done = False

#Main Game play
def game_loop():
    doctor_mode = False
    active = False
    user_text = ''
    name = ''
    save_name = False
    current_dialogue_index = 0
    current_list = messages

    # Switch between pictures
    current_doctor_image = 0
    doctor_image = doctor_images[current_doctor_image]
    current_secretary_image = 0
    secretary_image = secretary_images[current_secretary_image]
    image_change_timer = pygame.time.get_ticks() + 10000

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                if input_rect.collidepoint(event.pos):
                    active = True
                else:
                    active = False

            # Dialogue
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    if current_list == messages and current_dialogue_index == len(current_list) - 1:
                        current_list = intro
                        current_dialogue_index = 0
                        doctor_mode = True
                    elif current_list == intro and current_dialogue_index == len(current_list) - 1:
                        current_list = questions
                        current_dialogue_index = 0

# I was adding the answers.append(user_text) here assuming it would save the user_text to answers,but as stated it wasn't working. I've also tried user_text = ''.join(answers)? 

                    elif current_list == intro and current_dialogue_index == 0 and not save_name:
                        name = user_text
                        save_name = True
                    elif current_list == questions and current_dialogue_index == len(current_list) - 1:
                        current_list = Final_Messages
                        current_dialogue_index = 0
                    elif current_list == Final_Messages and current_dialogue_index == len(current_list) - 1:
                        screen.fill((0,0,0))
                    else:
                        current_dialogue_index += 1
                        if current_dialogue_index >= len(current_list):
                            done = True
                            break
                    if active:  # Check if input box is active
                        if current_dialogue_index < len(current_list) - 1:  # Check if there are more questions
                            current_dialogue_index += 1

# I was also trying to add it here as well

                        else:
                            done = True  # No more questions, end the dialogue

                if active == True:
                    if event.key == pygame.K_BACKSPACE:
                        user_text = user_text[:-1]
                    else:
                        if len(user_text) < 8 and event.unicode.isalpha() or (event.unicode.isdigit() and int(event.unicode) >= 1 and int(event.unicode) <= 5):
                            user_text += event.unicode
                        elif event.key == pygame.K_RETURN:
                            user_text = ''

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • "However, I run into two issues when I try to implement this into my code. One, it doesn't save the input at all, returning a blank list. Two, it saves input, but only from one of those responses so it would just save the answer 4 not 1, and 3. Three, It messes up my current_dialogue_index skipping questions." It looks like 1 and 2 are mutually exclusive (i.e. they cannot happen at the same time) it either never saves the input or it saves the input only sometimes. Please be more clear in describing the unexpected behaviour – Caridorc Mar 19 '23 at 23:09

1 Answers1

0

I'm not familiar with pygame but I may have a few ideas as far as what's going wrong.

"One, it doesn't save the input at all, returning a blank list."

You seem to indicate with your comments that you were trying to append user_text to your answers list. It doesn't appear that user_text has been assigned a value at the point in the code at which you tried to append it. user_text currently only appears to be set if you press backspace.

My guess is you want to set it right after your first if event.key == pygame.K_RETURN:

In other words, the user presses enter, it should get the value from the input box, assign that value to user_text, then append user_text to the answers list.

Second, I'm suspicious of

if active: # Check if input box is active if current_dialogue_index < len(current_list) - 1: # Check if there are more questions current_dialogue_index += 1

If you click on and off the input box, this seems like it would immediately increment current_dialogue_index. That could be causing it to skip questions.

Take a look and see what you think. It might be helpful if you posted the entire code file. It seems like some code is missing.

Brock
  • 21
  • 6