0

Why we need two input one is outside loop and another is inside loop.why not outside or inside only

passward='Sorry' guess=input() while guess != passward: guess=input() print('login in')

26th Aug 2025, 4:58 PM
Prabin Tharu
Prabin Tharu - avatar
4 Réponses
+ 6
Prabin Tharu I agree that the code looks messy having two input() statements for one input text. This style is often seen in Python code due to its limited offerings of loop syntaxes. Python would be nicer if it supported a do...while or do...until statement like some other languages. In the code shown above you could adjust it this way in order to make it look cleaner, though it is a little less efficient: passward='Sorry' guess="" while guess != passward: guess=input() print('login in')
26th Aug 2025, 5:28 PM
Brian
Brian - avatar
+ 3
Hey! In your code, you asked the user for 2 input statements, because the first one is initializing the 'guess' variable. But you totally can use inside only! Just create the guess variable as an empty string, delete the first input, and nest every single statement you want to repeat while the guess is not the password inside the while loop. That will run until you enter the right password. If you would put it ONLY outside the loop, it will simply ask once then terminate. passward = 'Sorry' guess = '' while guess != passward: guess = input("Enter: ") print('login in')
26th Aug 2025, 5:33 PM
Linux
Linux - avatar
+ 3
or you can put the input in the while condition and directly compare to the string. no variables needed, no duplications. while input('Enter password:\n ') != 'Sorry': print('Wrong password! Try again...\n') print('Login sucessful!')
26th Aug 2025, 10:37 PM
Bob_Li
Bob_Li - avatar
0
There was a proposal to include do-while loops in Python, but it was rejected.[1] Some reasoning can be found in the maillist.[2] 1. [PEP 315 – Enhanced While Loop](https://peps.python.org/pep-0315/) 2. [[Python-ideas] PEP 315: do-while (maillist discussion)](https://mail.python.org/pipermail/python-ideas/2013-June/thread.html#21602)
27th Aug 2025, 6:25 AM
Евгений
Евгений - avatar