【问题标题】:Python List slicing does not work in my guessing gamePython 列表切片在我的猜谜游戏中不起作用
【发布时间】:2019-12-25 12:47:57
【问题描述】:

我正在尝试创建一个游戏,其中用户想象一个从 1 到 20 的数字,然后计算机尝试猜测它。 以此目的: 1. 我创建了一个从 1 到 20 的所有数字的列表。 2. 程序从列表中选择一个随机数并询问该虚数是小于还是大于随机数。 3. 用户回答“j”或“n” 4. 每次猜测后,列表从冗余数字中切片 5. 最终,列表中应该只有一个数字,计算机应该得出正确的数字。 代码:

import random
d = list(range(1,21))
tries = []
while True:
    if len(d) > 1:
        x = random.choice(d)
        tries.append(1)
        print(f"Is your number bigger than {x}?")
        answer = input()
        if answer == 'j':
            del d[:x]
            print(d)
        if answer == 'n':
            del d[x:]
            print(d)
        tries.append(1)
        x = random.choice(d)
        print(f"Is your number smaller than {x}?")
        answer = input()
        if answer == 'j':
            del d[x-1:]
            print(d)
        if answer == 'n':
            del d[:x-1]
            print(d)
    else:
        print(f"Your number is {d[0]}. You guessed with {len(tries)} attempts.")
        break

但是,奇怪的是切片不能正常工作,我多次遇到这样的结果: Output

如您所见,在我确认我的号码小于 9 后,它不会删除 9-13 的号码

【问题讨论】:

    标签: python slice


    【解决方案1】:

    列表的索引会随着列表的缩小而变化;您可以使用i = d.index(x) 找到索引:

    import random
    d = list(range(1,21))
    tries = []
    while True:
        if len(d) > 1:
            x = random.choice(d)
            tries.append(1)
            print(f"Is your number bigger than {x}?")
            answer = input()
            i = d.index(x)
            if answer == 'j':
                del d[:i+1]
            if answer == 'n':
                del d[i+1:]
            print(d)
            tries.append(1)
            x = random.choice(d)
            print(f"Is your number smaller than {x}?")
            answer = input()
            i = d.index(x)
            if answer == 'j':
                del d[i:]
            if answer == 'n':
                del d[:i]
            print(d)
        else:
            print(f"Your number is {d[0]}. You guessed with {len(tries)} attempts.")
            break
    

    【讨论】:

      【解决方案2】:

      您不使用索引而是使用列表的内容进行切片。试试这个来找到你的索引:

      del[:d.index(x)]
      

      你也不需要一个while和一个带break的if语句。 像这样使用while。

      while len(d) > 1:
          pass
          # some more code
      else:
          print(f"Your number is {d[0]}. You guessed with {len(tries)} attempts.")
      

      也不要使用列表并追加。 Eiter 附加猜测的数字或仅使用增量计数器tries = 0/tries += 1

      完整的代码可能如下所示。 即使您只有选择余地,您的代码也需要始终进行尝试。 比你应该限制现在给出有价值答案的问题。 x = random.choice(d[:-1])x = random.choice(d[1:])

      import random
      
      if __name__ == '__main__':
          d = list(range(1, 21))
          tries = 0
          while len(d) > 1:
              if random.choice([True, False]):
                  x = random.choice(d[:-1])
                  tries += 1
                  print(f"Is your number bigger than {x}?")
                  answer = input("Type: j/n\n")
                  while answer != "j" and answer != "n":
                      answer = input("Type: j/n\n")
                  if answer == 'j':
                      del d[:d.index(x) + 1]
                      print(d)
                  else:
                      del d[d.index(x) + 1:]
                      print(d)
              else:
                  tries += 1
                  x = random.choice(d[1:])
                  print(f"Is your number smaller than {x}?")
                  answer = input("Type: j/n\n")
                  while answer != "j" and answer != "n":
                      answer = input("Type: j/n\n")
                  if answer == 'j':
                      del d[d.index(x):]
                      print(d)
                  else:
                      del d[:d.index(x)]
                      print(d)
          else:
              print(f"Your number is {d[0]}. You guessed with {tries} attempts.")
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-24
        • 1970-01-01
        相关资源
        最近更新 更多