2014-10-31 5 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

+2

* "전혀 작동하지 않습니다"*는 문제에 대한 설명이 아닙니다. 코드에서 무엇을하고 싶습니까? 대신 무엇을합니까? –

+2

while 루프를 사용하십시오 –

답변

-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

하하 그 위인입니다, 나가서 고마워요! 전설! –

관련 문제