0

I am using the newest python software and I am really stumped on printing a certain line in the code.

name = input("Before we begin, what is your name? ")
print("Cool, nice to meet you "+name+"!")

score = 0 
score = int(score)
count = 0

answer1 = "dogs" 
answer01 = "dog"
question1 = ""
while question1 != answer1 and count < 2:
    count = count + 1
    question1 = input("What animal barks? ") #Asks the user a question 

    if question1.lower()== answer1:
          print("Correct! Off to a great start, the right answer is dogs!") 
          score = score + 1
          break

    elif question1.lower()== answer01:
          print("Correct! Off to a great start, the right answer is dogs!") 
          score = score + 1
          break

    elif count == 1:
          print("That is wrong",name,"but you can try again. Hint: They like bones. ")  

if count == 2 and question1 != answer1:
    print("Incorrect again, sorry! The right answer was dogs.") 

Please do correct me if that looks wrong, I am new to python. Anyway all I want to do is print the question again without repeating the introduction (start). My actual code is much longer and is an entire quiz and I need to repeat certain sections of code from different parts of the code. Please help! Plus I am sorry if the code didn't print properly when I pasted it into the my post.

  • It looks like you understand how to "skip" portions of your code, one way is what you do here with `if .. elif .. else` statements. The typical ways of jumping *around* between areas of your code (including up) in Python are loops (e.g. `while`, `for .. in`) and functions. You might consider reading up on those aspects of the language. [This tutorial](http://introtopython.org/while_input.html) seems especially applicable, as it deals with loops *and* user input. – jedwards Jul 31 '16 at 11:56

1 Answers1

1

So, the problem is that your questions are hardcoded, so they repeat etc. Instead, you could provide a data structure, storing all your questions, wrong ang right answers, which you would pass to some parsing logic. For example:

questions = ['What animal barks?', ...]
answers = [('dogs', 'dog'), ...]
hints = ['They like bones', ...]

def dialog(qst, ans, hnt):
    score = 0
    for question, answers, hint in zip(qst, ans, hnt):
        incorrects = 0
        while True:
            usr_ans = input(question)
            iscorrect = any(usr_ans.strip().casefold() == answ.strip().casefold() for answ in answers)
            if incorrects < 5 or not iscorrect:
                print('That is wrong, {},  but you can try again. Hint: {}'.format(name, hint))
                incorrects += 1
            elif incorrects >= 5:
                print('Incorrect again, sorry! The right answer was {}.'.format(' or '.join(answers))
                break
            else:
                print('Correct! Off to a great start, the right answer is {}!'.format(usr_ans))
                score += 1
                break
    return score

It may look a bit complicated as you're starting to learn. I recommend not to code complete things, until the point in time, where you'd already known the basics. For a great start, try official Python Tutorial, and I'd recommend Lutz's Learning Python

thodnev
  • 1,564
  • 16
  • 20