While loop - PYTHON


#1
original = "Hello"

answer = "yes"
while answer == "yes":
          if len(original) > 0:
                    print(original)
          else:
                    print("empty")
           answer = raw_input("do you want to continue? type 'yes' for yes, otherwise 'no' ")

Is this correct?


#2

All the indentation is correct (I don’t know why it is not showing up here)


#3

correct for what? An exercise?

you need to use format, see FAQ.

else: and the last line of your program don’t seem to have same indent level, seems one space difference.


#4

I am using the format. I don’t know why it’s not showing up here.
And correct in the sense, the logic. In general.


#5

I will check the one space difference, thankyou! That might be what is preventing it from giving an output.


#6

you didn’t, see here:

Quick Tips for Writing Good Posts

the program doesn’t do much, it just prints Hello as long as the user enters yes. But logic wise this is correct, after fixing the indent error

Did you code this in the codecademy lesson? If not, i would recommend setting your editor to 4 spaces soft tabs.


#7

I will check the one space difference thing, thankyou!


#8

Thank you! The problem got solved. I had not put proper indentation for the “Do you want to continue?” line.


#9

For python i would personally recommend soft tabs, meaning that when the tab key is pressed, the editor insert 4 spaces (4 spaces is generally recommend). The opposite is hard tabs, which insert a tab character.

You can’t change this in the codecademy editor, but if you work outside codecademy, its highly recommend to look for this setting in your editor


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.