• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python开发的简单的猜单词游戏代码

python 水墨上仙 2235次浏览

python开发的简单的猜单词游戏代码,这是一个简单的人机交互的小游戏,用户有10次机会来猜对单词,如果用户猜对了其中的一个字母,系统会在正确的位置显示该字母。

#importing the time module
import time
 
#welcoming the user
name = raw_input("What is your name? ")
 
print "Hello, " + name, "Time to play hangman!"
 
print "\n"
 
#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 "\nYou 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\n"   
  
    # 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\n" 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明python开发的简单的猜单词游戏代码
喜欢 (0)
加载中……