如何打印单词列表中包含5个字母或更少的Python 2.7的单词?

时间:2015-06-17 19:05:24

标签: python list python-2.7

我是python的新手,你可以通过我下面的糟糕编码看到它。如果你要玩这个游戏,你可能会发现它真的很难,所以为了让它变得更容易,我想这样做它只会给用户5个字母或更少的单词。我目前的代码是:

word_site = "http://www.instructables.com/files/orig/FLU/YE8L/H82UHPR8/FLUYE8LH82UHPR8.txt"
response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()
randomword=(random.choice(WORDS))

目前上面的代码从该单词列表中获取任何随机单词,我希望它只能获得包含5个或更少字母的单词。

以下是我的所有代码,如果它可以帮助您:

import random
import urllib2
loop=True
while loop==True:
    name=raw_input("Hello there! What might your name be? ")
    yesorno=raw_input("Hello " + name + ", would you like to play a friendly game of Guess the Word? Answer either 'yes' or 'no': ")
    if yesorno == "yes":
        loop=False
        print "Ok then, here we go!"
        print "\n"
        print "You have 3 guesses"
        word_site = "http://www.instructables.com/files/orig/FLU/YE8L/H82UHPR8/FLUYE8LH82UHPR8.txt"
        response = urllib2.urlopen(word_site)
        txt = response.read()
        WORDS = txt.splitlines()
        randomword=(random.choice(WORDS))
        lengthrandomword=len(randomword)
        print "I'm thinking of a word with " + str(lengthrandomword) + " letters"
        print "\n"
        if lengthrandomword==2:
            print "_ _"
            print "\n"
        elif  lengthrandomword==3:
            print "_ _ _"
            print "\n"
        elif  lengthrandomword==4:
            print "_ _ _ _"
            print "\n"
        elif  lengthrandomword==5:
            print "_ _ _ _ _"
            print "\n"
        elif  lengthrandomword==6:
            print "_ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==7:
            print "_ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==8:
            print "_ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==9:
            print "_ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==10:
            print "_ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==11:
            print "_ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==12:
            print "_ _ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==13:
            print "_ _ _ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==14:
            print "_ _ _ _ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        elif  lengthrandomword==15:
            print "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _"
            print "\n"
        else:
            print "I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword
        print "\n"
        userguess=raw_input("Try and guess the word: ")
        input = randomword
        output = ""
        for i in input.split():
            output += i[0]
        firstletter=output
        if randomword.endswith("a"):
            lastletter="a"
        elif randomword.endswith("b"):
            lastletter="b"
        elif randomword.endswith("c"):
            lastletter="c"
        elif randomword.endswith("d"):
            lastletter="d"
        elif randomword.endswith("e"):
            lastletter="e"
        elif randomword.endswith("f"):
            lastletter="f"
        elif randomword.endswith("g"):
            lastletter="g"
        elif randomword.endswith("h"):
            lastletter="h"
        elif randomword.endswith("i"):
            lastletter="i"
        elif randomword.endswith("j"):
            lastletter="j"
        elif randomword.endswith("k"):
            lastletter="k"
        elif randomword.endswith("l"):
            lastletter="l"
        elif randomword.endswith("m"):
            lastletter="m"
        elif randomword.endswith("n"):
            lastletter="n"
        elif randomword.endswith("o"):
            lastletter="o"
        elif randomword.endswith("p"):
            lastletter="p"
        elif randomword.endswith("q"):
            lastletter="q"
        elif randomword.endswith("r"):
            lastletter="r"
        elif randomword.endswith("s"):
            lastletter="s"
        elif randomword.endswith("t"):
            lastletter="t"
        elif randomword.endswith("u"):
            lastletter="u"
        elif randomword.endswith("v"):
            lastletter="v"
        elif randomword.endswith("w"):
            lastletter="w"
        elif randomword.endswith("x"):
            lastletter="x"
        elif randomword.endswith("y"):
            lastletter="y"
        elif randomword.endswith("z"):
            lastletter="z"
        else:
            lastletter="I CODED SOMETHING WRONG!"
        if userguess != randomword:
            print "You guessed wrong..."
            print "\n"
            print "I'll give you a hint: the word STARTS with the letter " + firstletter + " and ENDS with the letter " + lastletter
            userguess=raw_input("I'll give you another chance to guess the word: ")
            if userguess != randomword:
                input = randomword
                output = ""
                for i in input.split():
                    output += i[1]
                secondletter=output
                print "You guessed wrong..."
                print "\n"
                print "I'll give you another hint: the second letter of the word is " + secondletter
                userguess=raw_input("I'll give you one last chance to guess the word: ")
                if userguess != randomword:
                    print "You guessed wrong..."
                    print "\n"
                    print "You're out of chances... the word was: " + randomword
                    print "I'm sorry to say this but..."
                    print "YOU LOST!"
                    print "\n"

                elif userguess == randomword:
                    print "You guessed right!"
                    print "YOU WON!"
                    print "\n"
                else:
                    print "I made a mistake in the code somewhere?!"
        elif userguess == randomword:
            print "You guessed right!"
            print "YOU WON!"
            print "/n"
        else:
            print "I made a mistake in the code somewhere?!"
        mindchange=raw_input("Wanna play again? If you do, just type 'Start'. If you don't then type 'Exit': ")
        if mindchange=="Exit":
            loop=False
        elif mindchange=="Start":
            loop=True
        else:
            print "You didn't type either 'Start' or 'Exit' : remember to include the Capitals!"
            print "\n"
    elif yesorno == "no":
        mindchange=raw_input("Well that's too bad, I was looking forward to beating you... well if you change your mind just type 'Start' but if you still don't want to play just type 'Exit':")
        if mindchange=="Exit":
            loop=False
        elif mindchange=="Start":
            loop=True
        else:
            print "You didn't type either 'no' or 'yes' : remember to not use capitals! "
            print "\n"

这是返回的内容:

Hello there! What might your name be? Alex
Hello Alex, would you like to play a friendly game of Guess the Word? Answer either 'yes' or 'no': yes
Ok then, here we go!


You have 3 guesses
I'm thinking of a word with 14 letters


_ _ _ _ _ _ _ _ _ _ _ _ _ _




Try and guess the word: SOMEWORD
You guessed wrong...


I'll give you a hint: the word STARTS with the letter o and ENDS with the letter g
I'll give you another chance to guess the word: SOMEOTHERWORD
You guessed wrong...


I'll give you another hint: the second letter of the word is v
I'll give you one last chance to guess the word: SOMEOTHERWORD
You guessed wrong...


You're out of chances... the word was: overregulating
I'm sorry to say this but...
YOU LOST!


Wanna play again? If you do, just type 'Start'. If you don't then type 'Exit': 

如果你也可以指出我效率低下的地方(可能在任何地方),那将是值得赞赏的。

4 个答案:

答案 0 :(得分:3)

WORDS = txt.splitlines()
WORDS_5_or_less = list(filter(lambda x: len(x) <= 5, WORDS))

然后从WORDS_5_or_less而非WORDS

中选择

答案 1 :(得分:1)

使用列表理解。

short_words = [word for word in txt.splitlines() if len(word) <= 5]

答案 2 :(得分:1)

此外,你可以摆脱if s的重复阻止:

    if randomword.endswith("a"):
        lastletter="a"
    elif randomword.endswith("b"):
        lastletter="b"
    elif randomword.endswith("c"):
        lastletter="c"
    elif randomword.endswith("d"):
        lastletter="d"
    elif randomword.endswith("e"):
        lastletter="e"
    ...
    elif randomword.endswith("z"):
        lastletter="z"
    else:
        lastletter="I CODED SOMETHING WRONG!"

然后得到最后一封信:

    lastletter = randomword[-1]

    if not lastletter.isalpha():
        lastletter="I CODED SOMETHING WRONG!"

同样适用于其他重复的块:

    if lengthrandomword==2:
        print "_ _"
        print "\n"
    elif  lengthrandomword==3:
        print "_ _ _"
        print "\n"
    ...
    elif  lengthrandomword==15:
        print "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _"
        print "\n"
    else:
        print "I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword

你可以缩短为:

    if lengthrandomword in range(2,16):
        print(' '.join('_' * lengthrandomword))
        print('\n')
    else:
        print("I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword)

答案 3 :(得分:0)

您需要使用一些数据结构,比如(比如字典)和一些循环。这不是你应该用任何语言编程的方式,至少在python或ruby等语言中。