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
结论
在本教程中,我们讨论了如何使用Python中的随机模块开发一个猜词游戏。