# Module 2 challenge

I expected an appropriate output for the challenge 2 mentioned in module 2 for the bingo game.
The problem statement is-
Write a python code for a bingo game.

The code should ask the user for a number and it should check it with the predefined number in the code. If it is the same, then the code should output Bingo!.

For example, the guess = 51.

The code asks the user to input a number between 0 - 99.

User inputs 51.

The code will output Bingo!. If the user inputs anything else, it should say Better Luck Next Time!.

The code I tried is as follows-
b=0
while(1):
b=b+1
if(b<=21):
break;

n=int(input("Enter any number of your choice from 1 to 20: "))
if n==b:
print(“Bingo”)
else:
print(“Better luck next time”)

Output being display is-
Enter any number of your choice from 1 to 20: 10
Better luck next time
but the expected output should be Bingo.
Please help me out to resolve this issue.

Sir, try using random module to generate the b in your code.
it should be:
import random

b = random.randint(0,20)

actually, while(1) this is an infinite loop so if the loop is not terminated the control does not transfer to next instruction and please check the quotation marks used for printing the strings.
Thank you.

Using break at while loop it will not return to the loop implemented.
At n==b b is 1 so you will get “Bingo” at b=1

import random
n = input("Enter a number between 0-99: ")
n = int(n)
b = random.randint(0,99)
print(b)
if (n==b):
print(“Bingo”)
else:
print(“Better luck next time”)

-----------------------------OR------------------------------------

import random
n = input("Enter a number between 0-99: ")
n = int(n)
if (n==51):
print(“Bingo”)
else:
print(“Better luck next time”)

The Problem is in your loop. There are two scenarios as you have not mentioned the indentation:

1. The break statement is in the if statement:
while(1) is an infinite loop, after executing b=b+1 the value of b becomes 1, as 1 is less than 21
the break statement is executed and the loop terminates at b=1. So the Bingo will be at b=1.

2. The break statement is outside the if statement:
The loop will be infinite and no output will be displayed.

Since you are trying to make a guessing game I suggest using the random module.

instead of the loop, you can put the following lines in your program

import random
b=random.randint(0,21)

the randint function of the random module generates a random number between the range given every time it is executed. syntax = random.randint(,<ending limit +1>)

import random
B=int(input(“Enter No from 0 to 100 :”))
if (B==51):
print(“Bingo!”)
else:
print(“Better Luck Next Time!.”)

import random
num = int(input("Enter a number between 0-99: "))

guess = random.randint(0,99)
print("random integer= ",b)
if (num==guess):
print(“Bingo”)
else:
print(“Better luck next time”)

Hello taleishwari I think if you try using a random function of python for variable b it will be more appropriate in this case so it would be like:
import random
b=random.randint(0,20)
when you use while (1) or while True it is an infinite loop that will not be terminated till the number is reached so the next instruction to be executed will not be executed try using a random function over the while infinite loop

Hello My dear friend Your first code is not working properly…

As far as i know this is the correct code for that challenge…
please have look

import random
number = int(input("Enter a number between 0-99: "))
number=int(num)
guess = random.randint(0,100)
print("random integer= ",guess)
if (number==guess):
print(“Bingo”)
else:
print(“Better luck next time”)

Hey,
A more better approach to the challenge question is

import random
a = input(“Enter any number between 0 to 99:”)
a = int(a)
b = random.randint(0, 99)
b = int(b)
print("The random number is ",b)
if (a == b):
print(“Bingo!”)
else:
print(“Better luck Next time”)

import random
x=int(input(“enter the number between 0-99:”))
x=int(num)
y = random.randint(0,100)
print(“random number=”,y)
if(x==y)
print(“bingo”)
else:
print("Better luck next time:)

Hi ,
In your code,after executing b=b+1,value of b becomes 1,since b<=21 ,the control breaks out of the loop and b remains 1.If you input value of n as 10 ,you will get output “Better luck next time”.
Here the code should be that if your input number matches with your predefined code ,it should print Bingo. The following code will be helpful:
guess=10
n=int(input("Enter any number of your choice between 1 to 20: "))
if (n==guess):
print(“Bingo”)
else:
print(“Better luck next time”)

import random
num=int(input("Enter a number from 0-99: "))
b=random.randint(0,99)
print(b)
if num==b:
print(“BINGO”)
else:
print(“Better luck next time”)

The correct code is as follows:
import random
a = int(input(“Enter any number between 0 to 99:”))
b = random.randint(0, 99)
b = int(b)
print("The random number is ",b)
if (a == b):
print(“Bingo!”)
else:
print(“Better luck Next time”)

I think there is no need of while loop since you having a single input therefore you can check this out

i=82
n=input(“guess a number from 0-99”)
n=int(bingo)
if(n==i):
print(“BINGO!”)
else:
print(“BETTER LUCK NEXT TIME!”)

or

import random
n=random.randint(0,99)
i=input(“guess a number between 0-99 :”)
i=int(i)
if i==77:
print(“BINGO!”)
else:
print(“BETTER LUCK NEXT TIME…”)

Hi! @taleishwari
As you approached this code using infinite while loop, even though you have used break statement with an if condition to exit from the loop it won’t run well to obtain your expected output .Because the loop will get exit at a first iteration ,this make the value b incremented just by 1 but you have mentioned the choices for users input between 1 to 20.So it is highly possible that user may enter other values instead of picking 1. This was the exact case happened while you entered 10 as an input which does not satisfies the condition to display “Bingo”. Your current condition will display “Bingo” only when you give 1 as an input. So it works better when you try to modify the logic.

import random

B=int (input(“Enter No from 0 to 99 :”))

if (B==51):

print(“Bingo!”)

else:

print(“Better Luck Next Time!.”)

after break statement do not use semicolon in your code
the code is as follows:-
b=0
while(1):
b=b+1
if (b<=21):
break
n=int(input("Enter the number of your choice from 1 to 20: "))
if n==b:
print(“Bingo”)
else:
print(“Better luck next time”)

There are two scenarios related to the indentation that may be causing the problem in your loop:

1. If the break statement is inside the if statement, the loop will execute indefinitely until the value of b becomes 1, as 1 is less than 21. Once the break statement is executed, the loop will terminate at b=1, indicating that the Bingo will be at b=1.
2. If the break statement is outside the if statement, the loop will continue to run indefinitely and no output will be displayed.

Since you are attempting to create a guessing game, I recommend using the random module to generate a random number. You can incorporate the following lines into your program instead of using the loop:
import random
b = random.randint(0, 21)

The `randint` function of the `random` module generates a random integer between the specified range every time it is executed. The syntax for using this function is `random.randint(starting limit, ending limit + 1)` .