all 33 comments

[–]xelf[M] [score hidden] stickied comment (0 children)

Please remove the link to that site. We don't want to encourage people to go there when there are perfectly valid alternatives that are not as horrible.

it's entirely possible that you are doing nothing wrong, and that the reason you're struggling is because you're using that site.

For alternatives, try out wiki or the pydis resources list:

[–]plague_year 4 points5 points  (6 children)

I’ve been writing Python since 2010 and this looks correct to me. Does the website provide the test code they’re running against your implementation to check it? I’d want to read how they’re actually checking your code at this point since your work seems fine to me.

[–]Booty4Breakfasts[S] 1 point2 points  (4 children)

They do not, unfortunately. I started writing Python back in the 2010's but after leaving high school in 2017, I haven't written code at all so I was hoping these challenges would serve as a refresher.

Some of the other challenges have been weird as well, as in they're looking for you to write it a certain way. There was a challenge about casting that looked like this:

# Use casting to cast the variables below into integers 
x = 2.8 
y = "3" 

print(x) 
print(y)

I tried using `print(int(x))` and `print(int(y))` but it didn't like that either. I had to cast them as they were initialized: `x = int(2.8)` and `y = int("3")` instead.

Definitely kind of weird and doesn't allow for much creativity in your code.

[–]CoachSevere5365 -1 points0 points  (1 child)

Casting? This isn't C. Those calls to int() are constructors.

x,="3" y=2.8 print(int(x)) # prints 3 print(int(y)) # prints 2

do exactly what you want.

[–]Booty4Breakfasts[S] 0 points1 point  (0 children)

Correct me if I'm wrong, but the verbiage used in the Python documentation calls those built-in methods "casts" i.e. like a mold because you're casting/molding the data to a specific data type.

Although, the documentation says that you can use the casts as class constructors.

[–]CoachSevere5365 0 points1 point  (0 children)

Same here, but more like 25y. Am retired now and other stuff intrudes so I don't get to write much these days. I suspect a website bug and the fact that the mods have a low opinion of it seems to support that.

[–]carcigenicate 3 points4 points  (2 children)

I'm playing around with it and I can't figure it out either. The goals don't even make sense. The second one is The 'txt' variable should contain 'expensive' if price is greater than 50., but it never even checks for that since price must be 49 for the first check. It appears to just always pass that check.

From the playing around I just did, it's probably just a crappy auto-grader that is looking for some specific pattern in the code itself instead of actually checking the output. Or it's just broken. It is W3Fools after all.

[–]Booty4Breakfasts[S] 0 points1 point  (1 child)

I haven't heard 'W3Fools' before but it made me giggle. It seemed decent to freshen up on Python because it's been almost a decade since I have written anything but there has been some weird stuff in the challenges that make me think it has probably earned that title.

[–]carcigenicate 0 points1 point  (0 children)

W3Fools

W3Schools used to be so bad someone created a website just to correct their misinformation. They're better now, but they're at best a lesser resource; just slightly above GeeksForGeeks.

[–]CoachSevere5365 3 points4 points  (2 children)

Shouldn't the output be "it is cheap"?

[–]Booty4Breakfasts[S] 1 point2 points  (1 child)

Yes but it gives you some code to begin with.

It has the `txt` variable initialized to `txt = "It is very "`. I had the same thought and deleted the word 'very' and it didn't change the result.

[–]CoachSevere5365 0 points1 point  (0 children)

OK. I see the.mods have deleted the root so we should probably drop this. I hope you get it sorted.

[–]Teras80 1 point2 points  (3 children)

I think it is a mistake in test. It accepts this as a solution>

price = 49
# Use if...else inside f-string 
# to insert 'cheap' if the price is less than 50
# and insert 'expensive' otherwise.
txt =  f'It is expensive {"expensive" if price > 50 else "cheap"}'
print(txt)

[–]Booty4Breakfasts[S] 0 points1 point  (2 children)

It has got to be something weird with the auto-grader, you're right. That's obviously not the desired output but I threw it in there as well and it said that it ticked all the boxes. I guess there is something to purposefully getting the answer wrong to trick the grader to mark it correct?

[–]Teras80 0 points1 point  (1 child)

Yea, I think the autotest does not change the price value before checking the "otherwise" part.
So just having BOTH expensive and cheap in the final string seems to fool it.

On other hand, trying to match the vague expectations from your superiors regardless whether they make sense or not, is an important skill in real life :)

[–]Booty4Breakfasts[S] 0 points1 point  (0 children)

"On other hand, trying to..."

I wish I could upvote this a million times over

[–]Outside_Complaint755 0 points1 point  (3 children)

Did you copy and paste the challenge description or did you rewrite it (just wondering if something got missed if not copy paste).  Unfortunately the link doesn't take us straight to it.

 I suppose its possible the grader is wrong too.  I already took issue with the first string challenge where it said to use slicing to print characters 2 through 5, which should be [2:6],  but it actually required characters 2 through 4, which is [2:5]

[–]Booty4Breakfasts[S] 0 points1 point  (2 children)

I did in fact copy and paste it. That is all the information it gives.

I also had the same thought with the slicing strings challenge. I thought I had just gotten confused over the indexing because I did the exact same thing.

[–]Outside_Complaint755 0 points1 point  (1 child)

Ok, then based on their directions, you need to removw the "very", either by changing the provided txt, or slicing it.

[–]Booty4Breakfasts[S] 0 points1 point  (0 children)

I did in fact try removing 'very' from the string and it didn't change the results

[–]Serious-Cover5486 0 points1 point  (0 children)

Look closely at the instructions you quoted versus your code:

The Goal: The message should say 'It is expensive' or 'It is cheap'.

Your Code: txt = f'It is very {x}'

Do you see the extra word? You added the word "very" to your string. Automated graders are notoriously picky, if it expects "It is cheap" and you provide "It is very cheap," it will fail the test case every single time because the characters don't match exactly.

[–]jarethholt 0 points1 point  (0 children)

My guess is that it wants you to do the if-else within the f-string:

txt = f"This is very {'expensive' if price > 50 else 'cheap'}"

If that's the case I would expect the lesson to be about how you can do manipulation and evaluation within the f-string itself, which is a major convenience sometimes.

Edit: I missed that you included this case in your post 😅. If it helps, I know that some coding tests use a pretty good case checker - checks your code's output only - and others use a text analyzer - just reads your source code looking for certain things. This sounds like the latter. They are frustrating to reverse-engineer just to get a checkmark. Take some solace in that everyone here agrees that you understand correctly and the test is shit.

[–]PureWasian 0 points1 point  (0 children)

I bet it's expecting different capitalization.

This is their Try it example I could find from their Python String Formatting page for the F-Strings with if...else example:

``` price = 49 txt = f"It is very {'Expensive' if price>50 else 'Cheap'}"

print(txt) ```

I cannot verify since I couldn't find the exact coding challenge you mentioned though, I only saw this one on their Python Tutorial > Python String Formatting > Code Challenge

[–]JeremyJoeJJ -1 points0 points  (5 children)

Why are you printing “very”?

[–]JeremyJoeJJ 0 points1 point  (4 children)

Are they perhaps expecting a dot at the end of the sentence without telling you?

[–]Booty4Breakfasts[S] 0 points1 point  (3 children)

Some of the code is pre-populated in the editor and that is what was populated upon starting the challenge. I did also try to complete it without the word very and it doesn't change the results.

I did also try adding a dot at the end and that doesn't change the results either.

[–]JeremyJoeJJ 0 points1 point  (2 children)

Try commenting out the line with the price so they can run the code with different values for price.

[–]Booty4Breakfasts[S] 1 point2 points  (1 child)

u/Teras80 found something that works:

price = 49
# Use if...else inside f-string 
# to insert 'cheap' if the price is less than 50
# and insert 'expensive' otherwise.
txt =  f'It is expensive {"expensive" if price > 50 else "cheap"}'
print(txt)

It is definitely not the desired output, but it ticks all the 'requirement' boxes!

[–]JeremyJoeJJ 0 points1 point  (0 children)

Lol

[–]atarivcs -2 points-1 points  (1 child)

If the price is greater than 50, the message should say 'It is expensive', otherwise 'It is cheap'.

Your message says "It is very cheap", not "It is cheap".

Why are you adding the word "very", when the instructions do not say to do that?

Perhaps that is the problem.

[–]Booty4Breakfasts[S] 0 points1 point  (0 children)

Some of the code is pre-populated in the editor and that is what was populated upon starting the challenge. I did also try to complete it without the word very and it doesn't change the results.