Python 程序:猜字游戏

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 中开发一个猜字游戏。


原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/241574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 12:40
下一篇 2024-12-12 12:40

相关推荐

  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29

发表回复

登录后才能评论