#๐Ÿ”’ Simple loop (while)

82 messages ยท Page 1 of 1 (latest)

rugged lagoon
#

Hi! Need help with this exercise:

Part 1

Please write a program which keeps asking the user for words. If the user types in end, the program should print out the story the words formed, and finish.
Sample output

Please type in a word: Once
Please type in a word: upon
Please type in a word: a
Please type in a word: time
Please type in a word: there
Please type in a word: was
Please type in a word: a
Please type in a word: girl
Please type in a word: end
Once upon a time there was a girl
Part 2

Change the program so that the loop ends also if the user types in the same word twice.
Sample output

Please type in a word: It
Please type in a word: was
Please type in a word: a
Please type in a word: dark
Please type in a word: and
Please type in a word: stormy
Please type in a word: night
Please type in a word: night
It was a dark and stormy night

My code:

# Write your solution here

sentence = ""
previous_word = ""

while True:
    word = str(input("Please type in a word: "))
    if word == "end":
        break
    sentence += word + " "
    previous_word = word
    if previous_word == sentence:
        break
    
    print(sentence)
    print(previous_word)
print(sentence)

I'm able to create a function for saving inputs to variable as well as a variable that only saves last input. I'm however not able to create a function for comparing last input to previous inputs and if matching break the loop.. How can I go about this?

subtle wadiBOT
#

@rugged lagoon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

south palm
#

have you worked with a list before?

balmy anvil
#

Also, do you know if order matters? A word twice in a row, or would you break if the same word has been entered twice?

#

@rugged lagoon

rugged lagoon
rugged lagoon
balmy anvil
#

You will need some way of storing the inputs you get (all of them)

#

The easiest way to do that would be to use something like a list

south palm
rugged lagoon
#

Yes I've created the sentence and previous_words variables for storing the inputs

#

Yes I understand that but the constraint here is not to use a list, only conditional statements and while loops

balmy anvil
#

You can also create a string, it's essentially the same thing

rugged lagoon
#

How?

south palm
#

@balmy anvil can you go on, you're explaining the other way

rugged lagoon
#

I believe my code is correct but the ordering of the logic is wrong

balmy anvil
#

For each time you get the input, do something like

previous_words += word

In order to check if the current word is already in that list, you will have to make an if statement. Something like this could do:

if word in previous_words:
    break
#

As you are using strings, this would however fail at certain conditions. Example where a new word is a sub word of another word.

rugged lagoon
#

thanks, its working better now

#
# Write your solution here

sentence = ""
previous_word = ""

while True:
    word = str(input("Please type in a word: "))
    if word == "end":
        break
    
    if word in previous_word:
        break
    sentence += word + " "
    previous_word += word
print(sentence)

The problem now is it is reaching some sort of word limit

subtle wadiBOT
#

Hey @rugged lagoon!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
balmy anvil
#

You could completely avoid that by using a list:

# create list
previous_words = []

# add to list
previous_words.append(word)

# comparison
if word in previous_words:
    break
rugged lagoon
#

Once upon a time there was a girl > maxes out at "Once upon a time there was"

rugged lagoon
#

Why is the variable reaching a word limit?

balmy anvil
rugged lagoon
#

yes

south palm
rugged lagoon
#

im doing the MOOC Python couse

south palm
#
sentence = ""
previous_word = ""
while True:
    word = str(input("Please type in a word: "))
    if word == "end" or previous_word == word:
        break
    if not sentence:
        sentence = word
    else:
        sentence += f" {word}"
    previous_word = word

print(sentence)
rugged lagoon
rugged lagoon
balmy anvil
south palm
rugged lagoon
#

yes your code works

#

@south palm

#

passes the test

balmy anvil
rugged lagoon
#

let me see if i understand what ur code does

balmy anvil
#

Aha you did it with f string

south palm
#

its comparing if the word is either end or equal to the previous word and exists
else it adds to the sentence and then assigns to previous word

balmy anvil
south palm
#

the if not sentence section, just checks if the sentence is empty, to not have space in the start of the sentence

rugged lagoon
#
#Creating variables for storing and comparing inputs
sentence = ""
previous_word = ""

#create a loop asking for multiple inputs
while True:
    #user input
    word = str(input("Please type in a word: "))
    #if condition that breaks if user inputs "end" or if latest input matches word in variable "previous_word"
    if word == "end" or previous_word == word:
        break
    #not sure what this if and else statement does
    if not sentence:
        sentence = word
    else:
        sentence += f" {word}"
    previous_word = word

print(sentence)
south palm
#
words = []
while True:
    word = str(input("Please type in a word: "))
    if word == "end" or (words and (words[-1] == word)):
        break
    words.append(word)

print(" ".join(words))

this is the list equivalent, cleaner won't you say ๐Ÿ˜›

rugged lagoon
#
sentence = ""
previous_word = ""

while True:
    word = str(input("Please type in a word: "))
    if word == "end":
        break
    
    if word in previous_word:
        break
    sentence += word + " "
    previous_word = word
print(sentence)

This is my code. Is there a way to make it work?

balmy anvil
#

Just know that the condition I talked about earlier is still true:

word1: pineapple
word2: apple

This will break the loop

rugged lagoon
#

i think that's acceptable for this exercise ๐Ÿ™‚

#

But is there a way to make my code work?

#

I'm trying to learn what i did wrong

south palm
#

changed in with ==

#

if word == previous word

balmy anvil
#

And compare with sentence I think

rugged lagoon
#

Now its working!! It was the "in" vs "==" creating problems

#

now it wont accept subsequence words @balmy anvil

#

i think

balmy anvil
#

Try

rugged lagoon
#

i mena it will*

#

mean

#

also

#

@south palm thanks for the solved code but that way i dont really learn anything ๐Ÿ™‚

balmy anvil
#

Also you can do:

apple
foo
apple

Right?

south palm
#

so like a in aaa is true

rugged lagoon
#

yes i can @balmy anvil

#

why is that

balmy anvil
#

Because you only compare previous_word, not sentence

#
if word in sentence:
    break
south palm
rugged lagoon
#

thanks ๐Ÿ™‚

dusty tusk
#

!rule 8 @south palm

subtle wadiBOT
#

8. Do not help with ongoing exams. When helping with homework, help people learn how to do the assignment without doing it for them.

dusty tusk
#

@rugged lagoon You said you've created some functions but I don't see any in your code.

#

Is that all of it?

south palm
dusty tusk
#

cool cool

subtle wadiBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.