man子手是一款经典的文字游戏,参与者需要在时间用完之前猜测尽可能多的秘密单词!因此,这是一个学习新单词的好游戏,一次只能读一个字母!
因此,我们将为该经典游戏“ hangman”编写python脚本。
#importing the time module
import time
#welcoming the user
name = input("What is your name? ")
print("Hello, " + name, "是时候玩子手了!")
print ("")
#wait for 1 second
time.sleep(1)
print ("Start guessing...")
time.sleep(0.5)
#here we set the secret
word= ("Secret")
word = word.lower()
#creates an variable with an empty value
guesses = ''
#determine the number of turns
turns = 12
# 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 = input("猜一个字符:")
# 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")输出结果
================== RESTART: C:\Python\Python361\hangman1.py ================== What is your name? Raj Hello, Raj 是时候玩子手了! Start guessing... _ _ _ _ _ _ 猜一个字符:s s _ _ _ _ _ 猜一个字符:h Wrong You have 11 more guesses s _ _ _ _ _ 猜一个字符:e s e _ _ e _ 猜一个字符:c s e c _ e _ 猜一个字符:r s e c r e _ 猜一个字符:e s e c r e _ 猜一个字符:t s e c r e t You won >>>