James Hoffman
James Hoffman

Reputation: 31

How do you compare the output of a function in python?

Sorry if this is a obvious question, but I am 12 and trying to write some code to help me with my science project, where I am trying to see which lock is the most secure. Basically my problem is, I need to figure out how to compare the output of a function with the if somethinghere() == somethingelse(). And by the way, I am using the random module. As an example of what im trying to do, this is the comparing code part:

def guessfixed(y):     
        y = print ("[",(random.randint(0, 9)),", ",(random.randint(0, 9)),", ",(random.randint(0, 9)),", ",(random.randint(0, 9)),", ",(random.randint(0, 9)),"]",sep="") #this is supposed to keep changing until it is the same as the key.
        return y

for one part that I am comparing, all the [, ] things are to format it to compare it, and the other part I am comparing is

mylistkey = [] # the random generated key, this is supposed to stay the same.
for i in range(0,5):
    x = random.randint(0,9)
    mylistkey.append(x)

and how I am trying to compare it is with:

while guessfixed(0) != mylistkey:
    if guessfixed(0) == mylistkey:
        print ("it worked!")
    if guessfixed(0) != mylistkey: #by the way, this is still in the loop
        print ("it did not quite work"),

The only way I know this is not working is because in the full script, I changed the key and guess to only have 4 combinations, so the key and the guess would have to overlap eventually, and it just kept going, supposedly trying 100 combos before I killed it. According to my tests, the guessfixed changes, and the key does not. And once again, thanks for reading this, and sorry if this is an off-topic question or is really obvious. I am 12, so I'm not that good at writing questions, but practice makes perfect!

Upvotes: 3

Views: 2944

Answers (2)

tripleee
tripleee

Reputation: 189397

You need for guessfixed to return a list -- you want to compare a list to a list, not a list to a string. There is no need to pass in y (and certainly not pass in 0 -- you can't assign a list or a string to that!):

def guessfixed():
    return [random.randint(0, 9) for _ in range(5)]

(Moreover, print doesn't return a useful value, so assigning the result of print to y didn't make sense either. print generates output to standard output, but simply returns None.)

Also, every time you call guessfixed() you are creating a new guess.

I'm guessing you want your loop to look like this instead:

while True:
    next_guess = guessfixed()
    if next_guess == mylistkey:
        print("It worked!")
        break
    # else
    print("It did not quite work")

Putting the result in a variable is not strictly necessary here, but it helps you see what's going on (you can add print(next_guess) at convenient places in the code to see what happened).

More fundamentally, you probably want to use a brute-force algorithm rather than generate random guesses. The further along you go with random guesses, the more likely you are to guess a combination which you have already tried at least once before.

See also Asking the user for input until they give a valid response and Testing all combinations in Python

Upvotes: 1

gdanton
gdanton

Reputation: 310

Great job attempting this James!

In-order to truly answer your question we need to know the use case of comparing the function return with the list. Read more on the python list from here - https://www.w3schools.com/python/python_lists.asp

There are few different things that you need to look at in the program

  • You don't need the argument for guessfixed as you are hardcoding the values inside the function
  • For all function calls you need parenthesis - ()
  • Making up a string to compare with a list is not a good idea.
  • I did not get the use of While loop here in this program

However you have made a really good start. Keep it up!!

Upvotes: 0

Related Questions