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?

24th Jan 2020, 11:17 PM
Hexagoñ
Hexagoñ - avatar
11 Respostas
+ 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.
24th Jan 2020, 11:35 PM
Erialdo Meta 🇦🇱
Erialdo Meta 🇦🇱 - avatar
+ 7
Erialdo Meta 🇦🇱 input is always string.
24th Jan 2020, 11:58 PM
Kevin ★
+ 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.
25th Jan 2020, 12:57 AM
HonFu
HonFu - avatar
+ 4
input()
24th Jan 2020, 11:29 PM
Kevin ★
+ 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.
25th Jan 2020, 1:10 AM
HonFu
HonFu - avatar
+ 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.
25th Jan 2020, 1:33 AM
HonFu
HonFu - avatar
+ 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!
25th Jan 2020, 1:44 AM
HonFu
HonFu - avatar
+ 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.
25th Jan 2020, 12:59 AM
Erialdo Meta 🇦🇱
Erialdo Meta 🇦🇱 - avatar
+ 1
Thanks!
24th Jan 2020, 11:31 PM
Hexagoñ
Hexagoñ - avatar
+ 1
Just assign the random string to a variable and you'll have access to it anywhere on you code
25th Jan 2020, 12:38 PM
John
John - avatar
+ 1
I second John. Just assign it to some variable and that's it
25th Jan 2020, 7:02 PM
sharpdesigner