2014-10-31 44 views
-1

因此,我試圖猜出需要多少次嘗試才能猜出混淆的單詞。坦率地說,我一直堅持這麼久,因爲這個代碼根本不起作用。請幫忙。我如何計算我已經採取了多少次嘗試?

#Word Jumble Game 
import random 
import string 


words = ['Jumble', 'Star', 'Candy', 'Wings', 'Power', 'String', 'Shopping', 'Blonde', 'Steak', 'Speakers', 'Case', 'Stubborn', 'Cat', 'Marker', 'Elevator', 'Taxi', 'Eight', 'Tomato', 'Penguin', 'Custard'] 

def jumbled(): 
    word = string.lower(random.choice(words)) 
    jumble = list(word) 
    random.shuffle(jumble) 
    scrambled = "".join(jumble) 
    print '\n',scrambled,'\n' 

    guess = raw_input('Guess the word: ') 

    count=0 

    if guess == word: 
     print '\n','Correct!' 
    else: 
     print '\n','Try again!','\n',jumbled() 
    count+=1 

jumbled() 
+2

請修復您的縮進。看起來你可能想在某處使用循環 – monkut 2014-10-31 00:27:09

+2

*「完全不起作用」*不是問題描述。你想要你的代碼做什麼?它做什麼呢? – 2014-10-31 00:28:30

+2

使用一段時間循環 – 2014-10-31 00:32:22

回答

-1
#Word Jumble Game 
import random 
import string 


words = ['Jumble', 'Star', 'Candy', 'Wings', 'Power', 'String', 'Shopping', 'Blonde', 'Steak', 'Speakers', 'Case', 'Stubborn', 'Cat', 'Marker', 'Elevator', 'Taxi', 'Eight', 'Tomato', 'Penguin', 'Custard'] 


def jumble_word(word_list): 
    word = random.choice(word_list) 
    jumble = ''.join(random.shuffle(list(word))) 
    return word, jumble 

def play(word_list): 
    word, jumble = jumble_word(word_list) 
    count = 0 
    while True: 
     print('Guess the word: {}'.format(jumble)) 
     guess = input('Enter your guess: ') 
     guess += 1 
     if word == guess: 
      print('Correct! You got the word in {} tries!'.format(count)) 
      break 
     else: 
      print('Guess again! You have guessed {} times.'.format(count)) 


if __name__ == '__main__': 
    play() 

這功能,您希望它的方式。

0

你走了。我爲你解決了一些問題。我的修復將在下面我的代碼的評論中提及。讓我知道這是否有助於您修復代碼。

#Word Jumble Game 
    import random 
    import string 

    def jumbled(): 
     words = ['Jumble', 'Star', 'Candy', 'Wings', 'Power', 'String', 'Shopping', 'Blonde', 'Steak', 'Speakers', 'Case', 'Stubborn', 'Cat', 'Marker', 'Elevator', 'Taxi', 'Eight', 'Tomato', 'Penguin', 'Custard'] 
     count =0      ## initialize the count value. 
     flag = True     ## I have used flag as to when to stop executing the program (depending on the value of flag) 
     while flag:     ## Let it run infinitely till user gets right answer! 
      word = string.lower(random.choice(words)) 
      jumble = list(word) 
      random.shuffle(jumble) 
      scrambled = "".join(jumble) 
      print '\n',scrambled,'\n' 

      guess = raw_input('Guess the word: ') 

      if guess.lower() == word: ## I have used guess.lower() to match with word.lower(). You had missed out to convert guess to lower case! 
       print '\n','Correct!' 
       count+=1       ## increment the counter 
       flag = False 
       print "Number of guesses you took to get right answer=",count ## print the count 
      else: 
       print '\n','Try again!','\n' 
       count+=1       ## increment count for every wrong answer 
       print "You had %d tries" %count ## let user know how many tries he had. 
    jumbled() 
+0

哈哈,這是偉大的傢伙,感謝您的出路!傳說! – 2014-11-01 14:14:43

相關問題