Saya baru mengenal StackOverflow (posting pertama kali) dan baru dalam pengkodean dengan python. Saat ini terdaftar dalam kursus melalui Udacity. Saya mengalami waktu yang sangat sulit dengan proyek yang diberikan kepada kami untuk kursus ini dan memutuskan untuk datang ke sini untuk melihat apakah ada yang bisa membantu.

Proyek ini adalah membuat kuis dengan 4 kosong yang harus dijawab dengan benar oleh pemain. Kuis harus dicetak dengan jawaban yang benar, tetapi saya kesulitan mencetak ini dengan benar.

Kode saya di bawah ini. Akan sangat menghargai bantuan atau saran yang bisa saya dapatkan tentang ini.

Terima kasih!

    easy_quiz = "If you ever get stuck, check out the __1__ for common 
    problems students face when attempting this project. If you need 
    additional help, you can schedule a 1:1 appointment with one of our 
    __2__ to get you un-stuck. This project should be __3__. If at any time 
    it becomes not fun, take a step back, deep breath, and ask for __4__!. 
    \n\n"
    easy_answers = ["forums", "mentors", "fun", "help"]

    medium_quiz = "Game must have 3 or more levels and each level contains 4 or more __1__ to fill in. Immediately after running the program, user is prompted to select a difficulty level from easy / __2__ / hard. Once a level is selected, game displays a fill-in-the-blank and a prompt to fill in the first one. When player guesses __3__, new prompt shows with correct answer in the previous blank and a new prompt for the next blank. When player guesses __4__, they are prompted to try again. \n"
medium_answers = ["blanks", "medium", "correctly", "incorrectly"]

hard_quiz = "__1__ are used as __2__ to automate tasks which are likely to be repeated. Functions produce the appropriate output (typically with a __3__ statement) from the appropriate input (function parameters). Your code should take advantage of __4__ and variable names should reflect the values they store. \n"  
hard_answers = ["Functions", "tools", "return", "variables"]

blanks = ["__1__", "__2__", "__3__", "__4__"]

difficulty = raw_input("\nChoose your difficuty level = easy, medium, or hard? ")
print "" 
if difficulty == "easy":
    quiz = easy_quiz
    answers = easy_answers
    print "You chose easy!\n\nYou will have 5 guesses to fill in each blank. Good Luck!!\n \n" + easy_quiz

elif difficulty == "medium":
    quiz = medium_quiz
    answers = medium_answers
    print "You chose medium!\n\nYou will have 5 guesses to fill in each blank. Good Luck!!\n \n" + medium_quiz

elif difficulty == "hard":
    quiz = hard_quiz
    answers = hard_answers
    print "You chose hard!\n\nYou will have 5 guesses to fill in each blank. Good Luck!!\n \n" + hard_quiz

def word_in_pos(word, parts_of_speech):
    for pos in parts_of_speech:
        if pos in word:
            return pos
    return None

def play_game(quiz, parts_of_speech): 
    replaced = []
    i = 0
    quiz = quiz.split() 
    for word in quiz:
        replacement = word_in_pos(word, parts_of_speech)
        if replacement != None:
            user_input = raw_input("Type an answer for: " + replacement + " " )
            word = word.replace(replacement, user_input)
            replaced.append(word)
            guesses = 0
            while user_input != answers[i]:
                guesses = guesses + 1 
                print "Incorrect, try again \n" + " ".join(replaced)
                user_input = raw_input("Type an answer for: " + replacement + " ")
                if guesses == 4:
                    return "\nGame Over! Better luck next time. \n"
            print "Correct \n" + " ".join(replaced)
            i = i + 1 
            word = word.replace(replacement, user_input)
            replaced.append(word)
        else:
            replaced.append(word)
    replaced = " ".join(replaced)
    return replaced

print play_game(quiz, blanks) 
-4
fredcoremusic 11 November 2017, 04:51

1 menjawab

Jawaban Terbaik

Ini adalah versi yang berfungsi dari metode play_game() Anda:

def play_game(quiz, parts_of_speech):
    replaced = []
    i = 0
    quiz = quiz.split()
    for word in quiz:
        replacement = word_in_pos(word, parts_of_speech)
        if replacement is not None:
            user_input = raw_input("Type an answer for: " + replacement + " " )
            guesses = 0
            while user_input != answers[i]:
                guesses = guesses + 1
                if guesses == 5:
                    return "\nGame Over! Better luck next time. \n"
                print "Incorrect, try again \n" + " ".join(replaced) + " " + replacement
                user_input = raw_input("Type an answer for: " + replacement + " ")
            replaced.append(user_input)
            print "Correct \n" + " ".join(replaced)
            i = i + 1
        else:
            replaced.append(word)
    replaced = " ".join(replaced)
    return replaced

Perubahan utama adalah menunda pengubahan daftar replaced sampai jawaban yang benar telah diberikan. Itu menyederhanakan banyak kode, menghilangkan kebutuhan akan variabel word.

0
John Anderson 11 November 2017, 03:07