简体   繁体   English

将新输入与先前存储的输入进行比较

[英]Compare new input with previous stored input

So, let's say I have a list;所以,假设我有一个清单; a random number between 1 and 100 1 到 100 之间的随机数

randomNumber = random.randint(1,100)
list = []

And I take input from a user, in this case a number, and append it to the list above.我从用户那里得到输入,在这种情况下是一个数字,append 到上面的列表中。

guess = int(input('Please make a guess'))

list.append(guess)

My question is: How do I check if the range between the new input from user to the randomNumber , is shorter than the distance between the previous guess to the randomNumber ?我的问题是:如何检查从 user 到randomNumber的新输入之间的范围是否比先前猜测到randomNumber之间的距离短?

I'm working on a Guessing Game, if the context helps如果上下文有帮助,我正在做一个猜谜游戏

Well, the previous input is list[-1].嗯,前面的输入是list[-1]。 Thus, just conpare list[-1] with randomNumber and guess with randomNumber by substraction.因此,只需将 list[-1] 与 randomNumber 进行比较,并通过减法与 randomNumber 猜测。

randomNumber = random.randint(1,100)
list = []

def guess():
    guess = int(input('Please make a guess'))

    list.append(guess)
    comparingTo = len(list)-2 if len(list)-2 > 0 else 0

    if randomNumber - guess < randomNumber - list[comparingTo]: # if the guessed number is lower than the latest array value...
        return True

for this line, list = [] I recommend you to change this variable name because its overriding built-in function list()对于这一行, list = []我建议您更改此变量名称,因为它覆盖了内置 function list()

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

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