Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes and Functions
#1
So I understand little about classes and functions. I know how to create simple classes and functions but I still need much help and learning. I am trying to create a login file for a game that I will start working on as soon as my login modules are finished and working properly. This code is examples of what I've created and learned while making simple classes:


from random import randint

class Die:

   def __init__(self, sides, roll=0):
       self.sides = sides
       self.roll = roll

   def roll_die(self):
       if self.sides==6:
           while self.roll < 6:
               print("You roll a six sided die: " + str(randint(1, 6)))
               self.roll += 1
       elif self.sides==10:
           while self.roll < 6:
               print("You roll a ten sided die: " + str(randint(1, 10)))
               self.roll += 1
       elif self.sides==20:
           while self.roll < 6:
               print("You roll a twenty sided die: " + str(randint(1, 20)))
               self.roll += 1
       else:
           print("Mistakes have been made!")


six_sided_die = Die(6)
six_sided_die.roll_die()
print('')
ten_sided_die = Die(10)
ten_sided_die.roll_die()
print('')
twty_sided_die = Die(20)
twty_sided_die.roll_die()
class User():

   def __init__(self, first_name, last_name, username):

       self.first_name = first_name

       self.last_name = last_name

       self.username = username



   def describe_user(self):

       print("\nThis is the information for " + self.username + ":\n-- Last name, First name --\n\t* " +

             self.last_name.title() + ", " + self.first_name.title())



   def greet_user(self):

       print("\nHello, " + self.username + "!")



user_1 = User('joshua', 'mitchum', 'Broodwar1998')

user_2 = User('loki', 'thor', 'Godz')



user_1.describe_user()

user_1.greet_user()



user_2.describe_user()
user_2.greet_user()
Ok so that's my knowledge as far as classes go... Now what I need help with is turning this next code into a class. I know how to make classes without taking in user input. I do not know how to implement user input into classes. For example this code:

prompt_message = "Enter the username you wish to create. \nType \'quit\' if you wish to exit: "

prompt_username = ""



while prompt_username.lower() != 'quit':

   prompt_username = input(prompt_message)



   if prompt_username.lower() == 'quit':

       break

   else:

       print("Welcome to a whole new world, " + prompt_username)
       break
I understand I need to create a list of usernames input and save it to a txt file or json file as a dictionary so i can recall it everytime a username is created in order to check if the username is available and passwords match also length restrictions and password hashing... But for now I just need to understand how to turn this into a simple class if that's possible? To make this more efficient code I guess?
Reply


Messages In This Thread
Classes and Functions - by Low_Ki_ - Mar-28-2017, 05:43 PM
RE: Classes and Functions - by ichabod801 - Mar-28-2017, 10:31 PM
RE: Classes and Functions - by Low_Ki_ - Mar-29-2017, 03:37 AM
RE: Classes and Functions - by nilamo - Mar-29-2017, 04:10 PM
RE: Classes and Functions - by wavic - Mar-29-2017, 04:00 AM
RE: Classes and Functions - by Larz60+ - Mar-29-2017, 04:48 AM
RE: Classes and Functions - by Low_Ki_ - Mar-29-2017, 03:02 PM
RE: Classes and Functions - by wavic - Mar-29-2017, 03:25 PM
RE: Classes and Functions - by Low_Ki_ - Mar-29-2017, 04:12 PM
RE: Classes and Functions - by ichabod801 - Mar-29-2017, 09:50 PM
RE: Classes and Functions - by Low_Ki_ - Mar-30-2017, 11:22 PM
RE: Classes and Functions - by ichabod801 - Mar-31-2017, 01:00 AM
RE: Classes and Functions - by Low_Ki_ - Mar-31-2017, 03:16 PM
RE: Classes and Functions - by wavic - Mar-30-2017, 11:58 PM
RE: Classes and Functions - by zivoni - Mar-31-2017, 04:07 PM
RE: Classes and Functions - by Low_Ki_ - Mar-31-2017, 04:39 PM
RE: Classes and Functions - by Low_Ki_ - Mar-31-2017, 07:36 PM
RE: Classes and Functions - by wavic - Apr-01-2017, 03:38 AM
RE: Classes and Functions - by Low_Ki_ - Apr-01-2017, 04:48 AM
RE: Classes and Functions - by wavic - Apr-01-2017, 05:15 AM
RE: Classes and Functions - by Low_Ki_ - Apr-01-2017, 03:58 PM
RE: Classes and Functions - by wavic - Apr-01-2017, 08:56 PM
RE: Classes and Functions - by Low_Ki_ - Apr-01-2017, 09:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,547 Aug-25-2020, 03:43 AM
Last Post: micseydel
  How can classes access each other Functions and Variables at the same time PythonOK 4 3,081 Dec-09-2018, 03:46 AM
Last Post: ichabod801
  Using classes? Can I just use classes to structure code? muteboy 5 5,080 Nov-01-2017, 04:20 PM
Last Post: metulburr
  Python Classes or Functions for large scale application ? Vithulan 5 4,586 Oct-23-2017, 04:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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