使用Python进行拼写游戏

时间:2019-02-18 06:44:29

标签: python python-3.x

我提供了一个单词列表,这些单词将被随机选择用于游戏。它的作用是提示玩家输入所选单词的字母。如果在所选单词中找到了用户提供的字母,则会询问他们是否要拼写整个单词。如果他们回答“是”,那么他们将能够这样做。否则,他们将不得不尝试输入另一个字母。如何将每个用户输入的输出/结果打印为(例如:结果:a ------),如果用户最终对拼写说“是”整个单词,应该如何提示他们输入单词? 例如:您现在要拼写单词吗? (y / n):y 拼写完整的单词:
你是对的! 正确的词是蛋白 拼另一个字吗? (y / n):y

import random

wordList = ('apple', 'albumen', 'toothpaste', 'enthusiastic')

word = random.choice(wordList)

letter_guess = ""

word_guess = ""

store_letter = ""

count = 1

limit = 5

countLetters = len(word)

choice1 = ("Do you want to spell the word now? (y/n):")

choice2 = ("Spell another word? (y/n):")

choiceY = ("y")

choiceN = ("n")



startGame = print("The word ________ has {} letters. Spell it in 5 tries.".format(countLetters))

while count < limit:
    letter_guess = input("Try {} - Current: ________. Your guess? ".format(count))

    if letter_guess in word:
        print ("yes")
        print (input(choice1))

    else :
        print("no")
        count += 1

    if choice1 == choiceY:
        print (input("Spell the complete word: "))

    else:
        print (letter_guess)
        store_letter += letter_guess
        count += 1


while count == limit:
    spellFinal = input("Spell the complete word: ")

    if spellFinal in word:
        print ("You are correct!")
        print ("Spell another word? (y/n):")

    if choice == "y":
        print (startGame)

    else:
        print('Remaining Words are: ', store_letter)

    if spellFinal not in word:
        print ("You are incorrect")
        print ("The correct word is {}.".format(correct))

Output example

Snippet of my code

1 个答案:

答案 0 :(得分:0)

您不是从print (input(choice1))行中收集信息。您应该将其分配给变量,以便以后与您的choiceY进行比较(或者您可以简单地将新变量与“ y”进行比较,而无需定义choiceY变量,具体取决于您)。

例如:

user_choice = ""
complete_word = ""

if letter_guess in word:
    print ("yes\n")
    user_choice = input(choice1)
    if user_choice.lower() is "y":
        complete_word = input("Spell the complete word: ")
        #here you compare complete_word with your word variable
    else:
        print (letter_guess)
        store_letter += letter_guess
        count += 1
else:
    print("no\n")
    count +=1

这同样适用于您的代码中其他带有print(input(...))的部分。 嵌套的ifs可能会变得凌乱。也许您可以考虑具有验证功能,然后调用它。

希望这会有所帮助!