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.
@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
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.
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
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
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
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