Python Forum
please help with classes and return values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please help with classes and return values
#1
#!/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.
Reply
#2
I would recommend starting here.
Another link on classes here
And one final link here on super.
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
I find it difficult to understand what the program does. Perhaps you should learn python the easy way...
buran likes this post
Reply
#4
#!/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.
Reply
#5
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 ()
Reply
#6
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)
BashBedlam likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to return 2 values from 1 DF that equals another DF cubangt 5 593 Oct-21-2023, 02:45 PM
Last Post: deanhystad
  [Solved]Return values from npyscreen Extra 2 1,092 Oct-09-2022, 07:19 PM
Last Post: Extra
  Parallelism with return values Plexian 7 1,425 Aug-14-2022, 09:33 AM
Last Post: Plexian
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,036 Jan-09-2022, 02:39 AM
Last Post: Python84
  Function - Return multiple values tester_V 10 4,317 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 1,916 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  What is the best way to return these 4 integer values? Pedroski55 4 2,491 Apr-13-2020, 09:54 PM
Last Post: Pedroski55
  Return values for use outside of function willowman 1 1,653 Apr-13-2020, 07:00 AM
Last Post: buran
  Return all Values which can divided by 9 lastyle 2 1,799 Mar-16-2020, 09:22 PM
Last Post: lastyle
  Save all values to pandas of multiple classes jenniferruurs 0 1,872 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs

Forum Jump:

User Panel Messages

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