Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:44:15+00:00 2026-06-15T14:44:15+00:00

There is a lot of code below but you dont have to really read

  • 0

There is a lot of code below but you dont have to really read any of it, you just need to know that the functions exist and the function names. I will describe my problem first.

I have created a procedural program based entirely on functions and a few global variables as you can see below. I want to change the program into an object-oriented-program but I am having trouble with it since I have never done anything like this before.

The procedure that needs to be followed is that:
– the function attack() needs to be placed into a file named attacker.py
-the functions defence(),updateVars(), and smartDefender() need to be placed into a file defender.py
-the main() function and the rest of the code(most of the code) will be placed in a file named manager.py which will be the main file and will bring everything together.
-I have to use classes.

I have tried a range of different things including changing the names of the functions to __init__ and then importing and attempting to use them in manager.py. I have also tried to keep the function names the same and just put the functions inside of classes and import attacker.py and defender.py into manager.py but nothing seems to work… Any and all help would be appreciated.

Although I don’t think you really need a description of what the program does, if you really need one, I can make a brief one, or you can view it here.

Any and all help will be appreciated.

import random

HIGH= 3
MED= 2
LOW= 1

def attack(attackList):

    x= random.uniform(0,1)
    for attackLevel,probability in attackList:
        if x<probability:
            break
        x=x-probability
    return attackLevel





def defence(attackLevel,defendList):

    x= random.uniform(0,1)
    for defendLevel,probability in defendList:
        if x<probability:
            break
        x=x-probability
    return defendLevel





def updateVars(attackLevel,defendLevel,block,hit,run):

    if attackLevel==1:
        printAttackLevel='Low'
    if attackLevel==2:
        printAttackLevel='Medium'
    if attackLevel==3:
        printAttackLevel='High'
    if defendLevel==1:
        printDefendLevel='Low'
    if defendLevel==2:
        printDefendLevel='Medium'
    if defendLevel==3:
        printDefendLevel='High'



    if attackLevel==defendLevel:
        block=block+1
        hit=hit
        run=run+1
    else:
        block=block
        hit=hit+1
        run=run+1

    return block,hit,run,printAttackLevel,printDefendLevel





def smartDefender(defendLevel,attackLevel,smartList):

    for i in smartList:
        if (i==(i+1)==(i+2)):
            defendLevel= attackLevel
            return defendLevel
        else:
            return






def main():

    DEFAULT_PROBABILITY= 0.33
    run=0
    hit=0
    block=0
    smartList=[]


    rounds= int(input("\nPlease enter the number of rounds between 1 and 100:"))
    if rounds<=0 or rounds>100:
        print("\n")
        print("Invalid range. The number of rounds has been set to 10 by DEFAULT_PROBABILITY.")
        rounds=10


    lowAttackProb= float(input("\nPercentage of attacks aimed low(0-100):"))/100
    medAttackProb= float(input("Percentage of attacks aimed medium(0-100):"))/100
    highAttackProb= float(input("Percentage of attacks aimed high(0-100):"))/100
    if lowAttackProb+medAttackProb+highAttackProb !=1.00:
        print("\n")
        print("Invalid entry. The sum of the pecentages must equal 100%. The probability of each level has been set to 33.0% by DEFAULT_PROBABILITY.")
        lowAttackProb=DEFAULT_PROBABILITY
        medAttackProb=DEFAULT_PROBABILITY
        highAttackProb=DEFAULT_PROBABILITY


    print('\nLet The Fighting Begin')
    print('-'*22)


    while  run < rounds:

        lowDefProb= DEFAULT_PROBABILITY
        medDefProb= DEFAULT_PROBABILITY
        highDefProb= DEFAULT_PROBABILITY


        attackList= [(LOW,lowAttackProb),(MED,medAttackProb),(HIGH,highAttackProb)]
        attackLevel= attack(attackList)
        smartList.append(attackLevel)
        defendList=[(LOW,lowDefProb),(MED,medDefProb),(HIGH,highDefProb)]
        defendLevel=defence(attackLevel,defendList)
        block,hit,run,printAttackLevel,printDefendLevel= updateVars(attackLevel,defendLevel,block,hit,run)
        if run>(rounds/2):
            defendLevel=smartDefender(defendLevel,attackLevel,smartList)
            #implement smart mode

        print('%s%2s%s%3s%s%5s%s%3s'% ('\nRound',run,':\t','Attacker:',printAttackLevel,'\t','Defender:',printDefendLevel))


    print("%2s%2d%s%s%2d"% ('\nTotal Hits:',hit,'\t','Total Blocks:',block))
    print('Attacker Proportions:','','','Low:','','',lowAttackProb*100,'%','','','Medium:','','',medAttackProb*100,'%','','','High:','','',highAttackProb*100,'%')
    print('Defender Proportions:','','','Low:','','',lowDefProb*100,'%','','','Medium:','','',medDefProb*100,'%','','','High:','','',highDefProb*100,'%')
    print("\nThank you for using this program, Goodbye!")





main()

My question is, How can I very easily (not necessarily efficiently) convert these procedural program into a object-oriented one that uses classes and multiple files.

I think problem areas would include where the functions are being called in main(), if that helps to solve the problem at all..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:44:16+00:00Added an answer on June 15, 2026 at 2:44 pm

    In case it helps … I just made this class-based script as an example.

    class Car():
    
        def __init__(self, mileage=0, mpg=10, tank_capacity=5):
            self.mileage = mileage
            self.mpg = mpg
            self.tank_capacity=tank_capacity
            self.tank_level = 0                
    
        def drive(self, miles):
            gallons_burned = float(miles) / self.mpg
            if self.tank_level < gallons_burned:
                miles_driven = float(self.tank_level) * self.mpg
                self.tank_level = 0
                self.mileage += miles_driven
                print "You ran out of gas after %s miles." % miles_driven
                return
            self.tank_level -= gallons_burned
            self.mileage += miles
            print "You made it to your destination after %s miles." % miles
    
        def pump_gas(self, gallons):
            self.tank_level += gallons
            if self.tank_level > self.tank_capacity:
                self.tank_level = self.tank_capacity
            print "You now have %s gallons in your tank" % self.tank_level
    
        def status(self):
            print "Mileage:", self.mileage
            print "Tank level: %s gallons" % self.tank_level
    
    if __name__ == "__main__":
        my_car = Car(mileage=100, mpg=30, tank_capacity=3)
        my_car.pump_gas(4)
        my_car.status()
        my_car.drive(50)
        my_car.status()
        my_car.drive(60)
        my_car.status()
    
    """
    You now have 3 gallons in your tank
    Mileage: 100
    Tank level: 3 gallons
    You made it to your destination after 50 miles.
    Mileage: 150
    Tank level: 1.33333333333 gallons
    You ran out of gas after 40.0 miles.
    Mileage: 190.0
    Tank level: 0 gallons
    """    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are lot's of questons on this, but all seem to
Basically the question above, you don't need to read the lot below, just additional
Note there's a lot of code posted below but its mostly all the same,
I'm writing some code that has a lot of substitution. There's a list<char> productions
I know there a lot of similar questions to this, but I didn't find
I have read through all the threads out there that looked as if they
In my code there are a lot of final values, like 10000. They never
Although there are a lot of jquery autocomplete code in this forum. However I
There are a lot of posts on here about this, I'm using this code
There are a lot of blogs saying that a hasOwnProperty check should be used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.