我怎么能数一数我试了多少次?

2024-09-29 23:33:01 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想试着猜出这些混淆的单词需要多少次。坦白地说,我被困了这么久,因为这段代码根本不起作用。请帮忙。在

#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()

Tags: 代码importstringcountrandom单词wordwords
2条回答

给你。我为你修好了几件事。我的修复将在下面的代码注释中提到。如果你帮我修改了密码,请告诉我。在

 #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()
#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()

这将按照你想要的方式运行。在

相关问题 更多 >

    热门问题