简体   繁体   中英

Spelling Game using Python

I have provided a list of words that will randomly be picked for the game. What it does is to prompt the player to enter an alphabet of the selected word. If the letter provided by the users is found in the selected word, they if be asked if they want to attempt spelling the whole word. if they say yes, then they will be able to do so. Else, they will have to try entering another alphabet.how do I get the output/ results of each user input to be printed as (eg: Outcome: a------) And if the users ended up saying yes to spelling the whole word, how should they be prompt to enter the word? eg: Do you want spell the word now? (y/n): y Spell the complete word:
You are correct! The correct word is albumen Spell another word? (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))

输出示例

我的代码段

You are not collecting the information from your print (input(choice1)) line. You should be assigning it to a variable to be able to compare it later with your choiceY (or you could simply compare the new variable to "y" without having a choiceY variable defined, up to you).

For example:

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

The same applies to the other sections of you code that have the print(input(...)) . Nested ifs can get messy. Perhaps you can consider having a verification function and call that instead.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM