Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何检查一个值是否在列表中,然后用一个变量存储分数每次增加10_Python_List - Fatal编程技术网

Python 我如何检查一个值是否在列表中,然后用一个变量存储分数每次增加10

Python 我如何检查一个值是否在列表中,然后用一个变量存储分数每次增加10,python,list,Python,List,我已经创建了下面的代码,但是希望每次我在Y中时分数都增加,然后显示增加的分数,允许用户输入他们的猜测8次 Y = ["Treasure", "Hi", "Hey", "whoops", "OK", "Hello"] count=0 while count<9: I = str(input("Enter your guess")) if I in Y: score=+10 print('Your score is:',score)

我已经创建了下面的代码,但是希望每次我在Y中时分数都增加,然后显示增加的分数,允许用户输入他们的猜测8次

Y = ["Treasure", "Hi", "Hey", "whoops", "OK", "Hello"]

count=0

while count<9:

    I = str(input("Enter your guess"))
    if I in Y:
        score=+10
        print('Your score is:',score)
    else:
        print("I don't understand")
Y=[“宝藏”,“你好”,“嘿”,“哇”,“好”,“你好”]
计数=0

而当前代码的两个主要问题是:

  • 在进入循环之前,您不会为
    分数设置初始值
  • 您可以使用
    =+
    而不是
    +=
    来尝试增加分数
  • 这是一个不幸的错误组合,因为
    score=+10
    不会抛出错误,而
    score+=10
    (正确的方法)会给出
    NameError
    。请参见下面的更改。除此之外,通过不增加每个循环的
    计数
    ,就可以得到一个无限循环

    Y = ["Treasure", "Hi", "Hey", "whoops", "OK", "Hello"]
    
    count=0
    score = 0 # Set an initial score of 0, before the loop
    
    while count<9:
    
        I = str(input("Enter your guess"))
        if I in Y:
            score =+ 10 # Change to += to increment the score, otherwise it's always 10
            print('Your score is:',score)
        else:
            print("I don't understand")
        count += 1
    
    Y=[“宝藏”,“你好”,“嘿”,“哇”,“好”,“你好”]
    计数=0
    score=0#在循环之前,将初始分数设置为0
    
    而当前代码的两个主要问题是:

  • 在进入循环之前,您不会为
    分数设置初始值
  • 您可以使用
    =+
    而不是
    +=
    来尝试增加分数
  • 这是一个不幸的错误组合,因为
    score=+10
    不会抛出错误,而
    score+=10
    (正确的方法)会给出
    NameError
    。请参见下面的更改。除此之外,通过不增加每个循环的
    计数
    ,就可以得到一个无限循环

    Y = ["Treasure", "Hi", "Hey", "whoops", "OK", "Hello"]
    
    count=0
    score = 0 # Set an initial score of 0, before the loop
    
    while count<9:
    
        I = str(input("Enter your guess"))
        if I in Y:
            score =+ 10 # Change to += to increment the score, otherwise it's always 10
            print('Your score is:',score)
        else:
            print("I don't understand")
        count += 1
    
    Y=[“宝藏”,“你好”,“嘿”,“哇”,“好”,“你好”]
    计数=0
    score=0#在循环之前,将初始分数设置为0
    
    而count完成逻辑需要做的一件事是在最后将count增加1。增量运算符是
    +=
    ,而不是
    =+
    。另一件事是变量分数需要在开始时初始化。因此,下面显示了带有关于缺失部分注释的最终代码

    Y = ["Treasure", "Hi", "Hey", "whoops", "OK", "Hello"]
    
    count = 0
    score = 0 # initialize the score
    
    while count<9:
    
        I = str(input("Enter your guess: "))
        if I in Y:
            score+=10 # increment the score
            print('Your score is: %s' % score)
        else:
            print("I don't understand")
        count += 1 # increment the count
    
    Y=[“宝藏”,“你好”,“嘿”,“哇”,“好”,“你好”]
    计数=0
    分数=0#初始化分数
    
    而count完成逻辑需要做的一件事是在最后将count增加1。增量运算符是
    +=
    ,而不是
    =+
    。另一件事是变量分数需要在开始时初始化。因此,下面显示了带有关于缺失部分注释的最终代码

    Y = ["Treasure", "Hi", "Hey", "whoops", "OK", "Hello"]
    
    count = 0
    score = 0 # initialize the score
    
    while count<9:
    
        I = str(input("Enter your guess: "))
        if I in Y:
            score+=10 # increment the score
            print('Your score is: %s' % score)
        else:
            print("I don't understand")
        count += 1 # increment the count
    
    Y=[“宝藏”,“你好”,“嘿”,“哇”,“好”,“你好”]
    计数=0
    分数=0#初始化分数
    

    while count您的while循环是一个无限循环。将计数增加1将使其成为一个确定循环。另外,将score变量初始化为零。你的while循环是一个无限循环。将计数增加1将使其成为一个确定循环。另外,将score变量初始化为零。这应该会解决问题。

    我不知道问题出在哪里。你只需要在循环之外初始化分数,将
    score=0
    放在
    上方,而
    。同时更改为
    +=
    而不是
    =+
    注意输入是否带有大写字母和小写字母,因为这对输入很重要match@roganjosh
    =+10
    好笑!它被接受是因为一元加运算符@如果答案是肯定的,roganjosh和downvote会产生一些相反的效果。我不明白问题是什么。你只需要在循环之外初始化分数,将
    score=0
    放在
    上方,而
    。同时更改为
    +=
    而不是
    =+
    注意输入是否带有大写字母和小写字母,因为这对输入很重要match@roganjosh
    =+10
    好笑!它被接受是因为一元加运算符@如果答案是确定的,那么roganjosh和downvote会产生一些相反的效果。我希望downvoter能用这个答案解释问题,这样我就能解决它。我希望downvoter能用这个答案解释问题,这样我就能解决它。我现在有没有办法检查列表中的索引点,所以我输入索引点,比如说1,如果它包含,那么分数增加10。你可能应该使用for循环和枚举函数。检查它们是否有任何方法我现在可以检查列表中的索引点,因此我输入索引点,例如1,然后如果它包含,则分数增加10。您可能应该将for循环与枚举函数一起使用。检查他们好答案,但若你们能在代码中也展示你们的意思,那个将是更好的答案,但若你们能在代码中也展示你们的意思,那个将是更好的答案