Module 2 challenge

After break keyword don’t use semicolon " ; " in your code
even though you getting the same error then try my code cause i checked my code properly its not giving you an error !
the code is given below -
#try_myCode
b=0
while(1):
b=b+1
if (b<=21):
break
n=int(input("Enter the number between 1 to 20: "))
if n==b:
print(“Bingo”)
else:
print(“Better luck next time”)

a = int(input( "Input A Number Between 0-99 : "))
if (a == 51):
print(“Bingo!”)
else:print(“Better Luck Next Time!”)

Hope You Will Understood

from random import randint
computer_number = randint(1, 99)
user_guess = int(input("Guess the number: "))
if(computer_number == user_guess):
print(“Bingo”)
else:
print(“Better luck next time”)

here is the solution.
If you want this to run for certain number of times, you can put this inside a while loop.

Hi
I think the code is also like this.

n = int(input("Guess a number between (0-99): "))
if n == 51:
print(“Bingo!”)
else:
print(“Better Luck Next Time!”)

Good work) for python developer

Hey,
basically the problem with your code is in the 1st half there u choose b=0
then as while(1) stands true in all case then u uses
b=b+1 as initially b was 0 so now it becomes b=1 then u used condition
if(b<=21): which is true because b=1 then u said break that means the value of b at the end of this loop is b=1
So to get the desired output as “Bingo” u need to put input as ‘1’ in this case u will get this output for any other number u put as input the output will be “Better luck next time”.

try this code:
import random
a=random.randint(0,99) # generates a random num for a
guess=int(input(“enter a num btwn 0 to 99:”)) #gets user input and converts to int
if(a==guess): #checks the cases
print(“bingo”)
else:
print(a)
print(“better luck next time”)

Hey @taleishwari ,

Correct me if I am wrong. I usually am.

b=0
while(1):
  b=b+1
  if(b<=21):
    break;

Per the code snippet above, you’ve assigned a predefined value of 0 to variable b. But afterward, you’re incrementing it by 1 inside the while loop until it reaches 21.

I failed to understand the reason why you did the above.

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

Per the code snippet above, you’ve asked the user for input. Then you’re comparing it with the predefined value and then printing out the relevant output. This backend part of this snippet is correct, but you’ve asked the user wrong question.

In my point of view, your second code snippet where you’re asking the user for input and rest is correct except for the question.

Also, your first code snippet could be improved just by assigning 21 directly to the variable b, instead of letting it run through a loop until it reaches 21.

b = 21

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

But there is still a flaw left in your code. You’ve not checked the prerequisite of the user input to be within the range of 0-99.

Let me know if you want me to correct your code further or if I should share you the code I made from my side.

Hi @taleishwari , Solution for Module 2 challenge involves:

  1. Running infinite loop.
  2. Prompting user to give input for a random number between 0 - 99.
  3. Checking the number, if it is equal to 51 or not.
  4. Printing “BINGO!..” if it is equal to 51, else “Better Luck Next Time!..” asking number for another time.

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

Dear, you have b = 1 as per the condition of your while loop. Use more optimistic way i.e. ‘random function’ for generating variable b.
Edited code:

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

My code ensured that user types in a valid number that is (0-99).I will share my code here
guess = input("enter your value in the range of 0 to 99: ")
bingo=51
guess =int(guess)
if (guess>99):
print (“invalid input”)
elif(guess<0):
print (“invalid input”)
else:
if(guess==bingo):
print(“Bingo!”)
else:
print(“Better luck next time”)

when you run your code your first loop(while()) is exited with b= 21and not before. If you desire BINGO! then you should enter number 21. But your input range specified is (1,20). So either fix value ‘b’ in range of 1 to 20 or use random.randint(1,20) to get a random ‘b’ . The code after the while loop is fine. Hopefully this is helpful.

try this code

Using loop,if you need an output bingo for number 10 try this code.
b=0
while(1):
b=b+10
if(b<=21):
(b)
break;

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

Since you used b=b+1 then it shows “Bingo” for number 1. If you need it for 10 then use b=b+10

try this:

import random
guess=int(input("enter your guess number between 0 and 10 : "))
luckyNumber=random.randint(0,10)
if(guess==luckyNumber):
    print('!!BINGO!!')
else:
    print('Lucky number is ',luckyNumber)
    print('Better Luck next time')
    

each time we run the program the program itself generates a random number as lucky number

It can be answered in this simple way without any complications:

the program is

n=input(“enter a number between 0-99”)
n=int(n)
if (n==51):
—print(“Bingo!!”)
else:
—print(“Better Luck Next Time!”)

Here we are just checking whether the user input is 51 or not .
I hope it helps

1 Like

**


**

Hey @taleishwari
The Problem in your loop is as you have not mentioned the indentation: 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.
So code like this by using a random function of python for variable b it will be more appropriate in this case :

1 Like

hey @taleishwari so the problem is mainly in your fourth line of code , since you have used b<=21 , it will satisfy at any value of b less than 21 and break the code , so it is breaking the code at b = 1 and setting the value of b at 1 which means u will get ‘Better luck next time’ at any other value , the best way to set a random value of b is by importing random library and using b = random.randint(0,20) this will make it far better code.