圣诞节记忆游戏

时间:2017-12-12 14:41:19

标签: python

感谢您给我们的回复。我非常感谢小伙子。我真的很爱你stackoverflow。我们已经达到了相当不错的成绩,感谢大家。我真的很感激。

1 个答案:

答案 0 :(得分:0)

我认为发生的事情是你的range迭代提前停止了。如果我理解正确的话,游戏应该告诉玩家除了可用名称中的一个名字。这只通过迭代range(difficulty - 1)

来解决

我根据你的输出构建了游戏的其余部分。希望这可以帮助!圣诞快乐。

import random

print("+----------------------------------------+")
print("| Welcome to a christmas memory game!    |")
print("+----------------------------------------+")

all_names = ['Kalle', 'Rickard', 'Linus', 'Jacob', 'Oscar', 'Lisa', 'Julia', 
'Selma', 'Pelle', 'Lina', 'Alieu', 'Maja', 'Filip', 'Linnea', 'Isak', 
'Ludde', 'Hanna', 'Maria', 'Anders', 'David']

all_presents = ['Dator', 'Bil', 'Penna', 'Boll', 'Tröja', 'Pengar', 
'Kortlek', 'Skor', 'Katt', 'Gitarr', 'Xbox', 'Playstation', 
'Tangentbord', 'Byxor', 'Laptop', 'Mixer', 'Skidor', 'Snowboard', 
'Presentkort', 'Bord']

difficulty = int(input("Choose difficulty (1-20) "))
names = random.sample(all_names, difficulty)
presents = random.sample(all_presents, difficulty)

print(", ".join(names), 'has gathered around the christmas tree')
print("Santa has the following presents:", ", ".join(presents))

# range(difficulty - 1) so we don't say what *everyone* got
for i in range(difficulty - 1):
    name = random.choice(names)
    names.remove(name)
    present = random.choice(presents)
    presents.remove(present)
    print(name, "got", present)
    input("Press enter to proceed")

name_guess = input("Who didn't get a present?: ")
present_guess = input("What present did he/she get?: ")

def correct_guess(guess, collection):
    # This is so that the user doesn't need to capitalize the guesses correctly
    if guess.lower() in [g.lower() for g in collection]:
        return True
    return False

if correct_guess(name_guess, names) and correct_guess(present_guess, presents):
    print("Good job!! You won :-)")
else:
    print("Sorry, you were wrong. Correct answer was:", names[0], "got", presents[0])
("Y")