Skip to content

Commit

Permalink
empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Mar 11, 2017
1 parent 4613129 commit 20ba81b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion basics/larger-program.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,34 @@ and then testing and fixing. Here are my versions of them:
def ask_questions(answers):
correct = []
wrong = []

for question, answer in answers.items():
if input(question + ' = ').strip() == answer:
print("Correct!")
correct.append(question)
else:
print("Wrong! The correct answer is %s." % answer)
wrong.append(question)

return (correct, wrong)


def stats(correct, wrong, answers):
print("\n**** STATS ****\n")
print("You answered", len(correct), "questions correctly and",
len(wrong), "questions wrong.")

if wrong:
print("These would have been the correct answers:")
for question in wrong:
print(' ', question, '=', answers[question])
```

Let's try them out.
Note that these functions have some empty lines in them and there are
two empty lines between the functions. This makes the code a bit longer,
but it's a lot easier to read this way.

Let's try out the functions.

```python
>>> answers = read_questions('questions.txt')
Expand Down Expand Up @@ -165,27 +173,33 @@ def read_questions(filename):
answers[question.strip()] = answer.strip()
return answers


def ask_questions(answers):
correct = []
wrong = []

for question, answer in answers.items():
if input('%s = ' % question).strip() == answer:
print("Correct!")
correct.append(question)
else:
print("Wrong! The correct answer is %s." % answer)
wrong.append(question)

return (correct, wrong)


def stats(correct, wrong, answers):
print("\n**** STATS ****\n")
print("You answered", len(correct), "questions correctly and",
len(wrong), "questions wrong.")

if wrong:
print("These would have been the correct answers:")
for question in wrong:
print(' ', question, '=', answers[question])


def main():
filename = input("Name of the question file: ")
answers = read_questions(filename)
Expand Down

0 comments on commit 20ba81b

Please sign in to comment.