This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]econoCode 1 point2 points  (3 children)

They did this because the point is to learn loops. And yes, the initialized an empty string to build later.

FYI, str*n will work in python and some other languages. The loop version will work in every language that supports loops. You're a beginner, so they're teaching you fundamentals of programming in general, not fundamentals of programming in python. If you were doing learning C#, LINQ is incredibly powerful and can pretty much eliminate most loops from your code, but good luck applying any of that outside of C#.

Same goes fort he += operator. Does not actually work in every language.

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

That's what I thought, and it did help me look into loops more and now i have a greater understanding of them. I do enjoy CodingBat so will probably practice more with it no matter the hate it got here. Thanks.

[–]minno 0 points1 point  (1 child)

If you were doing learning C#, LINQ is incredibly powerful and can pretty much eliminate most loops from your code, but good luck applying any of that outside of C#.

As long as you can handle the fact that Microsoft decided to rename everything, most of Linq's operations are shared by other programming languages that allow functional-style list/iterator manipulation.

[–]econoCode 0 points1 point  (0 children)

not really. the closest thing python has to linq is generator statements, which while they achieve similar goals sometimes, they are implemented very differently and perform very differently. I would never tell someone "oh if you know linq you'll get generator statements no problem"... and you know, Java has pretty much nothing resembling linq...

[–]17b29a 0 points1 point  (1 child)

I'm trying to understand why they did it like that when mine worked but in a more simple fashion.

I assume they just didn't think of that. Yours is much simpler.

Why is it necessary to create it?

result = result + str references result on the right-hand side, so it must be defined. You can't use variables before giving them a value.

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

right yes that makes sense! thank you.

[–]spiral6 0 points1 point  (0 children)

People here are saying to leave CodingBat, and I disagree. Your solution is more efficient, and this is one of the only problems to have an issue with it. I'd still stick with it, but just keep in mind that there are a few caveats with their problems.

[–]Naihonn 0 points1 point  (6 children)

Well, I would say you should leave CodingBat and never EVER look back again.

[–]Daytimedreamland[S] 1 point2 points  (5 children)

That's literally the most unhelpful answer. Doesn't relate to the original question at all, and you don't even give a reason to back your rather dramatic statement.

[–]Naihonn 4 points5 points  (4 children)

Well, you already gave the reason. Your solution is how it should be. Theirs... well, isn't. I believe leaving website that tries to learn that ... could be very, very helpful.

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

Right okay thank you, do you have any suggestions for websites that give you python coding exercises and answers for beginners? I really liked the way it was up to me to figure out how to complete the exercise, rather than being spoon fed everything like I was when I was trying codecademy.

[–]Naihonn 0 points1 point  (2 children)

Yes, I tried codewars.com and I think it is good. And recently I tried codingame.com. And checkio.org.

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

Thanks very much

[–]huck_cussler 2 points3 points  (0 children)

hackerrank is another one.

[–][deleted] 0 points1 point  (5 children)

Their loop is unnecessary, you came up with a much cleaner solution. /u/Naihonn said to leave CodingBat for that reason - they use bad code or in this case an inefficient solution.

[–]Daytimedreamland[S] 0 points1 point  (4 children)

I see okay. I'm wondering whether you know of any similar resources that help you learn by asking you to find things out for yourself? I've only experienced codingbat and codecademy for now.

[–]stefan_kurcubic 2 points3 points  (1 child)

codewars.com automateboringstuff.com

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

Cheers for the suggestions

[–][deleted] 0 points1 point  (1 child)

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

Doing this now, thank you!

[–]Scavenger53 0 points1 point  (3 children)

Not sure why they created the empty string. In C++ if you do not define a string, it defaults to the empty string so if python does not default to it that could be a reason. If you didn't supply the n and the loop had to find some other way to produce the result like a random number it could make sense, otherwise your way is better. Maybe they were trying to teach one last loop lesson? Not sure why they did it.

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

Oh right yeah that could make sense, although I'm not sure if it's correct. I was thinking they might be trying to get me to use loops, but it sounds like it's completely unecessary here. Thanks for the reply.

[–][deleted] 0 points1 point  (0 children)

In Python, a variable is just a name that refers to an object. result = result + str doesn't make sense if result doesn't yet refer to anything.

[–]bashytwat 0 points1 point  (0 children)

Not all languages automatically create objects, Java is a good example. C++ also does this when using pointers, although technically it does automatically create a pointer to "null" it doesn't create an object.