I am a complete beginner currently doing a online course of python to challenge myself and i have hit a wall so to speak. I would like to know how to stop a while loop using the word stop or two consecutive same words. This is the current code that i have:
all = "" #store variable
while True:
entry = input("Enter a word: ")
if entry == "stop":
break
all += entry + " " # add to list
print(all)
To stop the loop with two consecutive words, you can use split
to get the last word entered and compare it to the current word.
Try this code:
all = "" # store variable
while True:
entry = input("Enter a word: ")
if entry == "stop":
break
if len(all) and all.split()[-1] == entry: # if same as last word
break
all += entry + " " # add to list
print(all)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.