我如何计算我尝试了多少次尝试?

时间:2014-10-31 00:24:53

标签: python count

所以我试图计算猜测混合词的尝试次数。坦率地说,我已经被困住了很长时间,因为这段代码根本不起作用。请帮忙。

#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 个答案:

答案 0 :(得分: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()

答案 1 :(得分:-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()

这将按您希望的方式运作。