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.
Thursday, 5 September 2013
Wednesday, 4 September 2013
Problem 1: Slow down!
a)
b) Overview of the problem:
In Slow down, you have to create a program that reads a speed limit, and then when you type in your speed, it tells you if you are OK, or if you need to slow down, according to the speed limit (this was pretty easy). However, the computer needs to continually ask for a speed after each time it responds to your previous speed and it should only stop when you hit enter without typing a speed into its question. In this problem I will have to use the usual if, else, print and integer/string functions, however I will have to try and incorporate in the while function to make the question repeat, (which I don't really understand how to do) and also use less that/equal to/more than statements.
c) Steps to solve the problem:
1. To start, you need to ask the user for a speed limit. (which needs to be an integer)
2. Then, ask the user for a speed (NOT an integer).
3. Use a while statement for when the input is something, not nothing (so that if the user hits enter, none of the
following will occur).
4. use an if statement for one possible relationship between the speed (needs to be an integer) and the speed limit.
5. print the appropriate statement according to the if statement.
6. re- define the speed so that the process can start again.
7. use an else statement to refer to the other possible relationship between the speed and the speed limit.
8. print the appropriate statement according to the else statement.
9. re-define the speed so that the process can start again.
d) Notes used for problem:
print - week 1, part 1
input - week 1, part 1
int/str - week 1, part 2
if/else - week 2, part 1
comparing (!=, <=, <, etc.) - week 2, part 1
while - week 4, part 1
e), f) Attempts and testing:
Attempt 1:

In this attempt I defined what the speed limit and speeds were, and also told the grok thing that while the speed was higher than the speed limit, it would tell the user to slow down, and when it was equal to or lower than the speed limit, it would tell the user that their speed was OK, and then continue on to ask them once again what the speed was. I then realised that I didn't distinguish and integers from strings, so I tried again, adding int where needed.
Attempt 2:

In this attempt, I added int every time I defined something, but not when I re-defined something, so when I tried to run the program, it got confused whether speed was an integer or normal. So I tried again.
Attempt 3:

This time, as well as putting int before inputs when I defined speed and speed_limit, I also added int before the inputs when I re-defined them. Thinking that I was right, I marked my work, and sure enough, I was wrong. At first I didn't understand, because all of the technical stuff was right, so I re-read the question and realised that I hadn't put into account that when you click 'enter' without entering anything as an input, the cycle is supposed to stop, and with my current program, it had no instructions for if the speed is actually nothing.
Attempt 4:
With this attempt, I completely re-structured my while and if statements. I decided (with some help from the people around me) to create one giant while statement for if the speed is actually something. That way, if the speed is nothing and the user just clicks 'enter', none of the cycle stuff applies it just ends. Then I just did all the same stuff that I had before, except instead of being while statements, they were if statements underneath the giant while statement.
Attempt 5:

I was really proud of what I had just done, but when I clicked 'run', I had some more issues with strings and integers. I didn't really know what to do, but I decided that to keep it even, I would try and get rid of the int on the line that was highlighted and also on line 6, which both are the ones that re-define the speed, and see what happens.
Attempt 6:

This worked well, however then I ran into problems with line 7. I am still not sure what happened with this line, and neither could the teachers work out what happened, but I decided (impatiently) to mark the problem anyway...
Attempt 7:

... and sure enough something was wrong with line 7, which no-one could figure out what it was. Then it struck me that maybe I could avoid problems in a second if statement by simply writing else. So I tried this out!
Attempt 8:

And it worked!! Yay!!
g) Grok's answer:
h) comparison:
I find it interesting how in the proper solution, the giant while statement, unlike mine, which was while the answer is actually something, seems to be while the answer is an integer. Because of this, the initial definition of the input of "Current speed: " is called length and later the speed is defined as the integer version of the length. I am not really sure which version is better- I think that both methods are pretty clean and elegant, however I am finding it easier to understand the while statement in my version than the while statement in this version.
b) Overview of the problem:
In Slow down, you have to create a program that reads a speed limit, and then when you type in your speed, it tells you if you are OK, or if you need to slow down, according to the speed limit (this was pretty easy). However, the computer needs to continually ask for a speed after each time it responds to your previous speed and it should only stop when you hit enter without typing a speed into its question. In this problem I will have to use the usual if, else, print and integer/string functions, however I will have to try and incorporate in the while function to make the question repeat, (which I don't really understand how to do) and also use less that/equal to/more than statements.
Subscribe to:
Posts (Atom)