Python Forum
Am I wrong or is Udemy wrong? String Slicing!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Am I wrong or is Udemy wrong? String Slicing!
#1
Hi,
I'm using the 2022 Complete Python Bootcamp From Zero to Hero in Python course.
It features a series of test excercises..but I am getting errors even though the answer appears correct?

Udemy Question:
Use string slicing to grab the word 'ink' from inside 'tinker'

My attempt:
print('tinker'[1:4])

Udemy tells me:
Oops your solution is incorrect
'ink' != None " Sorry. that was incorrect
Your output
ink

um..I am confused as to why I am wrong! Thanks!
Reply
#2
Maybe the answer is simply 'tinker'[1:4] ?
Mavoz likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Given the response you get, rob101's solution is sensible.

Why are you printing?

Do you know what an expression is? "tinker"[1:4] is an expression that evaluates to "ink". A function call is also an expression and it evaluates to the return value of the call. The print function's job is to write it's arguments to the console, not to produce a value to be used in the program (for example in a comparison used for the test case you have). Therefore, print returns None.

To add some more colour to the story: when we write software in the real world, we generally don't print things to check it's working correctly. The reason why not is humans: we're error prone! I could misread the output for example and say something's working when it isn't. Not only that but systems can be quite large, so this process would be time consuming and tedious. So what we do instead is get the computer to do the checking for us: we write test code that exercises the real code of the system to check it does the right thing. If something's not working right, these tests will fail and we can see a report of which ones have passed/failed along with what the failures were. Moreover, we can get computers to take action automatically based on whether the tests have passed or not. For the kinds of systems I work on (web apps, in my case APIs), this means either deploying the application if the tests pass or not deploying and alerting us if there was a failure.

In fact, you're seeing the benefits of this kind of automation when you're submitting your solution to Udemy. Imagine if you had to wait for someone to check your answers! One bad thing could be there might not be anyone online in your timezone so you would have to wait a while and not get the fast feedback that the website gives you.
Mavoz and rob101 like this post
Reply
#4
(Nov-05-2022, 01:32 AM)rob101 Wrote: Maybe the answer is simply 'tinker'[1:4] ?

(Nov-05-2022, 05:54 AM)ndc85430 Wrote: Given the response you get, rob101's solution is sensible.

Why are you printing?

Do you know what an expression is? "tinker"[1:4] is an expression that evaluates to "ink". A function call is also an expression and it evaluates to the return value of the call. The print function's job is to write it's arguments to the console, not to produce a value to be used in the program (for example in a comparison used for the test case you have). Therefore, print returns None.

To add some more colour to the story: when we write software in the real world, we generally don't print things to check it's working correctly. The reason why not is humans: we're error prone! I could misread the output for example and say something's working when it isn't. Not only that but systems can be quite large, so this process would be time consuming and tedious. So what we do instead is get the computer to do the checking for us: we write test code that exercises the real code of the system to check it does the right thing. If something's not working right, these tests will fail and we can see a report of which ones have passed/failed along with what the failures were. Moreover, we can get computers to take action automatically based on whether the tests have passed or not. For the kinds of systems I work on (web apps, in my case APIs), this means either deploying the application if the tests pass or not deploying and alerting us if there was a failure.

In fact, you're seeing the benefits of this kind of automation when you're submitting your solution to Udemy. Imagine if you had to wait for someone to check your answers! One bad thing could be there might not be anyone online in your timezone so you would have to wait a while and not get the fast feedback that the website gives you.

Thanks for the thorough reply..much appreciated. That makes sense..as a beginner I'm used to regularly using print console..but can see 'return' is the key word.

Interestingly enough someone on Udemy did reply too...given there hadn't been a lot of action there in the comments...was quite amazing. They said the same thing as you all here...don't use print! Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  wrong entries in sqlite database and tkinter epsilondatum 2 343 Apr-23-2024, 04:48 PM
Last Post: epsilondatum
  I don't know what is wrong (Python and SQL connection) shereen 3 370 Apr-01-2024, 08:56 AM
Last Post: Pedroski55
  Python3 string slicing Luchano55 4 626 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 491 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Is this a multiprocessing bug in Python - or am I doing something wrong? haimat 1 1,199 Oct-18-2023, 06:07 AM
Last Post: absalom1
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 2,083 Aug-13-2023, 12:16 AM
Last Post: cubangt
  Wrong type error rowan_bradley 6 1,231 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,057 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  np.percentile returns wrong value? AceTylercholine 2 663 Jul-13-2023, 06:59 PM
Last Post: Skaperen
  Something wrong with my code FabianPruitt 5 868 Jul-03-2023, 10:55 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020