Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do you write a function or class in python3.

+2 votes
asked Jun 29, 2023 by Chidera Chidubem-J (180 points)

Please you have to tell me. I'm havin' some trouble with it cuz of my projects.

2 Answers

0 votes
answered Jun 30, 2023 by Peter Minarik (86,640 points)

You can find online tutorials such as this that explain how to create classes and functions.

Here's a simple example:

class Warrior:
    def __init__(self, name, health):
        self.name = name
        self.health = health

    def display(self):
        print(f"The warrior {self.name} has {self.health} HP.")

spartacus = Warrior("Spartacus", 100)
spartacus.display()

Please, share your existing code or add more details to your question for a more specific answer.

0 votes
answered Jul 15, 2023 by Gulshan Negi (1,580 points)

Hello this is Gulshan Negi

Well, here is the simple way to write a function in Python3.

def greet(name):
    """A function that greets the user."""
    print(f"Hello, {name}!")

# Calling the function
greet("John")

Thanks

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...