Python Forum
[MicroPython] Beginner, program with lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[MicroPython] Beginner, program with lists
#3
Ok ! But what about the rest ?

for i in range(1,n+1):
 r=randint(1,6)+randint(1,6)
 if r==2:
  a=a+1
 if r==3:
  b=b+1
 if r==4:
  c=c+1
 if r==5:
  d=d+1
 if r==6:
  e=e+1
 if r==7:
  f=f+1
 if r==8:
  g=g+1
 if r==9:
  h=h+1
 if r==10:
  j=j+1
 if r==11:
  k=k+1
 if r==12:
  l=l+1
print("2:"+str(a))
print("3:"+str(b))
print("4:"+str(c))
print("5:"+str(d))
print("6:"+str(e))
print("7:"+str(f))
print("8:"+str(g))
print("9:"+str(h))
print("10:"+str(j))
print("11:"+str(k))
print("12:"+str(l))
How can I reduce that :/

Ok, so I managed to do that, is it ok or can I reduce/optimise it more ?

from random import *
from math import *
n=int(input("n="))
sommes = [0]*11
for i in range(1,n+1):
 r=randint(1,6)+randint(1,6)
 if r==2:
  sommes[0] += 1
 if r==3:
  sommes[1] += 1
 if r==4:
  sommes[2] += 1
 if r==5:
  sommes[3] += 1
 if r==6:
  sommes[4] += 1
 if r==7:
  sommes[5] += 1
 if r==8:
  sommes[6] += 1
 if r==9:
  sommes[7] += 1
 if r==10:
  sommes[8] += 1
 if r==11:
  sommes[9] += 1
 if r==12:
  sommes[10] += 1
print(sommes)
Reply


Messages In This Thread
RE: [MicroPython] Beginner, program with lists - by Hellon - Nov-09-2019, 10:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  MicroPython ESP32 color changing from temps Sim00778 2 3,379 Aug-05-2022, 05:02 PM
Last Post: deanhystad
  working with Micropython - which ecosystem to choose - ESP 8266 or ESP 32? apollo 1 2,611 Aug-11-2019, 12:05 PM
Last Post: apollo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020