Python猜词游戏

时间:2017-05-02 03:27:07

标签: python string

目前,我和我的同伴正在进行一项小型运动调用猜词游戏。但是,当我们运行此功能时。它没有显示符合教师期望的正确输出。我们也坚持使用def check_letter。因此,我想知道如果我做错了,你们是否可以为我检查一下:

import random




def read_word_list(word_list):
   str_list = []
     for line in word_list:
        for s in line.split():
           try:
              x= str(s)
              str_list.append(x)
           except ValueError:
              pass
        return str_list

words = ["introduction", "programming", "database", "concepts", "techniques", "security"]
my_list = read_word_list(words)
random_word = random.choice(my_list)
blanks = '_ ' * len(random_word)
print()
print ("Word: ",blanks)

def greetings():
    name = input("What is your name? ")
    print("Welcome,", name + "!")
    return name


def read_char(prompt, first, last):
    x= input(prompt)
    while x < first or x > last:
         print("Not in range. Try Again!!!")
         x= input(prompt)
    return x



def check_letters(target, letters):

    print(len(random_word))
    count = 0
    for c in random_word:
       if "a" <= c <= "z":
            count += 1
    print(count)

    count2 = 0
    for i in range(len(random_word)):
        if "a" <= random_word[i] <= "z":
            count2 += 1
    print(count2)

    new_s = " "
    for c in random_word:
        if "a" <= c <= "z" or "A" <= c <= "Z":
            new_s += c
        else:
            new_s += "_"
    print(new_s)

预期产出:

Guess a word game.
What is your name? David
 Welcome David!
 7 letters in the word to guess.
 Maximum of 10 attempts.
 Word to guess: _______
 Letters already guessed:
 10 failed attempts left.
Guess a letter: e
Found 1 e.
Word to guess: ______e
Letters already guessed: e
10 failed attempts left. Guess a letter: a
# Didn't find any a
Word to guess: ______e
Letters already guessed: ae
9 failed attempts left.
Guess a letter: i
Didn't find any i
Word to guess: ______e
....
2 failed attempts left.
Guess a letter: l
Didn't find any l
Word to guess: a__e__i__
Letters already guessed: abcdefghijkl
1 failed attempts left.
Guess a letter: m
Didn't find any m
You lose! The word was: attention
Do you want to play again? n

实际输出:

Word:  _ _ _ _ _ _ _ _ _ _ _ _ 
Guess a word game.
What is your name? David
Welcome, David!

Please make a guess: a
guess is wrong! 9 more failed attempts allowed.
guess is wrong! 8 more failed attempts allowed.
guess is wrong! 7 more failed attempts allowed.
guess is wrong! 6 more failed attempts allowed.
guess is wrong! 5 more failed attempts allowed.
guess is wrong! 4 more failed attempts allowed.
guess is wrong! 3 more failed attempts allowed.
guess is wrong! 2 more failed attempts allowed.
guess is wrong! 1 more failed attempts allowed.
guess is wrong! 0 more failed attempts allowed.
guess is wrong! -1 more failed attempts allowed.
guess is wrong! -2 more failed attempts allowed.

Traceback (most recent call last):
  File "C:/Users/USER/PycharmProjects/Exercise 4/testing.py", line 106, in <module>
    print("Words: ", newBlanks)
NameError: name 'newBlanks' is not defined

0 个答案:

没有答案