-2

I'm new to Python and I was trying to create a quiz but I don't know how to make the procedure.


The quiz is a phrase with gaps and you need to put the right word in the gaps. If the word is correct, print the phrase with the correct word in the gap. If wrong, print try again.
I'm trying something like this:

phrase = 'HTML is HyperText __1__ __2__.'
answers = ['Markup','Language']

def quiz(p,a):
    """ I can do it manually with if statements, 
        but this is not a good idea.""""

Can someone help me?

niraj
  • 17,498
  • 4
  • 33
  • 48
  • Not sure it's possible, though you can try to shorten it and use `if` statements – Bryan Zeng Jun 16 '18 at 00:45
  • You'll probably want to store the blanks and full text parts in a list instead of as a whole string. That will likely be much easier to deal with. This question is far too broad though. What have you tried? What specifically do you need help with? – Carcigenicate Jun 16 '18 at 00:46
  • 1
    This question is too broad in its current state. What exactly do you need help with? This may be helpful: https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – PM 2Ring Jun 16 '18 at 00:47
  • @Carcigenicate He is trying to make a "Fill in the blanks" quiz without using the `if` statement to check the answer – Bryan Zeng Jun 16 '18 at 00:53
  • @BryanZeng Ya, I got what the assignment is. And he never said that he didn't want to use if's. He said that he doesn't want to do manual checking with ifs. And adding a restriction to a request doesn't make it more specific of a question. – Carcigenicate Jun 16 '18 at 00:55
  • @Carcigenicate I had tried something like this: If input __1__ == 'Markup': print (phrase.replace('__1__','answer[0])). Did make any sense? I know that can work but I want something automated. Because this quiz is short and if statement is easy but can you imagine a big quiz with a lot of gaps? – Rafael Sousa Jun 16 '18 at 01:05
  • Is it okay to have the user answer the blanks in order? –  Jun 16 '18 at 01:23
  • @CMalasadas Yes, the idea is in order. – Rafael Sousa Jun 16 '18 at 21:41

1 Answers1

0

This method iterates through the answers in order (per your comment).

To iterate through each of the answers, a for loop is used. Inside the for loop is a while loop which will continually ask the user for input until they get the correct answer, when it then updates the phrase and breaks out of the while loop.

blank = "_____"
phrase = ["HTML is HyperText ", blank, " ", blank]
answers = ['Markup','Language'] 
def quiz(phrase, answers):
    for answer in answers:
        while True:
            # "".join() converts the list into a string
            inp = raw_input("".join(phrase)+" ") #replace with input() in python3 
            if inp == answer:
                # Updates the first available blank with the answer
                phrase[phrase.index(blank)] = answer
                break

Consider adding a .lower() function to make the input case-insensitive. Call this on the raw_input and answer.

if inp.lower() == answer.lower():