[Tutor] Beginners question
DL Neil
PyTutor at danceswithmice.info
Mon Mar 30 22:56:43 EDT 2020
On 31/03/20 1:31 PM, Alex Kleider wrote:
> On 2020-03-30 14:11, Michael Langdon wrote:
>> Hi there,
>>
>>
>> I have been tasked to use a while loop to repeatedly ask the user to
>> enter
>> a word.
>>
>> If the word is not 'quit', then add it to the list of words collected so
>> far, in lower case. Then ask for the next word and so on.
>>
>> If they enter 'quit', do not add this word to the list, but print the
>> list
>> of words collected so far and exit the program.
>>
>> My input.
>> a = input("Enter a word: ")
>> sentence = sentence , a
>> while a != ("quit"):
>> sentence = sentence , a
>> a = input("Enter a word: ")
>> print(sentence)
>>
>> My output.
>> Enter a word: orange
>> ((), 'orange')
>> Enter a word: apple
>> (((), 'orange'), 'orange')
>> Enter a word: quit
>> ((((), 'orange'), 'orange'), 'apple')
>>
>>
>> Can you help me with this?
>
> Do you know about the key word 'break'?
>
> use it as follows:
>
> while True:
> # get input
> # if input means quit
> break # break out of the loop
> # proceed with whatever you want to do
I am 'breaking rules'* here...
The above always looks so untidy to my eye. Could we use the 'walrus
operator' instead of while True:?
*
1 I haven't loaded v3.8 at home, to be able to test for myself.
2 'walrus' is likely beyond the OP's level of learning
--
Regards =dn
More information about the Tutor
mailing list