簡體   English   中英

Python 列表切片在我的猜謎游戲中不起作用

[英]Python List slicing does not work in my guessing game

我正在嘗試創建一個游戲,用戶可以想象一個 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

然而,奇怪的是切片不能正常工作,我多次遇到這樣的結果:輸出

如您所見,在我確認我的號碼小於 9 后,它不會刪除 9-13 中的數字

列表的索引隨着列表的縮小而變化; 你可以用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

您不使用索引切片,而是使用列表的內容切片。 試試這個來找到你的索引:

del[:d.index(x)]

此外,您不需要 while 和帶中斷的 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.")


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM