简体   繁体   English

你如何比较python中函数的输出?

[英]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.对不起,如果这是一个明显的问题,但我 12 岁,正在尝试编写一些代码来帮助我完成我的科学项目,我正在尝试查看哪种锁最安全。 Basically my problem is, I need to figure out how to compare the output of a function with the if somethinghere() == somethingelse().基本上我的问题是,我需要弄清楚如何将函数的输出与 if somethinghere() == somethingelse() 进行比较。 And by the way, I am using the random module.顺便说一下,我正在使用random模块。 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.我知道这不起作用的唯一方法是因为在完整脚本中,我将键和猜测更改为只有 4 个组合,因此键和猜测最终必须重叠,并且它一直在进行,据说尝试了 100 个组合在我杀死它之前。 According to my tests, the guessfixed changes, and the key does not.根据我的测试,guessfixed 会发生变化,而密钥不会。 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!我 12 岁,所以我不太擅长写问题,但熟能生巧!

You need for guessfixed to return a list -- you want to compare a list to a list, not a list to a string.您需要guessfixed返回一个列表——您想将列表与列表进行比较,而不是将列表与字符串进行比较。 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!):没有必要传入y (当然也不能传入0 —— 你不能给它分配一个列表或一个字符串!):

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 .) (此外, print不会返回有用的值,因此将print的结果分配给y也没有意义print生成输出到标准输出,但只是返回None 。)

Also, every time you call guessfixed() you are creating a new guess.此外,每次您调用guessfixed()您都在创建一个新的猜测。

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).将结果放入变量在这里并不是绝对必要的,但它可以帮助您了解发生了什么(您可以在代码中方便的位置添加print(next_guess)以查看发生了什么)。

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另请参阅询问用户输入直到他们给出有效响应在 Python 中测试所有组合

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从这里阅读有关 python 列表的更多信息 - 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您不需要guessfixed的参数,因为您正在对函数内的值进行硬编码
  • 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我没有在这个程序中使用While循环

However you have made a really good start.然而,你已经有了一个非常好的开始。 Keep it up!!保持!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM