2016-05-12 92 views
0

这是我第一次用Python编程。 我正在尝试创建一个hang子手游戏。Python Hang子手游戏,取代多次出现的字母

所以我的问题:我该如何检查

  1. 如果人猜测,已经猜到了,并在那里把它列入了一封信。
  2. 检查他们是否输入有效的字母不是多个字母或数字。
  3. 如果是'Good'这样的词,会发生什么情况,如果出现这种情况,他们就会猜测'o'在我的代码被破坏的时刻。此外,在地球上这将包括在代码中?

这里是我的代码

import random 
import math 
import time 
import sys 


def hangman(): 
    if guess == 5: 
      print " ----------|\n /\n|\n|\n|\n|\n|\n|\n______________" 
      print 
      print "Strike one, the tigers are getting lonely!" 
      time.sleep(.5) 
      print "You have guessed the letters- ", guessedLetters 
      print " ".join(blanks) 
      print 

    elif guess == 4: 
     print " ----------|\n/  O\n|\n|\n|\n|\n|\n|\n______________" 
     print 
     print "Strike two, pressure getting to you?" 
     time.sleep(.5) 
     print "You have guessed the letters- ", guessedLetters 
     print " ".join(blanks) 
     print 

    elif guess == 3: 
     print " ----------|\n/  O\n|   \_|_/ \n|\n|\n|\n|\n|\n______________" 
     print 
     print "Strike three, are you even trying?" 
     time.sleep(.5) 
     print "You have guessed the letters- ", guessedLetters 
     print " ".join(blanks) 
     print 

    elif guess == 2: 
     print " ----------|\n/  O\n|   \_|_/ \n|   |\n|\n|\n|\n|\n______________" 
     print 
     print "Strike four, might aswell giveup, your already half way to your doom. Oh wait, you can't give up." 
     time.sleep(.5) 
     print "You have guessed the letters- ", guessedLetters 
     print " ".join(blanks) 
     print 

    elif guess == 1: 
     print " ----------|\n/  O\n|   \_|_/ \n|   |\n|  /\\ \n|\n|\n|\n______________" 
     print 
     print "One more shot and your done for, though I knew this would happen from the start" 
     time.sleep(.5) 
     print "You have guessed the letters- ", guessedLetters 
     print " ".join(blanks) 
     print 

elif guess == 0: 
     print " ----------|\n/  O\n|   \_|_/ \n|   |\n|  /\\ \n|  GAME OVER!\n|\n|\n______________" 

     print "haha, its funny cause you lost." 
     print "p.s the word was", choice 
     print 


words = ["BAD","RUGBY","JUXTAPOSED","TOUGH","HYDROPNEUMATICS"] 

choice = random.choice(words) 


lenWord = len(choice) 

guess = 6 

guessedLetters = [] 

blanks = [] 

loading = ".........." 

print "Loading", 
for char in loading: 
    time.sleep(.5) 
    sys.stdout.write(char) 
    sys.stdout.flush() 
time.sleep(.5) 
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" 

print "Great I'm glad to see you have finally woken." 
time.sleep(1) 
raw_input("Did you have a nice journey? ") 
time.sleep(2) 

print """ 
Oh wait, I don't care. 
I have brought you to this island simply for my amusment. I also get     paid to do this. 
""" 

time.sleep(2) 
print""" 
Don't worry this isn't all for nothing. 
I'm sure the tigers will enjoy your company. 
""" 

time.sleep(2) 
print""" 
Hold on, let us make things interesting. 
""" 

time.sleep(2) 
print "I will let you live if you complete an impossible game!" 
time.sleep(2) 
print "A game know as hangman!" 
time.sleep(2) 
print "HAHAHAHAHAH, I am so evil, you will never escape!" 
time.sleep(2) 
print "Enjoy your stay :)" 

time.sleep(1) 
print 
print "Alright lets begin! If you wish to guess the word type an '!' and you will be prompted" 
time.sleep(.5) 
print 

for s in choice: 
    missing = choice.replace(choice, "_") 
    blanks.append("_") 
    print missing, 
print 
time.sleep(.5) 

while guess > 0: 
    letterGuess = raw_input("Please enter a letter: ") 
    letterGuess = letterGuess.upper() 

    if letterGuess == "!": 
     print "If you guess the FULL word correcly then you win, if incorrect you die. Simple." 
     fullWordGuess = raw_input("What is the FULL Word? ") 
     fullWordGuess = fullWordGuess.upper() 
     if fullWordGuess == choice: 
      print "You must have hacked this game" 
      time.sleep(.5) 
      print "Looks like you beat an impossible game! \nGood Job \nI'll show myself out." 
      break 
     else: 
      print "You lost, I won, you're dead :) Have a nice day!" 
      print "P.S The word was ", choice 
      break 
    else: 
     print 

    if letterGuess in choice: 
     location = choice.find(letterGuess) 

     blanks.insert(location, letterGuess) 
     del blanks[location+1] 

     print " ".join(blanks) 

     guessedLetters.append(letterGuess) 
     print 
     print "You have guessed the letters- ", guessedLetters 

     if "_" not in blanks: 
      print "Looks like you beat an impossible game! \nGood Job \nI'll show myself out." 
      break 
     else: 
      continue 

    else: 

     guessedLetters.append(letterGuess) 
     guess -= 1 
     hangman() 
+0

不要使用find,遍历字符串,只需使用str.join替换任何出现的事件,这将处理多次重复 –

+0

酷游戏!这里有几个编码技巧。 (1)重复通常是不好的,试图摆脱它。 (2)如果你使用三重引号,Python有多行字符串。请参阅http://pastebin.com/MVc3D03q –

回答

0

我并没有真正按照你的代码,因为似乎有一些问题与您的缩进,以及许多打印的搞糊涂了一点,但这里是我会怎样建议提出您的问题:

1.1。创建猜字的列表:

guessed_chars = [] 
if(guess in guessed_chars): 
    guessed_chars.append(guess) 
    #do whatever you want to do with the guess. 
else: 
    print("You already guessed that.") 
    #do what ever you want to do if you already guessed that. 

1.2比方说球员有这个字的GES是word = "snake"。要获得指数(因为一个字符串就是一个在Python charcters的名单,只知道它是不可改变的),其中猜字母的单词,你可以再使用这样的:

reveal = "_"*len(word) 
temp = "" 
for i, j in enumerate(word): 
    if j == guess: 
     temp += guess 
    else: 
     temp += reveal[i] 

reveal = temp 
print(reveal) 
  • 也许不是幻想,但它应该工作:

    if (guess not in [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]):

  • 我的建议为1.2你只需要检查if(word==reveal):重复的charcters没有问题。

  • 因为我不只是一个业余爱好者,但可能会有一个更专业的方式。 Nex时间可能会分裂你的问题,并首先单独查找他们会更好,因为我非常肯定你之前至少有部分问题被问过。