a)
b) Overview of the problem:
In this problem, you have to create a program that can distinguish questions from statements. It needs to go into a cycle to keep asking for another question if the input is a question, but if the input is a statement, it prints "Statement!" and then ends the game. To do this problem, you will need to be able to use the usual print and input statements, as well as while and if statements.
c) Steps to solve the problem:
1. To start, you need to define what the line is, and then tell the program to ask for a line.
2. Then make a while statement that differentiates questions from statements, and continues the cycle to ask for an input if the statement is a question.
3. Make an else statement that prints "Statement!" if the input is not a question.
d) Notes used for the problem:
1. print= week 1, part 1
2. input= week 1, part 1
3. .endswith= week 2, part 2
4. if/else= week 2, part 1
5. while= week 4, part 1
e) f) Attempts and testing
Attempt 1:
In this attempt, I did what I did in the last problem, and firstly defined what the line was. Then I did a giant while statement for if the input was actually something, rather than nothing. I then had an if statement for if the statement ended if a question mark (therefore it would be a question), and then would start the cycle of the game again. I then had a separate else statement out of the while statement that said that anything else would be a statement, and therefore the computer should print "statement!"I did it out of the while statement because I did not want "Statement!" to go on and on forever as part of the while statement.
However, when I tried this code, nothing happened when the line didn't end in a question mark, so I tried again.
Attempt 2:
I thought that perhaps nothing was happening because I had written my else statement wrong, so I tried all sorts of other elif, else and if else statements, but of course, this only ran me into lots more syntax errors.
Attempt 3:
In this trial, I moved the else statement into the while statement, because I realised that nothing was happening because my else statement was referring to if the statement was actually nothing, and there was nothing under my giant while statement for if the statement is something, but doesn't end in a question mark. So I tried this, and, of course, "Statement!" was printed on and on because it was under a while statement. I was very confused as to what to do at this point.
Attempt 4:
After a bit of thinking, I realised that I was completely wrong because the question didn't ask for anything to happen if the statement was nothing, like Slow down! had, so I scrapped my giant while the statement is actually something stuff, and I decided to simply say while the statement is a question, that cycles re-occurs, or else, it just prints "Statement!" and everything ends. I tried this, and it seemed to work!
Attempt 5:
I tested and marked this idea, and (to my surprise) it worked!! Yay!
g) The suggested solution
h) Comparison
The two solutions are exactly the same, apart from that I wrote a separate else statement for statements, and the suggested solution simply wrote print ("Statement!") on its own line out of the while statement. Because of this, I think that the suggested solution is a bit neater and more concise than my one, but apart from one word, they are exactly the same.
No comments:
Post a Comment