[Tutor] stuck on a new program

Hugo Arts hugo.yoshi at gmail.com
Fri Jan 4 23:43:24 CET 2013


On Fri, Jan 4, 2013 at 11:15 PM, Ghadir Ghasemi <
ghasemmg01 at leedslearning.net> wrote:

> Hi guys I recently created a binary to denary and denary to binary
> convertor program. It is pretty much finished but I can't get it to work.
> Can you spot the fault in it please and tell me how it coul be fixed?
>
> print("==================")
> print("1 = binary to denary")
> print("2 = denary to binary")
>
>
> return = input("enter an option)
> menu_option = get_option()
>
>
> while True:
>         menu_option = get_option()
>
> if menu_option == '1':
>         a = int(input('please enter an 8 bit binary number: '),2);
> print("the result is", a)
>
> if menu_option == '2':
>          b= bin(int(input('please enter a denary number: ')));print("the
> result is", b)
>
>
There are several errors here, in fact. The first error you made is not
including the output of your program when you attempted to run it. There
should have been an error message of some form, with an indicator of where
the error happened. For example, if I run the program on the command line,
I get this:

  File "test.py", line 6
    return = input("enter an option)
           ^
SyntaxError: invalid syntax

always, always always include any error messages you get when you send an
e-mail to this list or other programming lists. It may not be absolutely
necessary for small programs like this, but it still saves everyone a bunch
of time. And if there are no error messages, tell us what you expected to
happen, and what happened instead. "I can't get it to work" is the least
useful problem description in the history of mankind, giving absolutely
zero helpful information that can be used to solve the problem. Help us
help you, please.

The error indicated by python has to do with keywords: python has several
"keywords" which are used to indicate certain structures in the language.
Examples are "while", "if", and "return". These keywords have a special
meaning in the language, and can thus not be used as variable names. You
tried to use return as a variable name, which is why the syntax error shows
up on that line.

There are other mistakes in this program: the get_option() function, for
example, which you have used in this program, is not defined anywhere. You
used it but never told python what it means. Thus if I corrected the return
error and tried to run the program again, python would complain about the
"get_option" name not existing (or did you not include the entire program
in this e-mail? That would be another mistake. If you did not include an
error, and we cannot run the program to see what it is, helping you becomes
very difficult indeed).

So assuming we fixed that missing function, the program now runs without
error messages. However, there are four lines at the bottom that will never
be reached by the computer. Why not? Look at these lines above it:

while True:
    menu_option = get_option()

what does it do? Well, "while True" describes a loop that runs forever. The
only line inside the loop is "menu_option = get_option(). So, the program
will keep running that line for eternity, and never reach the two "if"
statements below. Can you fix it now?

HTH,
Hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130104/67071b5e/attachment.html>


More information about the Tutor mailing list