0
How do i refer to the strings given in code coach challenges?
I use python for them and i generally know how the code should be written, but i don't know what to put to represent the input shown in the results window. For example a challenge where i have to find how many letters are there in a random string. I know it should be lke this: print(len(randomstring)) How do i refer to that random string?
11 Respuestas
+ 7
either you assign the input like this
a=input()
input by default is converted from python interpreter to string
the input is assigned to variable a.
Or you get your string input length directly like this
print(len(input()))
If you have only one entry from user.
If you have multiple inputs you assign them to variables like this.
a,b,c = input(), input(), int(input())
where a expects a string input
b expects a string input
c expects an integer input.
+ 7
Erialdo Meta 🇦🇱 input is always string.
+ 6
I would say that it is indeed wrong to write str(input()), since it is a redundant operation.
We're interested in reaching our program goal not in just any way, but in an efficient way.
Sometimes there may be some added value going the extra mile, but turning something into a string which is already a string has no merit.
If str(input()) was alright, then str (str(str(input()))) would be just as right.
+ 4
input()
+ 4
Erialdo Meta 🇦🇱, sure, most of us are in some beginning stage, and it's not always necessary to be crazily accurate - you can't learn it all in one day.
Lacking efficiency is something I observe a lot here, though - people write three times the amount of code they'd have to.
I'm sometimes surprised and awed myself by what the really strong people here can do, increasing efficiency, writing just a few lines instead of what I did.
Well, we can only try.
But we *should* try, and maybe especially in an interpreted language like Python, where no ingenious compiler will optimize some of the nonsense away.
Even if - we should always try to write efficiently. It's also easier to read and maintain.
+ 3
I'm not necessarily talking about complicated stuff like optimization, hacks or anything.
My meaning is just: Try to be efficient, don't write redundant stuff.
When three or four conditions are enough for rock paper scissors, don't write 9.
When you can write a loop or a function, don't repeat code.
When a value is already a string, don't turn it into a string.
I think, although they may initially do it wrong, it's not too hard for beginners to understand.
+ 3
No no, just the regular DRY vs WET stuff.
Sometimes people write a whole class, and it hasn't a single method in it, just three attributes.
Why not just take a dict? Or even a tuple?
I'm sure you can write a 300 line object oriented rock scissors paper that behaves exactly like the oneliner, if you try hard.
And that's just a bad habit that we should point out in beginners from day one.
Don't babble in code!
+ 2
HonFu you are right. I was just talking about semantics not code overhead since it is out of the scope of what the poster asked.
+ 1
Thanks!
+ 1
Just assign the random string to a variable and you'll have access to it anywhere on you code
+ 1
I second John. Just assign it to some variable and that's it