需要帮助使用random.randint命令制作单词猜谜游戏

时间:2013-12-03 22:22:21

标签: python

我已经设置了一些作业来制作一个单词猜谜游戏,我已经让它在大多数情况下工作但是在这一点上我使用random.choice并且该命令允许相同的字符串重复多次。我需要知道在这个例子中如何使用random.randint。

    """ This is a guessing game which allows the person operating the program to
guess what i want for christmas"""
import random
sw = ("Trainers")
print ("In this game you will have 10 chances to guess what I want for christmas, you start with 10 points, each time you guess incorrectly you will be deducted one point. Each time you guess incorrectly you will be given another clue.") 
clue_list = ["They are an item of clothing", "The item comes in pairs", "The Item is worn whilst playing sport", "The item is an inanimate object", "The item can be made of leather","They come in differant sizes", "They have laces", "can be all differant colours", "the item has soles", "Im getting it for christmas ;)"] 

def guessing_game(sw, clue_list):
    x = 10
    while x<=10 and x > 0:
        answer = input("What do I want for christmas?")
        if sw.lower() == answer and answer.isalpha():
            print ("Good Guess thats what I want for christmas")
            print ("You scored %s points" % (x))
            break

        else:
            print ("incorrect, " + random.choice(clue_list))
            x -=1
    if x == 0:
        print ("You lost, try again")

guessing_game(sw, clue_list)

1 个答案:

答案 0 :(得分:0)

我认为你想要实施:

clueint = random.randint(0, len(clue_list))
print("incorrect, " + clue_list[clueint])

确保相同的线索不显示两次声明一个空列表来存储OUTSIDE中的函数定义:

clues_shown = []

然后将每个int添加到列表中:

def showClue():
    clueint = random.randint(0, len(clue_list))
    if clueint in clues_shown:
        showClue()
    else: 
        clues_shown.append(clueint)
        print("incorrect, " + clue_list[clueint])