Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First piece of code
#1
Hey team,

I have worked on a piece of code that should be able to read my question on sustainability and produce me an answer with some ideas based on what I asked it.

The code loads so I can test the instance but it doesn't produce an answer?

The code is
import random
import json

# Create a list of questions and answers about sustainability
questions = [
    "How can I reduce the environmental impact of my event?",
    "What are some sustainable catering options?",
    "How can I encourage people to use public transportation to my event?",
    "How can I reduce the amount of waste at my event?",
    "How can I make my event more accessible to people with disabilities?",
    "What is the most sustainable food?",
]

def new_func():
    return "Here are some tips on how to reduce the environmental impact of your event:\n"

answers = [new_func().strip()]

# Create a database to store the questions and answers
database = {}

# Create a function to answer a question
def answer_question(question):
    if question in database:
        return database[question]
    else:
        keywords = question.split()
        for keyword in keywords:
            if keyword in database:
                return database[keyword]
        answer = random.choice(answers)
        database[question] = answer
        return answer

# Create a function to start the web app
def start_web_app():
    print("Welcome to the sustainability web app!")
    print("What question would you like me to answer?")
    question = input()
    answer = answer_question(question)
    print(answer)

# Start the web app
start_web_app()
I can ask the question but no answer is delivered. Can someone please advise if I have missed a step for a reason to why I can see the answer being submitted?
Gribouillis write Aug-02-2023, 05:27 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Please use the bbtags when posting
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
What do you mean by:
Quote:The code loads so I can test the instance but it doesn't produce an answer?
The code produces the answer: "Here are some tips on how to reduce the environmental impact of your event:" Granted that is not a useful answer, but your program cannot produce any other answer because it only has one.

Let's start with "What is the program supposed to do?" This is obviously a homework assignment. You should post the instructions along with the code. As is, I don't know what code you wrote (probably new_func()) or what the code is supposed to do.

The database is empty.
# Create a database to store the questions and answers
database = {}
You never put anything in database, so it never finds any answers.

What are you doing here?
def new_func():
    return "Here are some tips on how to reduce the environmental impact of your event:\n"
 
answers = [new_func().strip()]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 726 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Understanding a piece of code Michael1 4 1,431 Jan-20-2022, 07:14 PM
Last Post: Michael1
  How to make this piece concise leoahum 0 1,348 Sep-23-2021, 09:23 PM
Last Post: leoahum
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 2,998 Jan-28-2021, 05:05 PM
Last Post: buran
  I am a newbie.Help me with this simple piece of code feynarun 3 2,824 Jan-08-2020, 12:40 PM
Last Post: perfringo
  How can I improve this piece of code? aquerci 3 2,221 Nov-17-2019, 10:57 AM
Last Post: Gribouillis
  Am i doing something wrong with this piece of code pythoneer 3 2,701 Mar-02-2018, 08:50 PM
Last Post: JustaNoOb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020