boolean AND loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

boolean AND loop

Hi I've just start learning Python. Can someone help me? I've tried to wrote a code for a boolean AND. I wrote this: a = int(input("\"a\": ")) b = int(input("\"b\": ")) x = a and b while x: if x == True: print("True") a = int(input("\"a\": ")) b = int(input("\"b\": ")) x = a and b else: print("False") a = int(input("\"a\": ")) b = int(input("\"b\": ")) x = a and b When the and statement is True while loop is OK and i can type a,b values again. But when the statement is False it prints nothing and just ends the program. What i did wrong?

11th Jan 2018, 9:04 AM
Michał Romanowski
Michał Romanowski - avatar
6 Answers
0
because when you write while x: your code is gonna run according to x, so, if x is true while loop is gonna run if x is false it s gonna continue its road without entering while loops block
11th Jan 2018, 3:14 PM
Mustafa K.
Mustafa K. - avatar
0
When it ends it's already not gonna print anything because else statement is working in while loop, so its just gonna print something while "while loop" is running
11th Jan 2018, 10:28 AM
Mustafa K.
Mustafa K. - avatar
0
So how I can code this? I want to run the program again and again even when the a and b will be False.
11th Jan 2018, 11:08 AM
Michał Romanowski
Michał Romanowski - avatar
0
write while true: and test the x, i mean while true: //we're putting codes in it to get loop forever if x: //do something else: //do something if you couldnt understand i can try to explain better note: you can write x instead of x==true
11th Jan 2018, 12:52 PM
Mustafa K.
Mustafa K. - avatar
0
So I modified the code with your instructions and now it works. a = int(input("\"a\": ")) b = int(input("\"b\": ")) x = a and b while True: if x: print("True") a = int(input("\"a\": ")) b = int(input("\"b\": ")) x = a and b else: print("False") a = int(input("\"a\": ")) b = int(input("\"b\": ")) x = a and b Thanks @Mustafa K.. But could you explain me more why "while x:" was wrong?
11th Jan 2018, 2:54 PM
Michał Romanowski
Michał Romanowski - avatar
0
Ok. I get it now. Thx mate.
11th Jan 2018, 3:40 PM
Michał Romanowski
Michał Romanowski - avatar