0

I am trying to make a python script that will prompt the user for two input floats and perform calculations with the input. Before I can get to that part I am having trouble understanding how to create an object and access it.

I am starting out simple with just one object here called FightingForce to learn the concept I am just take input for creating 1 of these object and trying to print it.

My question is how do I get the user input and store it as a FightingForce object, that is accessible for use in an equation?

# Create a fighting force object
class FightingForce(object):
    size = 0
    lethalityCoefficient = 0

    # Class constructor/initilizer
    def __init__(self, size, lethalityCoefficient):
        self.size = size
        self.lethalityCoefficient = lethalityCoefficient

def make_fightingForce(size, lethalityCoefficient):
    fightingForce = FightingForce(size, lethalityCoefficient)
    return fightingForce

# Prevent user from inputting anything other than a float
while True:

    try:
        # Promt user for input and set variables
        size = float(raw_input('Enter the amount of troops: '))
        lethalityCoefficient = float(raw_input('Enter the lethality coefficient: '))

    except ValueError:
        print("Please input a floating point integer greater than zero")
        continue

    else:
        break

# Display results to user
print(fightingForce.size)
raw_input('Press <ENTER> to exit')

Currently my code will ask for the two inputs and immediately close after I enter them. I have tried placing "raw_input('Press to exit')" in various locations in an attempt to see where it is failing but I am not getting any good results.

2 Answers 2

2

You are creating an object of the class in function make_fightingForce and returning it from there. Just call this function and take its return value to use it.

Change the last lines to:

print(make_fightingForce(size, lethalityCoefficient).size)
Sign up to request clarification or add additional context in comments.

3 Comments

This works for creating a single object, and I upvoted for your help (thanks!), but I am looking to be able to create multiple of them based on the value of the loop counter.
That's not apparent in your original question. And what do you mean by "loop counter"?
@ alan, sorry if the initial question didn't mention where I was going with it. I want to basically create fightingForce + str(x) where x is incremented for the number of loop iterations. Then I want to be able to do fightingForce1.size, fightingForce2.size, etc.
1

Just instantiate an instance of the FightingForce object inside the loop

while True:
    try:
        size = float(raw_input('Enter the amount of troops: '))
        lethalityCoefficient = float(raw_input('Enter the lethality coefficient: '))
        fightingForce = make_fightingForce(size, lethalityCoefficient)
        break
    except ValueError:
        print("Please input a floating point integer greater than zero")
print(fightingForce.size)

4 Comments

this answers my original question, thanks so much. I am trying to work through creating multiple fightingForce objects with the loop now. If the loop executed 2 times I am trying to create fightingForce1 and fightingForce2... Thanks again.
If you're trying to create multiple FightingForce objects then you should first prompt the user for how many they want to create. Then each iteration of this loop should create a new FightingForce object and add it to a list containing all the FightingForce objects. Break the loop when the list contains as many FightingForce objects as the user wanted.
excellent idea, I am trying to figure out how to make two of them at the moment then I will work on that.
I got it working and I owe you and yeniv both much thanks! Until next time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.