Python Forum
please help with classes and return values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: please help with classes and return values (/thread-36065.html)



please help with classes and return values - jamie_01 - Jan-14-2022

#!/usr/bin/python3

import module

class StartProgram():
    def display1(self):
        print("This is superclass")

    def function(self):
        print("Hello")

class CentralCorridor (): # main body
    def swim (self):
        print("""You have selected a very important part of the game""")

    def print_function(self):
        print("Welcome to my Classes Python Program!")
        print("What do you want to do?")
        print("1. Press (1) to win.")
        print("2. Press (2) to die.")

        action = input("> ")

        if action == "1":
            print("You won!")
            return 'finished'
        elif action == "2":
            print ("You died.")
            return 'death'
        elif action == "3":
            print ("You pressed '3'...")
            return 'death'
        else:
            print (f"The name of the string is {answer}".format (answer))

class Finished ():

    def __init__(self, first_name, last_name ="Fish"):
        self.first_name = first_name
        self.last_name = last_name

    def swim(self):
        print("The fish is swimming.")

    def swim_backwards (self):
        print("You won! Good job.")

class Death ():

    elements = []

    the_count = [5, 4, 3, 2, 1]

    for i in elements:
        print(f"And you die in...: {i}")

    def swim(self):
        print(Death.quips[randint(0, len(self.quips) -1)])

    quips = [   # dictionary
            "You died. You kinda suck at this.",
            "Your Mom would be proud...if she were smarter.",
            "Such a luser.",
            "I have a small puppy that's better at this.",
            "You're worse than your Dad's jokes."
    ]

print(module.tangerine)

g = CentralCorridor()
g.print_function()
Please, could someone please help me with the return value's, please, what would be the best way for these return statements

        if action == "1":
            print("You won!")
            return 'finished'
        elif action == "2":
            print ("You died.")
            return 'death'
        elif action == "3":
            print ("You pressed '3'...")
            return 'death'
to initialize the classes (Death() and Finished()). I will work on the inside of these afterward. Also, this is a question out of my book, "Learn Python 3 the hard way,", also, I plan on posting more to this thread and work more on it over the next few days, would like to add an "class Engine()" class which would include a while() loop with additional code that in the book is described as an Engine and called Engine(). Please, help with the return statements and post follow up code if I am not asking too much.


RE: please help with classes and return values - menator01 - Jan-14-2022

I would recommend starting here.
Another link on classes here
And one final link here on super.


RE: please help with classes and return values - Gribouillis - Jan-14-2022

I find it difficult to understand what the program does. Perhaps you should learn python the easy way...


RE: please help with classes and return values - jamie_01 - Jan-17-2022

#!/usr/bin/python3

import new_module
import sys

print("""Welcome to my Classes Python program! Do you want,
        to (C)ontinue gmae or (Q)uit game? Press "1" or "2""")

answer = input("> ")

class MainProgram ():

        if answer == "1":
            print("What do you want to do?")
            print("1. Do you want to Win the game, press (1)")
            print("2. Do you want to Lose the game, press (2)")

            answer_2 = input("> ")

            if answer_2 == "1":
                print("You pressed (1)") # You Lost, go to function
                print("You win")

            elif answer_2 == "2":
              print("you pressed 2") # Won Game
              print("You lost")

            else:
                 print(f"Wrong selection, {answer_2}")
                 print("Error!")

            the_count = [5, 4, 3, 2, 1]

            for i in the_count:
                print(f"And you lose in...: {i}")

        elif answer == "2":
            print ("you pressed (2)")
            print ("You lose")

        else:
            print(f"Wrong selection, {answer}")
            print("Error!")

class Lost_Game():
    def print_function (name):
        print(f"You {name}")

        sys.exit()

class Win_Game():

    def win_function (name):
        print(f"You {name}")

        sys.exit()

var_2 = Lost_Game.print_function('lost')
var = You_Won.identify('won')
Please, could someone come up with a solution to this code so it works, for the last two classes Lost_Game() and Win_Game() classes and var and var_2 only print one line per correct each if statement above. Or explain how a Inheritance object works, thanks. Very new to Python classes.


RE: please help with classes and return values - BashBedlam - Jan-17-2022

Is this more like what you are trying to do?
import sys
 
class MainProgram ():
	def __init__ (self):
		self.main_loop ()

	def Won_Game(self):
		print("You won the game.")

	def Lost_Game(self):
		print ("Sorry... You lost.")

	def main_loop (self):
		print("Welcome to my Classes Python program! Do you want,")
		print ('to (C)ontinue gmae or (Q)uit game? Press "1" or "2')
		answer = input("> ")

		while True :
			if answer == "2" :
				print ("See ya later.")
				sys.exit ()
			if answer == "1":
				print("What do you want to do?")
				print("1. Do you want to Win the game, press (1)")
				print("2. Do you want to Lose the game, press (2)")
		 
			answer_2 = input("> ")
			if answer_2 == "2":
				print("You pressed (2)") # You Lost, go to function
				self.Lost_Game ()
			elif answer_2 == "1":
				print("you pressed 1") # Won Game
				self.Won_Game ()
			else:
				print(f"Wrong selection, {answer_2}")
				print("Error!")
			print("1. Do you want to play again, press (1)")
			print("2. Do you want to quit now, press (2)")
			answer = input ("> ")

MainProgram ()



RE: please help with classes and return values - menator01 - Jan-17-2022

I did a version as well.
#! /usr/bin/env python3

# Create our class
class Game:
    def __init__(self):
        # Set some class variables
        self.win = 0
        self.lost = 0

    # Create a method/function for returning score
    def score(self):
        return f'Games Won: {self.win} / Lost: {self.lost}'

# Define the main program
def main():
    # Inialize the Game class
    game = Game()
    # Set playing to True
    playing = True
    while playing:

        # Print the scores to the screen
        print(game.score())

        # Ask user what to do
        print('Valid inputs are: 1(win), 2(loose), q(to exit)')
        choose = input('>> ')

        # if 1 is chosen, add 1 to the win score
        # elif 2 is chosen, add 1 to the lost score
        # elif q is chosen quit the game
        # else any other characters is not a valid entry
        if choose == '1':
            game.win += 1
            print('\nYou win\n')
        elif choose == '2':
            game.lost += 1
            print('\nYou loose\n')
        elif choose == 'q':
            print(f'\n{game.score()}')
            print('Thanks for playing!\n')
            break
        else:
            print(f'\nError!, {choose.upper()} is not a valid entry.\n')

main()
Output:
Games Won: 0 / Lost: 0 Valid inputs are: 1(win), 2(loose), q(to exit) >> 1 You win Games Won: 1 / Lost: 0 Valid inputs are: 1(win), 2(loose), q(to exit) >> 1 You win Games Won: 2 / Lost: 0 Valid inputs are: 1(win), 2(loose), q(to exit) >> 2 You loose Games Won: 2 / Lost: 1 Valid inputs are: 1(win), 2(loose), q(to exit)