i've been having trouble figuring this out since im new to python and i really wanna figure it out!

Write a Sentinel controlled While loop that allows the user to enter text until they enter ! by itself.

Output would be like this:
Enter some text: Hi
Enter some text: ThisIsFun
Enter some text: Bye Now
Enter some text: !
HiThisIsFunBye Now!

Heres what i have to far.

user = 0
total = 0

text =str(input("Enter some text "))

while total != 1:
    total = text + 1
    
    print(total)

Dani AI

Generated

You are close. The key is: read inside the loop, stop only when the user types the sentinel ! by itself, and do not print each entry as you go. Instead, accumulate the lines and print once at the end. Also note for Python 3 you should use input() (what showed with raw_input() was the Python 2 name). ’s function-based approach is fine conceptually; this version just updates it for Python 3 and matches your expected output (HiThisIsFunBye Now!).

# Python 3
parts = []
while True:
    s = input('Enter some text: ')
    if s == '!':            # stop only if the line is exactly "!"
        break
    parts.append(s)         # save, don't print yet

print(''.join(parts) + '!') # concatenate exactly as typed, then add "!"

Why your earlier output had numbers: you were incrementing and printing a counter each loop. If you want a count for debugging, compute it after collecting: print(len(parts)). But do not print it during the loop if the final output must be only the concatenated text.

Tip: If you like a more compact pattern, Python can iterate until a sentinel without a manual while True:

from functools import partial

parts = list(iter(lambda: input('Enter some text: '), '!'))
print(''.join(parts) + '!')

Both snippets handle spaces correctly (e.g., Bye Now) and stop only when the user enters ! alone.

Recommended Answers

All 8 Replies

What are you using the 'user' variable for? You should use raw_input to set the 'text' variable and if you want it to be called more than once you have to put it inside the while loop.. something like this:

total = 0
text = ''
while text != '!':
   total += 1
   text = raw_input('Enter some text\n')
   print(text)

i was just basing this off an example in a python book i have. thanks for you help.

the code is getting a little closer now, i just dont have any idea what to change. this is the output:

Enter a number 1
Loops Are Neat!
Enter some text
hello
2
Enter some text
Whats up
3
Enter some text
how are you
4
Enter some text
!
5

.... What's the problem? The numbers?

its supposed to not have the numbers, and it stores the text until ! is typed then it prints all of the text.

The code I posted doesn't print numbers, so the code you're using is different

ah i see, yeah im using the newest version of python

def sentinel(s='!'):
    t = inp = raw_input('Enter some text: ')
    while inp != s:
        inp = raw_input('More text: ')
        t += inp
    return t

if __name__ == '__main__':
    print(sentinel())
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.