how to loop using while and for
from random import random
a = int(input("MASUKAN ANGKA :"))
for i in range(a):
bil = random()
print("Perulangan ke- :", bil)
Example run:
MASUKAN ANGKA :5
Perulangan ke- : 0.0813806069084485
Perulangan ke- : 0.5072770244491718
Perulangan ke- : 0.8493965282202113
Perulangan ke- : 0.43884806653052943
Perulangan ke- : 0.7745391208748537
CodePudding user response:
It's not clear what you want to do here, if not with a combination of for and while loops.
I can only assume that you want a while loop version of your code, which goes like this:
from random import random
a = int(input("MASUKAN ANGKA :"))
while (a > 0):
bil = random()
print("Perulangan ke- :", bil)
a -= 1
All you have to do is decrement the input with each loop until it equals 0.
CodePudding user response:
If you want to do that in while loop, this is the answer.
from random import random
a = int(input("MASUKAN ANGKA :"))
while (a > 0):# runs a times
bil = random() # gives the random number
print("Perulangan ke- :", bil) #
a -= 1
CodePudding user response:
If you want to combine both loops, you can use the for loop inside an infinite loop:
from random import random
a = int(input("MASUKAN ANGKA :"))
while (True):
for i in range(a):
bil = random()
print("Perulangan ke- :", bil)
break