Reapeat input untill a certain word | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Reapeat input untill a certain word

Can anyone help me with this code please? I have to use the while statement. 🙏🏽🙏🏽

21st Oct 2023, 2:39 PM
Buzi Chaban
Buzi Chaban - avatar
13 Answers
+ 3
Be careful, the answer is in the task itself. Why did you leave the input out of the loop? What needs to be done so that the cycle always repeats?
21st Oct 2023, 4:03 PM
Solo
Solo - avatar
+ 6
Buzi Chaban , as already mentioned by Solo , we need to take the input inside the while loop. > there is also this issue: do not use ’ ‘ this special quotation marks for creating strings, python requires single or double quotes around strings like 'hello' or "hello" > based on your code, i prepared a basic input procedure. you can try and adapt it to your needs with the atrached file: https://code.sololearn.com/c5jQYPxMgZ02/?ref=app
21st Oct 2023, 7:38 PM
Lothar
Lothar - avatar
+ 4
Solo , yes, you are right, i have corrected it in the sample code. thanks !
21st Oct 2023, 8:08 PM
Lothar
Lothar - avatar
+ 3
#this is what I have so far zin = ‘utter the secret word, friends, and thy shall pass…’ key = ‘stop’ print(zin) word = str(input()) while(word == key): print(‘Thank you, cole again!’) break else: print(zin)
21st Oct 2023, 3:07 PM
Buzi Chaban
Buzi Chaban - avatar
+ 3
Lothar, Solo, it maybe best to describe how string formatting works as this topic is not covered in SoloLearn. To use a formatting string with f-string, we put the variable inside curly brackets. An example: word = 'abc' print(f'Variable of word: {word}') # Variable of word: abc f-string also works with triple quote. Inside triple quote there is no need for \n to break the line. \n is needed when using single / double quote if you want it output multiple lines. I made a code bits about string function days back. You may want to take a look and see how it can be useful. (Although it doesn't cover triple quote) https://code.sololearn.com/c81oGO8Ygckc/?ref=app
22nd Oct 2023, 2:39 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
while loop in c++ int x=10; int y=1; while(y<x){ cout<<"hello world"; y++; }
21st Oct 2023, 5:49 PM
Alhaaz
Alhaaz - avatar
+ 2
Lothar, that's right, but there is another inaccuracy: "the str() function is not needed here, since data input is already the default string type."
21st Oct 2023, 7:49 PM
Solo
Solo - avatar
+ 2
Thanks for the help. I think I got it. zin = 'Utter the secret word, friend, and thy shall pass... ' key = 'stop' print(zin) while True: word = input() if word != key: print(zin) else: print('Thank you, come again!') break
22nd Oct 2023, 12:21 AM
Buzi Chaban
Buzi Chaban - avatar
+ 2
Buzi Chaban, excellent, you have completed the task almost independently demonstrating the knowledge that you possess today. You can leave your code and take a look at it after some time to compare your future progress, but nevertheless it's still worth spending a little more time to fix this topic and try to shorten this code, especially since you have already been kindly provided Lothar layout to this task. Successful coding!...🖐️😎
22nd Oct 2023, 2:14 AM
Solo
Solo - avatar
+ 2
while (input() != "word"): print("Enter word: ") # rest of your logic here
23rd Oct 2023, 12:19 AM
Naiim Khaskhoussi
Naiim Khaskhoussi - avatar
+ 1
String Word=""; While (!Word.equals("certain word") cin>>Word;
23rd Oct 2023, 5:22 AM
Am me
Am me - avatar
0
Wong Hei Ming, that's right, but you can format without the format() method, but it has much more features. Example: greeting = '"...This is the %s\n of the %s."'%(names[2],owner[2]) print('\n\n{:_^36}\n\n'.format('Hi bro!'),greeting,'\n\n{:>36}'.format('Solo'))
22nd Oct 2023, 12:15 PM
Solo
Solo - avatar
0
Solo, I intentionally omit % style for a few reasons. 1. I'm not very familiar with that style. 2. It has many notations. 3. Format() methid is consist with other method mentioned in the course, such as str.lower(). A object.method() approach is easier to understand.
22nd Oct 2023, 3:11 PM
Wong Hei Ming
Wong Hei Ming - avatar