Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Problems with coding a script in python JoeLamond Programmer named Tim Posts: 7 Threads: 4 Joined: Nov 2016 Reputation: 0 Likes received: 1 #1 Nov-09-2016, 09:17 PM (This post was last modified: Nov-09-2016, 09:33 PM by snippsat. Edited 1 time in total. Edit Reason: Added code tag ) Hello, I've learnt the absolute basics of python and I am writing a script for my computing homework. The idea is that it asks you to think of a number between 1 and 10, before asking you to double it, add 10, halve it and take away the number you started with. Because it goes through the sequence n --> 2n --> 2n + 10 --> n + 5 --> 5 the computer can tell you the number you are thinking of, that is 5. Here is what I've tried so far, could anyone find where I went wrong and why it isn't working? ```number=(input("Think of a number between 1 and 10. Reply with an Ok when you have it.")) if number==Ok:           nextstep==(input("Double it, then add 10 "))           else print("Must try harder ...")           if nextstep==Ok:               stepafterthat==(input("Halve it"))               else print("Must try harder")               if stepafterthat==Ok:                   laststep==(input("Take away the number you started with."))                   else print("Must try harder ...")                   if laststep==Ok:                       print("You are thinking of 5!")                       else print("Must try harder ...")``` Larz60+ aetate et sapientia Posts: 7,664 Threads: 289 Joined: Sep 2016 Reputation: 286 Likes received: 1059 #2 Nov-09-2016, 09:23 PM Please use code tags, otherwise your indentation will be lost snippsat Posts: 3,766 Threads: 82 Joined: Sep 2016 Reputation: 255 Likes received: 1177 #3 Nov-09-2016, 10:30 PM (Nov-09-2016, 09:17 PM)JoeLamond Wrote: I've learnt the absolute basics of python and I am writing a script for my computing homework. You should look trough the basic stuff one more time. What you have written now is a mess of very basic errors. All "else" block has syntax error. Try run 1 of your line in interactive shell. ```>>> nextstep==(input("Double it, then add 10 ")) Traceback (most recent call last):  File "", line 301, in runcode  File "", line 1, in NameError: name 'nextstep' is not defined >>> # Should be = >>> nextstep  = input("Double it, then add 10 ") >>>``` sparkz_alot Da Bishop Posts: 1,298 Threads: 32 Joined: Sep 2016 Reputation: 30 Likes received: 267 #4 Nov-10-2016, 12:03 AM You are storing a 'string' value in number. There for the comparison has to be with an equivalent  'string' value in order to be True. So line 2 for example should be written as ```if number == "Ok":    # note the quotes ```Also, indentation is very important in Python. In your case, the "else" statements should line up with the associated "if" statement. YES ```if condition:     do_something else:     do_something_else ```NO ```if condition:     do_something     else:         do_something_else ``` If it ain't broke, I just haven't gotten to it yet. OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch" Python 3.6.5, IDE: PyCharm 2018 Community Edition JoeLamond Programmer named Tim Posts: 7 Threads: 4 Joined: Nov 2016 Reputation: 0 Likes received: 1 #5 Nov-10-2016, 08:32 PM Thank you very much for the help. As I said before, I only learn the absolute basics of python and was trying to do something a bit more ambitious. Hopefully, I wil be able to write the code correctly next time. « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)