I am new to Python and I have a question about using if statements inside while loop:
the code is for writing a number-guessing game till the number you entered matches the random integer generated.
the sample code is written as follows:
import random
answer=int(random.uniform(1,10))
number=int(input("guess:"))
if number==answer:
print("correct!")
while(number!=answer):
if number > answer:
print("smaller!")
number=int(input("guess again"))
if number < answer:
print("bigger!")
number=int(input("guess again"))
if number == answer:
print("bingo!!")
break
and the result I got was:
guess: 5 smaller! guess again 3 bigger! guess again 4 bingo!!
but when I used if-elif-elif or if-elif-else inside the while loop instead of the multiple if's, in the case of first guess was wrong and subsequent guess(es) were correct, the last bingo!! was not shown, for example:
import random
answer=int(random.uniform(1,10))
number=int(input("guess:"))
if number==answer:
print("correct!")
while(number!=answer):
if number > answer:
print("smaller!")
number=int(input("guess again"))
elif number < answer:
print("bigger!")
number=int(input("guess again"))
elif number == answer:
print("bingo!!")
break
the results became:
guess: 5 smaller! guess again 3
as shown, after last correct guess, the code just stopped, and "bingo!!" was not displayed.
Can anyone help me understand why this difference occurred when using multiple if's and if-elif-else/elif inside while loop? Thank you very much!
added: I realized my question eventually is, every time after I enter a new value for number, from where in the code it will be run again with the new number, and how is this different when I used multiple if's or if-elif-else.