#Help me with codewars
101 messages · Page 1 of 1 (latest)
Just one for loop
uh...your code doesn't make much sense
your while loop is false so it never runs
I am not sure why you are assigning 1 to num when the function starts
well, how would you know when to stop the loop if you reassign num? Like if I passed 3 to the function, the first line would reassign num to 1 and you'd have no way of knowing what the original value was so you wouldn't know when to stop the loop
try using a for loop
we use break in js
ther have to but sum in py
break is also in python but you still wouldn't know when to break out of the loop.
if statment
yeah but what would the condition be if you reassign num to 1??
do you rlly need while loop
No, that's what I am saying. You could use a while loop, but a for loop is simpler for this kind of problem.
Lets try a simpler exercise to help you get there one step at a time.
Do you know how to write code that prints 1 2 3? Just prints those 3 numbers.
Hint: use one for loop.
Good job
So you understand it is doing the print(i) for each thing in the list
A for loop is just doing the same thing for each item in a list
For each shirt in my laundry: fold the shirt
So now, instead of printing 1 2 3.
How would you modify your code to print
1 sheep
2 sheep
3 sheep
now, you are overcomplicating it a little bit
so, you started with code that prints 1 2 3
the only thing this needs to do differently
is to print 1 sheep instead of 1
for each number
so my hint is, you can use the code you used above to print 123, but the only thing you need to change is the line with the print
instead of print(i)
you need to print i sheep, how would you write that?
This is the code you had:
list = [1,2,3]
for i in list:
print(i)
You only need to change the line that says print(i)... which prints the number. Instead of just printing i, you want to print i followed by the word sheep. Can you write the print that does this?
Start with the code that prints 1 2 3, because you did that correctly.
You correctly made a list of [1, 2, 3], and then did your for loop over that list. The last bit of code you printed did not make sense because you did the for "in sheep" instead, sheep is a variable that says "sheep..." so that's not what you want the for loop over.
Good job!
Try running this code. What does it output? Then we can move on to the next step.
Just run the code, don't worry about turning it into a function yet.
You can just place that in a python file and run it, no need to add anything. Or paste it into the python interpreter.
That's okay, we are getting there one step at a time
It prints
1 sheep
2 sheep
3 sheep
Right?
Or something like that? With 1 2 3?
Great. So... The next step is to learn about the range function. You will need this function because, right now, you are hard-coding the list to contain 1 2 and 3.
But in your exercise, it needs to go up to n. So you need a function that will give you a list of numbers up to n.
Replace the first line:
list = [1, 2, 3]
With this instead:
list = range(3)
And then run the code again, what does it output?
(It will output something "wrong" but we will learn how to fix it in a moment!)
Good
But we want it to start at 1 and end at 3
No
We are going to do a little math instead
Do not change the range function call
Instead - what math do you do to turn 0 into 1?
Really simple answer-- you add what?
do, it was a typo
great
so instead of print i
print i+1
can you modify your print to do that?
good, what does your code look like now?
and does it work when you run it?
good
now... try changing range(3) to say range(10) instead. Can you guess what it will print now?
Good
So whatever number you give to range, you now have code which prints that many sheep
The next step is to put this inside a function
def count_sheep(number):
this right?
good, so it takes a variable called number.
So you can use the code you have written, but now
you are going to just change the first line to say range(number)
because you want to use the number variable given to the function
make sense?
thats okay, we are almost there
the assignment does not want you to print sheep, right? it wants you to return a string?
It says "return a string"
at least what you copied did
yeah
so instead of printing, we need to construct a string to return
can you show me the code you have now?
and do you know the difference between printing and returning a string?
great
So functions in python can have outputs and side effects. Outputs are what is returned when we call a function.
def hello():
print("hello!")
output = hello() # Call the hello() function
This function prints hello to the screen, but it doesn't return anything. It has no output, so output variable doesn't have any value. It prints hello to the screen as a side effect.
However, this function RETURNS the string hello:
def hello():
return "hello!" # Return instead of printing
output = hello() # Call the hello() function
Now, the function does not print anything. But the variable output now contains the string "hello!" which was returned from the function.
To return something we have to write return followed by what we want to return.
Okay, so in this assignment, it is asking you to return a string from the function, not print the string.
The way to do this is to construct the string. I'll write some comments to help you out.
def count_sheep(number):
# create an empty string called output
# ...
list = range(number)
for i in list:
# concatenate "i sheep... " to the output string
# return the output
Basically, you are going to start by creating an empty string.
Then, in the for loop, instead of printing each sheep, you will add it to that string. (Also called concatenation)
Then, when everything is done, you return the string.
Does that overview make sense?
Okay, so in this assignment, it is asking you to return a string from the function, not print the string.
output += 'Sheep...'
you also need to concatenate i
output += str(i) + 'Sheep...'
where do you think??
in the for loop
so add 1
what are you trying to add one to? output or i?
so where would you add one?
try it
dude there is only one place where you can add 1 to i
share the code
you are adding 1 to a string. add it to i when it is an integer. str() converts and an integer to a string
Hey @torn locust When you are back - let me first encourage you, you are so close to solving this problem.
Think about your code in steps like this:
- Add 1 to i
- Convert the result of that into a string.
Now, you do the steps like this: i + 1- Convert the above using
str()
Hence, you will write code that looks like this:str(i + 1)- I'm telling you the answer in case you didn't think you were allowed to pass an expression to str. It doesn't have to just be a variable. You can write an entire expression likei+1and give that to a function.
I want to also explain why you had to write output += and not just output +
This is because writing output + "whatever" does not change the value of output. It only creates a new value that is the result of adding those two strings together.
You have to then assign it back to output in order to update the value of output.
output = output + "whatever"
The sign += is a shortcut way of saying this.
output += "whatever"
# is the same as:
output = output + "whatever"