Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Contribution #2492

Open
realeesh17 opened this issue Jan 15, 2025 · 4 comments
Open

Python Contribution #2492

realeesh17 opened this issue Jan 15, 2025 · 4 comments

Comments

@realeesh17
Copy link

Incorrect Output in calculate_total Function

Description:I have encountered an issue in the calculate_total() function in the billing.py file. When I provide input with a list of prices, the total calculated is incorrect. The function does not seem to handle decimal values correctly.

from billing import calculate_total
prices = [12.99, 5.49, 8.75]
total = calculate_total(prices)
print(total)
@hazey21
Copy link

hazey21 commented Jan 18, 2025

The problem has occurred because the calculate_total function is not properly handling the decimal values. You need to insert the Decimal class to avoid these errors.

from decimal import Decimal

def calculate_total(prices):
   total= sum(Decimal(str(price)) for price in prices)
   return total 
from billing import calculate_total

prices = [12.99, 5.49, 8.75]
total = calculate_total(prices)
print(total)

@zilolaegamberganova
Copy link

Hello, I’m Zilola. I’ve been looking into the issue regarding the calculate_total function.

Problem: As you mentioned, it seems that the function is not properly handling decimal values when calculating the total.

Suggestion: I believe the issue can be resolved by using the Decimal class for accurate calculations. Additionally, I recommend using the round() function to ensure that the values are rounded correctly.

If you need further assistance, I am here to help. Thank you!

@zilolaegamberganova
Copy link

Hello, I’m Zilola. I need help with my code because it has an error.

I have a simple chatbot implementation, but I am facing an issue. Here’s the code:

class ChatBot:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return "Hello! I'm " + self.name

    def respond(self, message):
        if message.lower() == "how are you?":
            return "I'm fine, thank you!"
        elif message.lower() == "what's your name?":
            return "My name is " + self.name
        else:
            return "Sorry, I don't understand."

bot = ChatBot(None)
print(bot.greet())
print(bot.respond("How are you?"))

@navjotmaan
Copy link

you are getting issue because of None, you can solve it by using a default name. Change self.name as given below. I hope it helps.

class ChatBot:
def init(self, name):
self.name = name

def greet(self):
    # Handle None by using a default name
    return "Hello! I'm "  + (self.name if self.name else "ChatBOT")

def respond(self, message):
    if message.lower() == "how are you?":
        return "I'm fine, thank you!"
    elif message.lower() == "what's your name?":
        return "My name is " + (self.name if self.name else "ChatBOT")
    else:
        return "Sorry, I don't understand."

Create a Chatbot instance with None as the name

bot = ChatBot(None)
print(bot.greet())
print(bot.respond("How are you?"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants