📜  用于猜词游戏的Python程序

📅  最后修改于: 2022-05-13 01:54:38.528000             🧑  作者: Mango

用于猜词游戏的Python程序

Python是一种强大的多用途编程语言,被多家大公司使用。它具有简单易用的语法,使其成为第一次尝试学习计算机编程的人的完美语言。它是一种高级编程语言,其核心设计理念是代码可读性和允许程序员用几行代码表达概念的语法。
在本文中,我们将使用随机模块来制作一个猜词游戏。该游戏适合初学者学习Python编码,并向他们简要介绍如何使用字符串、循环和条件 (If, else) 语句。

在这个游戏中,有一个单词列表,我们的解释器会从中随机选择 1 个单词。用户首先必须输入他们的名字,然后会被要求猜测任何字母。如果随机单词包含该字母表,它将显示为输出(位置正确),否则程序将要求您猜测另一个字母表。用户将有 12 轮(可相应更改)来猜测完整的单词。
下面是Python的实现:

Python3
import random
# library that we use in order to choose
# on random words from a list of words
 
name = input("What is your name? ")
# Here the user is asked to enter the name first
 
print("Good Luck ! ", name)
 
words = ['rainbow', 'computer', 'science', 'programming',
         'python', 'mathematics', 'player', 'condition',
         'reverse', 'water', 'board', 'geeks']
 
# Function will choose one random
# word from this list of words
word = random.choice(words)
 
 
print("Guess the characters")
 
guesses = ''
 
# any number of turns can be used here
turns = 12
 
 
while turns > 0:
     
    # counts the number of times a user fails
    failed = 0
     
    # all characters from the input
    # word taking one at a time.
    for char in word:
         
        # comparing that character with
        # the character in guesses
        if char in guesses:
            print(char, end=" ")
             
        else:
            print("_")
            print(char, end=" ")
             
            # for every failure 1 will be
            # incremented in failure
            failed += 1
             
 
    if failed == 0:
        # user will win the game if failure is 0
        # and 'You Win' will be given as output
        print("You Win")
         
        # this print the correct word
        print("The word is: ", word)
        break
     
    # if user has input the wrong alphabet then
    # it will ask user to enter another alphabet
    print()
    guess = input("guess a character:")
     
    # every input character will be stored in guesses
    guesses += guess
     
    # check input with the character in word
    if guess not in word:
         
        turns -= 1
         
        # if the character doesn’t match the word
        # then “Wrong” will be given as output
        print("Wrong")
         
        # this will print the number of
        # turns left for the user
        print("You have", + turns, 'more guesses')
         
         
        if turns == 0:
            print("You Loose")


输出:

What is your name? Gautam
Good Luck!  Gautam
Guess the characters
_
_
_
_
_
guess a character:g
g
_
_
_
_
guess a character:e
g
e
e
_
_
guess a character:k
g
e
e
k
_
guess a character:s
g
e
e
k
s
You Win
The word is:  geeks