1

The problem

I want get_scores to take tests and use it as an int for x > 0 but be able to print tests. I've tried fiddling around for awhile and I can't get what I want.

What I've tried

My problem is that I want the get_scores method to print "What percentage are tests weighted". Tests is assigned to an int so it can't print anything unless I do this which prints an int which I don't want it to do:

int(input("What percentage are" + str(x) + "weighted?"

I've tried a couple different things such as:

get_initial_input function (   like this: testsp = get_scores("tests")    )

This makes it so x > 0 can't be processed because x is a string. I also tried:

int(x) > 0 

... to see if it was as simple as changing it back to an int (didn't work).

The code

def main():
    tests = get_initial_input("tests")
    assignments = get_initial_input("assignments")
    exercises = get_initial_input("exercises")
    labs = get_initial_input("labs")
    finals = get_initial_input("finals")
    testsp = get_scores(tests)
    assignmentsp = get_scores(assignments)
    exercisesp = get_scores(exercises)
    labsp = get_scores(labs)
    finalsp = get_scores(finals)
def get_initial_input(x):
    val = int(input("How many "+ x + " were there?    "))
    return val

def get_scores(x):
    if x > 0:
            xp = int(input("What percentage are "+ str(x) + " weighted?"))
            return xp

main()

Is there anyway to get what I want?

Thanks for your help in advance.

1
  • maybe make a dict where the key is your type of tests an the values are ints representing weightings, might be easier Commented Nov 16, 2015 at 0:36

2 Answers 2

1

You could have get_scores take two arguments: one being a string giving the type of the assignment, and therefore what to print, and the other being the number of such assignments:

def get_scores(x, kind):
    if x > 0:
        xp = int(input("What percentage are "+ kind + " weighted?"))
        return xp

Which you'd call like:

testsp = get_scores(tests, 'tests)

But it might make more sense to write the function without the if, and do the checking before you call it.

Sign up to request clarification or add additional context in comments.

Comments

0

I may be a little confused, but it sounds like you are trying to use the variable tests to represent both an integer value and a string at the same time. You could turn tests into a string, and then use string indexing to get what you want:

tests = ["tests"]

Now you can get the second value into the list by using:

tests.append(get_initial_input(tests))

Now when you want to print the string, just use print tests[0] and when you want to print the number of tests, use print tests[1].

Comments

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.