Python - Loops question

Hi Guys,

I’m just having some trouble answering this question

x = 1
y = 1
while x <= 3 and y <= 10:
x = x + 1
y = int(input('Enter a whole number: ')
print(x *2)

Assuming the user correctly enters a whole number, which one of these statements is correct:

  1. The while loop will iterate at most 3 times, but possibly fewer
  2. The while loop will iterate 0 times
  3. The while loop wil iterate at least 3 times and possibly more
  4. The while loop will iterate exactly 3 times
    5)None of the answers

I know what the output would be, and I think the loop will only iterate once, but I am not sure. Would appreciate some help!

It will iterate at least once.

Aside

There is a missing ) on your input line.

1 Like

Hey there,

if you run your code in a console (after adding the missing bracket) you will see that it executes at least once, before the user is asked to input. Then there is really only 2 choices. Either the number they input is less than equal to 10, in which case the loop will continue, or if they enter any number greater than 10, the loop will stop.

So given that the user always enter a number smaller than or equal to 10 it would basically make the second part of the while statement (y <= 10) always true and then it only matters what the first statement comes to (x <= 3). And this statement has been determined by your code, so there is no uncertainty. This means that the code will execute a maximum of 3 times (because that’s how it’s been programmed) but it may be fewer, if the user enters a number greater than 10, which would end the loop immediately.

So the correct answer would be 1.

Hope this makes sense :slight_smile:

Thank I didn’t notice that!

1 Like

Oh that makes complete sense, so maximum is 3, but could be fewer because it depends on the y input.

Thank you soo much for your help!

Hello everyone!!

Its my first time here, so I hope I am writing in the proper place and not bothering anyone!!

Here is my question, and thank you for reading!!

Its an activity from the Python 3 beginners course. I’ve been for a while trying to understand why I don’t get the result I should in the second function call, the “Hello world” one. Any hint about why ¿?

In the first function call I get “Cdcdm” but in the second one I get “Hlloworld”

Thanks for the help!

Write your every_other_letter function here:

def every_other_letter(word):
other = “”
for letter in word:
if word.find(letter)%2 == 0:
other += letter
return other

Uncomment these function calls to test your function:

print(every_other_letter(“Codecademy”))

should print Cdcdm

print(every_other_letter(“Hello world!”))

should print Hlowrd

print(every_other_letter(“”))

should print

You might have an indentation error, but it’s difficult to tell b/c your code isn’t formatted. If you could please format your code by using the “</>” button above, that would be helpful.
Please see here on that.

Going forward, this is a good guide to check out before posting. It’s under the “New User Guide” on the home page:

That said, remember that spaces also have an index value. And, .find() finds the first index value of the occurrence.

This might be of some help:

1 Like