+ 2
Infinite loops
Can someone please help me understand or explain something in the following lines of code please? 1.while 1: 2. a=input("Enter 'q' to quit:") 3. 4. if a =="q": 5 break I would like to know why 'q' has single quote marks around it (on line 2), and why "q" has double quote marks around it (on line 4)?
3 Respuestas
+ 3
Thank you all for your answers.
+ 1
It’s made purposely, if you open a string with double quotes, all the single quotes inside that string will be just another character inside that string, unless you close the string with the double quotes.
It works viceversa too.
+ 1
Because you used double quotation marks in line 2 to indicate the sentence as a string, using double quotations marks again within it would signify that the string ended, resulting in there being 2 separate strings ("Enter" and "to quit:") with a q in between them. As this would result in an error, single quotes were used to not exit the string and still use some form of quotation marks. The same could be done in reverse with the outside string being indicated with single quotes and the q being represented with double quotes. Because it is being used within the middle of a string, the type of quotes don't really matter (as long as they don't exit the string), which is why double quotes were used the second time. On line 4, either double quotes or single quotes could be used as they both indicate that the q is a string.
Hope this helped! d: