LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2024, 01:14 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Rep: Reputation: 4
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.

Last edited by ajiten; 03-23-2024 at 01:53 AM.
 
Old 03-23-2024, 01:34 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Please post the code here between [code] [/code] tags. It is hard to guess what the problem might be without actually seeing the code.
 
2 members found this post helpful.
Old 03-23-2024, 01:43 AM   #3
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by Turbocapitalist View Post
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?
 
Old 03-23-2024, 02:27 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
The code and the suggested change works just fine for me
 
Old 03-23-2024, 02:40 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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

Last edited by NevemTeve; 03-23-2024 at 02:42 AM.
 
1 members found this post helpful.
Old 03-23-2024, 02:53 AM   #6
___
Member
 
Registered: Apr 2023
Posts: 140
Blog Entries: 1

Rep: Reputation: Disabled
Is your line 4: index = alphabet.find(text[0].lower())
Note the ())
It prints 7
 
Old 03-23-2024, 04:05 AM   #7
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by ___ View Post
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.

Last edited by ajiten; 03-23-2024 at 04:20 AM.
 
Old 03-23-2024, 04:13 AM   #8
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
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

Last edited by ajiten; 03-23-2024 at 06:49 AM.
 
Old 03-23-2024, 07:52 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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()))

Last edited by NevemTeve; 03-23-2024 at 07:58 AM.
 
1 members found this post helpful.
Old 03-23-2024, 09:35 AM   #10
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
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)

Last edited by ajiten; 03-23-2024 at 09:37 AM.
 
Old 03-23-2024, 10:07 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
3 members found this post helpful.
Old 03-23-2024, 07:15 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,266
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by ajiten View Post
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.
 
Old 03-24-2024, 02:24 AM   #13
___
Member
 
Registered: Apr 2023
Posts: 140
Blog Entries: 1

Rep: Reputation: Disabled
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!)

Last edited by ___; 03-24-2024 at 03:51 AM.
 
Old 03-24-2024, 10:07 AM   #14
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by ___ View Post
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.

Last edited by ajiten; 03-24-2024 at 10:16 AM.
 
Old 03-24-2024, 12:02 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by ajiten View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to find out the root cause of "UNABLE TO SSH to the box." kumark15 Linux - Newbie 8 10-11-2023 06:40 PM
CentOS7_Error: Unable to find kernel source code directory umma CentOS 3 07-17-2018 12:50 AM
Error in Perl Code : Bad switch statement(Problem in code block)? near ## line # suyog255 Programming 4 02-20-2008 05:35 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration