【python教程入门学习】Python Hangman猜字游戏

218 篇文章 7 订阅
207 篇文章 6 订阅

这是经典猜字游戏”Hangman”的Python脚本。要猜的词用一行破折号表示。如果玩家猜出单词中存在的字母,则脚本会将其写入所有正确的位置。玩家有10个回合来猜测这个词。您可以通过更改变量轻松自定义游戏。

Hangman脚本
确保您了解每一行的作用。

#importing the time module
import time

#welcoming the user
name = raw_input("What is your name? ")

print "Hello, " + name, “Time to play hangman!”

print "
"

#wait for 1 second
time.sleep(1)

print “Start guessing…”
time.sleep(0.5)

#here we set the secret
word = “secret”

#creates an variable with an empty value
guesses = ‘’

#determine the number of turns
turns = 10

Create a while loop

#check if the turns are more than zero
while turns > 0:

# make a counter that starts with zero
failed = 0             

# for every character in secret_word    
for char in word:      

# see if the character is in the players guess
    if char in guesses:    

    # print then out the character
        print char,    

    else:

    # if not found, print a dash
        print "_",     
   
    # and increase the failed counter with one
        failed += 1    

# if failed is equal to zero

# print You Won
if failed == 0:        
    print "

You won"

# exit the script
    break              

print

# ask the user go guess a character
guess = raw_input("guess a character:") 

# set the players guess to guesses
guesses += guess                    

# if the guess is not found in the secret word
if guess not in word:  

 # turns counter decreases with 1 (now 9)
    turns -= 1        

# print wrong
    print "Wrong

"

# how many turns are left
    print "You have", + turns, 'more guesses' 

# if the turns are equal to zero
    if turns == 0:           

    # print "You Loose"
        print "You Loose

"
在这里插入图片描述

猜单词游戏Hangman

Python学习路线图:(路线

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值