Monday, 9 September 2013

Question 2. 'Access Denied'

Access Denied:

a)


b) This problem is asking me to write a program which only grants access if the password is correct. It is asking that if anything else is entered, for it to print access denied.


c) For this question, you need to break it down into sections. 
1. You need to tell the computer what 'password' equals.
2. You need to make an 'if' statement equalling 'chEEzburg3rz' 
3. Then you need to tell it to print 'Access granted' 
4. You then need to make an 'else' statement 
5. Finally you need to tell it to print 'Access denied' in order to make sure that if anything else apart from the correct password is entered, you do not receive access. 

d) You need to remember a few things that were taught previously.

- Input: asking the user for input: Week 1 Module 1

- Print: Week 1 Module 1

- 'if' statements: Week 2 Module 1

- 'else' statements: Week 2 Module 1

e) My attempts:









So in this attempt, I reached a syntax error. It was pointing to my equals sign. So I figured that I needed to add another equals sign in, as I had previously read about.

























I tried the exact same code, just using a double equals sign. I didn't get a syntax error so I tried it out and it worked with the correct password.



So I then tried with an incorrect password and it also worked. So I decided to mark my code.


My code was correct! 


f) My main source of error was just realising that I needed to use a double equal sign.
Also I had to figure out how to successfully use 'if' and 'else' statements.


g)

h) This solution provided by grok is almost the exact same as my solution. The only difference is that I used another set of brackets to enclose everything on the first line after the equals sign. That set of brackets proved to be unnecessary. Both my solution, and the sample solution work perfectly fine. The sample solution however is slightly more elegant as it is slightly less busy.





1 comment: