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
6 odpowiedzi
+ 8
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
+ 5
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
+ 4
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
+ 4
There are two more methods to achive the same goal with the regular while loop. 1. Using an infinite loop with the break statement. ``` password='Sorry' while True: guess=input() if guess == password: break print('login in') ``` 2. Using the walrus operator. ``` password='Sorry' while guess:=input() != password: pass print('login in') ```
27th Aug 2025, 6:35 AM
Евгений
Евгений - avatar
+ 3
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
+ 3
i like the version with the `walrus operator` (also called `assignment expression operator`), so i recommend this.
27th Aug 2025, 10:36 AM
Lothar
Lothar - avatar