您的位置:

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


Python 程序:猜字游戏

2022-07-24
python猜数字游戏脚本(python怎么做猜数字游戏)

2022-11-15
python猜成语游戏代码,python猜谜游戏

2022-11-20
Python猜词游戏

Python猜词游戏是一种有趣的游戏,通过编写代码实现了猜词的功能。下面将从多个方面介绍Python猜词游戏的实现。 一、游戏规则 1、游戏开始时,程序会随机选择一个单词作为答案。 2、玩家需要根据程

2023-12-08
java猜拳,java猜拳小游戏

2023-01-08
php猜单词游戏,玩猜字游戏英文单词

2022-11-29
java猜数字游戏,JAVA猜数字游戏实验报告

2023-01-09
Python小游戏代码简单

2023-05-16
用Python做游戏

2023-05-17
最新python学习笔记3,python基础笔记

2022-11-17
猜数字游戏有10次机会代码js(猜数字游戏的代码)

本文目录一览: 1、要编一个猜数字的程序 猜10次没猜到就失败 怎么编 2、求JAVA计算器和猜数的程序代码 3、js数组:有一个4个长度的数组,每个位置装有0-9之间的随机数字,有10次输入的机会来

2023-12-08
jsp编程猜数(用jsp编写猜数字游戏)

本文目录一览: 1、用JSP编写一个猜数游戏程序 2、应用jsp编译猜数字游戏 1到100内 使用者想数字由电脑来猜 使用者告诉电 3、jsp中写猜数字的 用JSP编写一个猜数游戏程序 在一个页面上帮

2023-12-08
关于python打字游戏cdsn的信息

2022-11-10
c语言输出猜拳,猜拳游戏c程序设计

2023-01-06
java小游戏,java小游戏编程源代码连连看

2022-11-17
python吃豆豆游戏制作,Python怎么做游戏

2022-11-20
php实现简单的猜数字游戏(编写一个猜数字的小游戏)

2022-11-10
java猜数字,java猜数字游戏实验报告

2023-01-09
java猜拳游戏,java猜拳游戏编程代码简单刘备

2022-12-02
用python写个小游戏,python写一个游戏

2022-11-17