LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Unable to find error in my code. (https://www.linuxquestions.org/questions/programming-9/unable-to-find-error-in-my-code-4175735193/)

ajiten 03-23-2024 01:14 AM

Unable to find error in my code.
 
Though simple, but was interested in the site that states to build up a project in steps.
The very first project is stuck at: https://www.freecodecamp.org/learn/s...cipher/step-17.
The original answer, as well as the modified one, don't help; as shown in the two attachments attached herewith.


The further steps are blocked by the unknown error, so needed help.

--------

Edit : Sorry, am unable to add images (screenshots), and this is continuing.

--------

Edit 2: Same error of 'Security token missing', on attempting from smartphone, too.

Turbocapitalist 03-23-2024 01:34 AM

Please post the code here between [code] [/code] tags. It is hard to guess what the problem might be without actually seeing the code.

ajiten 03-23-2024 01:43 AM

Quote:

Originally Posted by Turbocapitalist (Post 6491358)
Please post the code here between [code] [/code] tags. It is hard to guess what the problem might be without actually seeing the code.

So, is my link not working on your (i.e., anybody else's) browser?

grail 03-23-2024 02:27 AM

The code and the suggested change works just fine for me :)

NevemTeve 03-23-2024 02:40 AM

Please quote (as text) all of these:
- what do you wish to accomplish
- your program (complete and minimal)
- the error message or symptom you got

Note: attaching screenshot images is very insulting, don't do that

___ 03-23-2024 02:53 AM

Is your line 4: index = alphabet.find(text[0].lower())
Note the ())
It prints 7

ajiten 03-23-2024 04:05 AM

Quote:

Originally Posted by ___ (Post 6491364)
Is your line 4: index = alphabet.find(text[0].lower())
Note the ())
It prints 7

But, the error is still there; and I assume that the same error is visible to you too.

Edit: The output '7' is not important, but the moving to the next step is, which it is not, due to the error, as 'also' shown in the next post by me.

ajiten 03-23-2024 04:13 AM

Quote:

Originally Posted by NevemTeve (Post 6491363)
Please quote (as text) all of these:
- what do you wish to accomplish
- your program (complete and minimal)
- the error message or symptom you got

Note: attaching screenshot images is very insulting, don't do that

1. - what do you wish to accomplish

I intend to complete a particular step of the project, that is building up in an incremental manner the very first project. And am stuck at the 17th step.

The code is:

Code:

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0])
print(index)
print(alphabet.find(text[0].lower()))
#text1 = text[0].lower()
#print(alphabet.find(text1))
# The last two lines, even when uncommented bring no change to the status quo.

2. - your program (complete and minimal)

It is given as the first project titled: Learning string manipulation by building a cipher; at : https://www.freecodecamp.org/learn/s...g-with-python/

The task as given for the step #17 is:
Code:

Step 17
Remove the last print() call. Then, instead of text[0], pass text[0].lower() to .find() and see the output.


3. - the error message or symptom you got

The error it gives is:
Code:

Sorry, your code does not pass. You're getting there.

You should update your alphabet.find(text[0]) call to use text[0].lower() as the argument. Pay attention to place the method call at the beginning of the line.


P.S.: For the last line, hope with time would understand it.

Edit: Please see my post at: https://forum.freecodecamp.org/t/lea...step-17/681588

NevemTeve 03-23-2024 07:52 AM

Your program prints -1 and then 7. Is that what you wanted, or something else?
Code:

$ python3 ajiten17.py
-1
7

Guess you forgot to comment out two lines from the earlier step:
Code:

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
# index = alphabet.find(text[0])
# print(index)
print(alphabet.find(text[0].lower()))


ajiten 03-23-2024 09:35 AM

Quote:

Originally Posted by NevemTeve (Post 6491397)
Your program prints -1 and then 7. Is that what you wanted, or something else?
Code:

$ python3 ajiten17.py
-1
7

Guess you forgot to comment out two lines from the earlier step:
Code:

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
# index = alphabet.find(text[0])
# print(index)
print(alphabet.find(text[0].lower()))


Thanks, but the 'index' variable need be retained, as it was there in some earlier step; and might be the problem wants to reuse it in some further step(S). Else, it gives error on the deletion of that variable too.
Code:

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0].lower())
print(index)


grail 03-23-2024 10:07 AM

So the issue is you have not followed the instructions correctly.

Original code:
Code:

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0])
print(index)
print(text.lower())

Which returns the following:
Code:

>>> -1
>>> hello world

-1 is returned as text[0] is "H" which is not in the varaible alphabet and is assigned to variable index
hello world is returned in final print which is displaying value of "text" variable in lower case

What you were requested to do:
Quote:

Step 17
Remove the last print() call. Then, instead of text[0], pass text[0].lower() to .find() and see the output.
So remove / comment out last print()
Code:

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0])
print(index)
#print(text.lower())

Second change is to locate the variable "text[0]" (note it does not say "text") and replace with text[0].lower() in .find()
Code:

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0].lower())
print(index)
#print(text.lower())

With these 2 changes made the output is now 7 as text[0].lower() is a lower case "h" which is at position 7 in the alphabet variable

Hope that is clearer

astrogeek 03-23-2024 07:15 PM

Quote:

Originally Posted by ajiten (Post 6491355)
Though simple, but was interested in the site that states to build up a project in steps.
The very first project is stuck at: https://www.freecodecamp.org/learn/s...cipher/step-17.
The original answer, as well as the modified one, don't help; as shown in the two attachments attached herewith.


The further steps are blocked by the unknown error, so needed help.

--------

Edit : Sorry, am unable to add images (screenshots), and this is continuing.

--------

Edit 2: Same error of 'Security token missing', on attempting from smartphone, too.

As you have been asked several times before, please post your code examples inline here at LQ and refrain from posting off-site links and images as your primary example code.

* Posting your code inline here prevents loss of context over time due to disappearing images and linked resources, thus preserving the full context of the question and its solution in one place. This preserves the value of the information found here at LQ and respects the efforts of others who take time to provide help, both for yourself and for all future visitors.

* Failure to post your code examples as text, inline and properly formatted using [CODE]...[/CODE] tags may be most convenient for you, but is disrespectful of the time taken by others to consider your question and provide replies. It requires extra effort on their part just to understand the question, then to either follow links off-site or type the examples themselves in order to reproduce the error conditions and offer solutions.

Please take this as a final request to amend this behavior, and review the Site FAQ for guidance in asking well formed questions. Especially visit the link from that page, How to Ask Questions the Smart Way for discussion of things to consider when asking others for help.

From that page:

Quote:

If you are unwilling or unable to ask questions in a manner that allows us to help you, it's unlikely our community will be able to provide you a solution. Unfortunately, serial offenders who show wanton disregard for this request after multiple pointers may be asked to seek help elsewhere. We truly hope that isn't necessary, and assure you Linux and Open Source are extremely rewarding and well worth the learning curve in the long run.

___ 03-24-2024 02:24 AM

Upon careful (&inferential) re-reading of #10, I think you neglected to say that you solved it, with the code you posted in #10. (In that case, mark Thread as Solv'ed with Thread Tools pull-down above #1)
IF not, here's my 'stern' reply (excuse the 'stern'):

You are, simply&clearly, **NOT** following the instructions!
Outputting a -1 *VIOLATES* their instructions & you FAIL.
Output *ONLY* a 7. Period. NOthing else.
Do it *THEIR* way (their site is probably NOT 'smart' enough to handle anything else.)
Make your code *EXACTLY* this (preferably delete, not comment, the last line tho # passes)
Code:

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0].lower())
print(index)
#print(text.lower())

Do NOT 'ad lib': print alphabet.find(text[0].lower())
They are trying to teach you the concept of 'index'.

More: that website is very AI-lacking because this PASSES (with -1!)
Code:

text = 'Hello World'
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0].lower())
index = alphabet.find(text[0])
print(index)

The 'H' needs to be lower-cased to match the 'h' in alphabet, and index printed, regardless of value (-1 here!).


Continuing to torture that website, this passes, tho it prints 0 then -1!
Code:

text = 'H'
alphabet = 'H'
index = alphabet.find(text[0])
print(index)
index = alphabet.find(text[0].lower())
print(index)

I hope OP understands why 0 then -1! (Yes @OP? BtW @OP: please take all this with 'thick skin' i.e. don't be offended!)


(it also passes without the 2nd print, so its primitive 'AI' maybe just grep'ing for those last 2 lines!)

ajiten 03-24-2024 10:07 AM

Quote:

Originally Posted by ___ (Post 6491544)
Upon careful (&inferential) re-reading of #10, I think you neglected to say that you solved it, with the code you posted in #10. (In that case, mark Thread as Solv'ed with Thread Tools pull-down above #1)
IF not, here's my 'stern' reply (excuse the 'stern'):

You are, simply&clearly, **NOT** following the instructions!
Outputting a -1 *VIOLATES* their instructions & you FAIL.
Output *ONLY* a 7. Period. NOthing else.
Do it *THEIR* way (their site is probably NOT 'smart' enough to handle anything else.)
Make your code *EXACTLY* this (preferably delete, not comment, the last line tho # passes)
Code:

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0].lower())
print(index)
#print(text.lower())

Do NOT 'ad lib': print alphabet.find(text[0].lower())
They are trying to teach you the concept of 'index'.

More: that website is very AI-lacking because this PASSES (with -1!)
Code:

text = 'Hello World'
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0].lower())
index = alphabet.find(text[0])
print(index)

The 'H' needs to be lower-cased to match the 'h' in alphabet, and index printed, regardless of value (-1 here!).


Continuing to torture that website, this passes, tho it prints 0 then -1!
Code:

text = 'H'
alphabet = 'H'
index = alphabet.find(text[0])
print(index)
index = alphabet.find(text[0].lower())
print(index)

I hope OP understands why 0 then -1! (Yes @OP? BtW @OP: please take all this with 'thick skin' i.e. don't be offended!)


(it also passes without the 2nd print, so its primitive 'AI' maybe just grep'ing for those last 2 lines!)

Thanks for your detailed insights, and am following the many constraints, and am again stuck (the second time) at the step #80:
It states for the code:
Code:

# Append space to the message
        if char.isalpha():
            final_message += char

The not operator is used to negate an expression. When placed before a truthy value — a value that evaluates to True — it returns False and vice versa.

Add the not operator at the beginning of the if condition to check if the character is not alphabetic.


I made the change to the code as:
Code:

# Append space to the message
        if not(not(char.isalpha())):
            final_message += char

as, expect the logic to remain the same. But, it fails.
Then removed the outer 'not' too, still fails.

The error message is:
Sorry, your code does not pass. Try again.

You should use the not operator in the condition of your if statement.

ntubski 03-24-2024 12:02 PM

Quote:

Originally Posted by ajiten (Post 6491606)
Then removed the outer 'not' too, still fails.

The error message is:
Sorry, your code does not pass. Try again.

You should use the not operator in the condition of your if statement.

I think they have some test that is comparing your source code against the expected solution, and they don't expect the extra parentheses around the 'char.isalpha()' expression. Remove them and it passes (but this makes no difference in terms of the Python interpreter). Another example, introducing a differently named variable also "fails":
Code:

        c = char
        if not c.isalpha():
            final_message += c



All times are GMT -5. The time now is 04:30 PM.