首页 > 编程知识 正文

Python 程序:猜字游戏

时间:2023-11-26 13:13:30 阅读:309455 作者:LNEP

Python 是一种非常通用的编程语言,被许多大公司采用。它是一种简单易懂的语法,对于那些第一次尝试掌握计算机编程的人来说,它是完美的。它是一种高级编程语言。它的基本设计原则是理解代码和语法,让程序员在几行代码中交流概念。

在本教程中,我们将使用“random模块”来玩猜字的互动游戏。这个游戏是为那些刚刚开始学习用 Python 编码的人准备的,它将向他们概述字符串、循环和条件(如果、否则)语句。

random模块:

有时,我们需要计算机从指定的范围中选择随机数,从一组中随机选择一个元素,从一副牌中选择随机牌,掷硬币,等等。random模块允许访问支持这些操作的功能。这些操作之一是 random.choice()技术(从元组、列表或字符串中返回未指定的项。)我们将利用它从我们生成的一组术语中选择随机单词。

猜字游戏:

游戏包括一系列单词,我们的翻译将从中选择一个随机单词。玩家必须首先输入他们的名字,然后被要求猜测他们选择的字母表。如果随机单词由字母表组成,它将显示在输出中(有适当的位置);否则,程序会提示您选择另一种字母表。用户将被给予 12 圈(可根据修改)来确定完整的单词。下面是 Python 实现的一个示例:

代码:


# First, we will import the library that we will be using to choose
# any random words from a list of words
import random as rdm

# Here the user will be asked to enter their name first
name1 = input("What is your NAME ? ")

print("Best of Luck! ", name1)

words1 = ['Donkey', 'Aeroplane', 'America', 'Program',
         'Python', 'language', 'Cricket', 'Football',
         'Hockey', 'Spaceship', 'bus', 'flight']

# rdm.choice() function will choose one random word from the gven list of words
word1 = rdm.choice(words1)

print ("Please guess the characters: ")

guesses1 = ''

# we can use any number of turns here
turns1 = 10

while turns1 > 0:

    # counting the number of times a user is failed to guess the right character
    failed1 = 0

    # all the characters from the input word will be taken one at a time.
    for char in word1:

        # here, we will comparing that input character with the character in guesses1
        if char in guesses1:
            print(char)

        else:
            print("_")

            # for every failure of the user 1 will be incremented in failed1
            failed1 += 1

    if failed1 == 0:
        # user will win the game if failure is 0 and 'User Win' will be given as output
        print("User Win")

        # this will print the correct word
        print("The correct word is: ", word1)
        break

    # if the user has input the wrong alphabet then
    # it will ask user to enter another alphabet
    guess1 = input("Guess another character:")

    # every input character will be stored in guesses
    guesses1 += guess1

    # here, it will check input with the character in word
    if guess1 not in word1:

        turns1 -= 1

        # if the input character doesnot match the word
        # then "Wrong Guess" will be given as output
        print("Wrong Guess")

        # this will print the number of turns left for the user
        print("You have ", + turns1, 'more guesses ')

        if turns1 == 0:
            print("User Loose")

输出:

What is your NAME ?  JavaTpoint
Best of Luck!  JavaTpoint
Please guess the characters: 
_
_
_
_
_
_
_
Guess another character: D
Wrong Guess
You have  9 more guesses 
_
_
_
_
_
_
_
Guess another character: C
Wrong Guess
You have  8 more guesses 
_
_
_
_
_
_
_
Guess another character: H
Wrong Guess
You have  7 more guesses 
_
_
_
_
_
_
_
Guess another character: F
Wrong Guess
You have  6 more guesses 
_
_
_
_
_
_
_
Guess another character: f
Wrong Guess
You have  5 more guesses 
_
_
_
_
_
_
_
Guess another character: b
Wrong Guess
You have  4 more guesses 
_
_
_
_
_
_
_
Guess another character: P
P
_
_
_
_
_
_
Guess another character: r
P
r
_
_
r
_
_
Guess another character: o
P
r
o
_
r
_
_
Guess another character: g
P
r
o
g
r
_
_
Guess another character: a
P
r
o
g
r
a
_
Guess another character: m
P
r
o
g
r
a
m
User Win
The correct word is:  Program

结论

在本教程中,我们讨论了如何使用random模块,在 Python 中开发一个猜字游戏。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。