Using While condition with Random functions

Hello,

I am trying to use random functio which is mentioned in IoT/ML training of Bolt.

Following is my code:

max=10

min=1

count==0

while count<2:

import random

luckyNumber=random.randint(min,max)

print(luckyNumber)

count=count+1

i am not able to get the desired output which is printing the luckyNumber twice by random. Please let me know how to use while loop in random function.

Hello,
For a loop you need to use indentation to write the body of the loop i.e.

while (condition):
<use multiple spaces or “tab”> (body of the loop)

I hope this helps.
Regards

Hi @himanshumanghani95,

Check the code below -

import random

max=10

min=1

count=0

while count<2:
	luckyNumber=random.randint(min,max)
	print(luckyNumber)
	count=count+1

and the output -

50%20PM

Do let me know in case you need further assitance.

1 Like

@rahul.singh1Yeah It worked.My mistake was count==0. Writing count=0 yields the result ! Hmmm…what is the difference between count==0 and count=o

1 Like

@tunirkv19…I used proper indentation. However in the forum indentation has not mentioned. Neverthless count==0 will not give answer , whereas count=0 will get us the desired result! Thank you

1 Like

Hi @himanshumanghani95,

= is assignment operator it is used to asign values to variables but == is used in comparison.
For example : if 1==1:

2 Likes

@rahul.singh1yes…Thank you ! I studied in college and forgot now ! Bolt forum is awesome…thanks again

1 Like

There was indentation error in the while loop. The indentation error can occur **when the spaces or tabs are not placed properly and u have to import random before the code starts.

it is clearly a indentation error .
while(condition):

answer:

max=10
min=1
count=0
while (count<2):
import random
luckyNumber=random.randint(min,max)
print(luckyNumber)
count=count+1

reason: You mistaken at the count==0, beacuse you did not assign the values outside the conditions. And also after conditions you have take the values inside the brackets. And after conditions you have the tab space for concadination

In your program you have not defined count properly
Use
count = 0
Also use proper indentation

Also I believe it is not possible to print the same number in succession using random function.

i think you are not indented that random function statement thus it is not considered as a statement inside the loop. All the statements that you want to put inside the while loop or any loop you have to put 4 spacec or tab before them

We have to use "count = 0 " instead of "count == 0 ". Hope it helps.

Thank you.

There is a problem with your value assignment operator ‘==’ for count instead it should be ‘=’. Further the indentation is incorrect for the code entered after the while loop and hence you should correct your indentation to represent it as a block of code.

there is a problem in your lines of code in line3
you have used ‘==’ (denoting comparision) instead of ‘=’(for initializing the value)
and there is indentation error as well

try running this code:

max=10
min=1
count=0
while count<2:
import random
luckyNumber=random.randint(min,max)
print(luckyNumber)
count=count+1

Hello Himanshu,
To solve your doubt we shall correct the code you are using.

To correct it lets first add the indents after the while loop
then lets add the import function to the top of the program for neatness
and lastly lets change the relational operator ‘==’ to the relational operator ‘=’
this shall be the code;

import random
max=10
min=1
count=0

while count<2:
  luckyNumber=random.randint(min,max)
  print(luckyNumber)
  count+=1

But it still outputs 2 numbers since python iterates through a range starting from 0 (common across many languages)
so to change it like how you needed lets do make the iteration to stop at 1 instead of 2

import random
max=10
min=1
count=0

while count<2:
  luckyNumber=random.randint(min,max)
  print(luckyNumber)
  count+=1

This should fix your code

But if you are expecting only 1 result, then a while loop is unneccesary

import random
max=10
min=1
print(random.randint(min,max))  

‘=’ operator is used for assigning values whereas ‘==’ is used for comparison between two values. In your code you are comparing the value of count to 0. You should rather use ‘=’ to assign the value 0 to variable count

You Give count=0 and after while loop you must click tab and then run the code:
image

In this program it shows the error because it not able to proceed ==.
where it can run and procced with count=0 and after while next code line should need some space so that program will be executed.

hello sir, i think the while loop used is unnecessary since required single output
also min and max variable can be optionable because random library is inbuilt within the range can be specified therefore you can make your code shorter .however you can check this out

import random
count=0
luckyNumber=random.randint(0,10)
print(luckyNumber)
count=count+1

hope this helps you