Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner needs help
#1
Hi everyone,
This is my first post, I just started to study coding and this is my first code.

This is a quiz contains a text with five spaces, the user has 5 tries for each space to enter the correct answer and if the user enters the correct answer the program will replace the space with the correct word. Every thing works well unless if the user enters a wrong answer 4 times and on the 5th try a right answer, the program stopped and exit.
Please help me to solve this problem.

easy = "\nThe United States of America, commonly known as the United States (U.S.) or America, \nis a federal republic composed of -----1----- states, a federal district, five major self-governing \nterritories, and various possessions. Forty-eight of the fifty states and the ederal\n district are contiguous and located in North America between -----2----- and \n-----3-----. The state of -----4----- is in the northwest corner of North America,\n bordered by Canada to the east and across the Bering Strait from Russia to the west. \nThe state of -----5----- is an archipelago in the mid-Pacific Ocean. The U.S. territories \nare scattered about the Pacific Ocean and the Caribbean Sea, stretching across nine-time zones. \nThe extremely diverse geography, climate and wildlife of the United States make it one of \nthe world's 17 mega diverse countries.\n"

easy_answer = ["50", "Canada", "Mexico", "Alaska", "Hawaii"]

mediam = "\nAncient Egypt was a civilization of ancient Northeastern -----1-----, concentrated \nalong the lower reaches of the -----2----- River in the place that is now the country \nEgypt. It is one of six historic civilizations to arise independently. Egyptian civilization \nfollowed prehistoric Egypt and coalesced around -----3----- BC (according to conventional \nEgyptian chronology) with the political unification of Upper and Lower Egypt under the first \npharaoh, -----4----- (commonly referred to as Menes). The history of ancient Egypt \noccurred as a series of stable kingdoms, separated by periods of relative instability known as \nIntermediate Periods: The Old Kingdom of the Early -----5----- Age, the Middle Kingdom \nof the Middle Bronze Age and the New Kingdom of the Late Bronze Age.\n"

mediam_answer = ["Africa", "Nile", "3150", "Narmer", "Bronze"]

hard = "\nThe -----1----- Empire comprised the dominions, colonies, protectorates, mandates \nand other territories ruled or administered by the -----2----- Kingdom and its \npredecessor states. It originated with the overseas possessions and trading posts established \nby -----3----- between the late -----4----- and early 18th centuries. At its \nheight, it was the largest empire in history and, for over a century, was the foremost global \npower. By 1913, the British Empire held sway over -----5----- million people, \n23% of the world population at the time, and by 1920, it covered 35,500,000 km2 \n(13,700,000 sq mi), 24% of the Earth's total land area. As a result, its political, legal, linguistic and \ncultural legacy is widespread. At the peak of its power, the phrase (the empire on which the \nsun never sets) was often used to describe the British Empire, because its expanse \naround the globe meant that the sun was always shining on at least one of its territories.\n"

hard_answer = ["British", "United", "England", "16th", "412"]

blanks = ["-----1-----", "-----2-----", "-----3-----", "-----4-----", "-----5-----",]


text = ""
correct_answer = ""
correct = ""
user_input = ""
answer_position = 1



user_name = raw_input("What is your Name?    ")
welcome = "Welcome " + user_name + "!!"
print welcome

difficulty = raw_input("\n Please Choose the difficulty. Easy / Medium / Hard    ")
choices = ['Easy', 'Medium', 'Hard']

while difficulty not in choices:
    print "Wrong choice!!!"
    difficulty = raw_input("Please Choose the difficulty. Easy / Medium / Hard    ")

def current_text(difficulty):
    if difficulty == "Easy":
        text = easy
    elif difficulty == "Medium":
        text = mediam
    else:
        text = hard
    return text

current_text = current_text(difficulty)

def current_correct_answer(difficulty):
    if difficulty == "Easy":
        correct_answer = easy_answer
    elif difficulty == "Medium":
        correct_answer = mediam_answer
    else:
        correct_answer = hard_answer
    return correct_answer

current_correct_answer = current_correct_answer(difficulty)

print "\nComplete the following sentences.\nYou Have 5 chance to give the right answer!\n"
print "Text: ", current_text

def answers(blanks, current_text):

    current_position = 0
    for position in blanks:
        response = raw_input("What should be substituted in " + blanks[current_position] + "?    ")
        if response == current_correct_answer[current_position]:
            current_text = current_text.replace(position, response)
            current_position += 1
            print "CORRECT !!! \n  \n", current_text
        else:
            counter = 4
            while counter >0:
                
                
                if response != current_correct_answer[current_position]:
                    
                    print "That isn't the correct answer! \nLet's try again; you have " +  str(counter) +" trys left!"
                    counter -= 1
                    response = raw_input("What should be substituted in " + blanks[current_position] + "?    ")
                    
                    if counter == 0:
                        print "GEME OVER"
                        return ""

                else:
                    current_text = current_text.replace(blanks[current_position], response)
                    current_position += 1
                    print "CORRECT !!! \n  \n", current_text
                    counter = 0
    print "congratulations"
print answers(blanks, current_text)

print raw_input("Click enter to exit")
Reply
#2
It's due to a logical error.
What you are doing is initializing counter with a value of 4 at line 70 when a wrong answer is given, which is alright as the one chance is missed. But immediately after that in while loop you are checking the previous value of response at line 74 and decrementing the counter. This makes the value of counter 3 even before the player could answer again.

You can solve this by initializing counter with 5 at line 70.
code: line 70-78.
counter = 5
while counter >0:                    
    if response != current_correct_answer[current_position]:                   
        print "That isn't the correct answer! \nLet's try again; you have " +  str(counter-1) +" trys left!"
        counter -= 1
        response = raw_input("What should be substituted in " + blanks[current_position] + "?    ")
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020