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
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.
No comments:
Post a Comment