The python code for the above micro bit program.
inner loop:
In the above microbit code, we can find a simple pattern. #led 1, is run for 15 times, and each time, the sequence of bits resemble the binary values of numbers from 1 to 15.
outer loop:
The above inner loop is executed 4 times. This is the number of occurrences of pin 6, 7, 8, 9. The parameters write_digital() of these pins also follow a pattern. This pattern is the multiplication table of 2. This repeats until value 8 is reached.
Below is the python code, with necessary comments
code:
form microbit import *
microbit.display.off()
# storing the pins into the arrays
pins = [microbit.pin0, microbit.pin1, microbit.pin2, microbit.pin3]
otherPins = [microbit.pin6, microbit.pin7, microbit.pin8, microbit.pin9]
while True:
num = 1
while num <= 8 : # outer loop for pins 6, 7, 8, 9
binVal = bin(num)[2:].zfill(4) # outputs binary value of num, with length of 4 bits
num = num * 2
for t in range(0,4): # looping over the 4 bits, for 4 different pins
if binVal[t] == 1:
otherPins[t].write_digital(1)
else :
otherPins[t].write_digital(0)
# led 1
for i in range(0,16): # looping over 15 times for led 1
binaryValue = bin(i)[2:].zfill(4) # calculating binary value of length 4 bits
for j in range(3, -1, -1): # looping over 4 binary bits in reverse order
if binaryValue[j] == 1 :
pins[j].write_digital(1)
else :
pins[j].write_digital(0)
sleep(200)
How can i simplify this micro bit program using for loop in python? from microbit import * display.off() while True:...
How do I write this code from while loop to for loop? (this is the whole code) NOTE: do not add any import other than: import java.util.ArrayList; import java.util.Collection;(collection not collections) import java.util.List; import java.util.Set; import java.util.TreeSet; import java.util.HashSet; code: public static int frequency(Collection values, int target) { var iter = values.iterator(); int app = 0; while (iter.hasNext()) { if(iter.next() == target) { app++; } } return app; } public static boolean isSorted(List list) { var iter = list.listIterator(); int...
Design a program using Python and using from flask Import flask that generates a lottery number but in a website.. The program should have an Integer array with 9 elements. Write a loop that steps through the array, randomly generating a number in the range of 0 through 42 for each element. (Use the random function) Then write another loop that displays the contents of the array. Each number should be displayed as a list, the numbers should be generated...
How do I make this program run the same using a do while loop instead of the for loop? I'm supposed to replace the for loop with a do-while. Also, how would i replace it with a while loop? #include void main(void) { int n, sum, product, i; printf("n?\n"); scanf("%i", &n); printf("You entered %i\n", n); sum = 0; product = 1; for (i=1; i<=n;i++) { if((i%3!=0) && (i%5!=0)){ sum = sum + i; product = product * i;} ...
Need in Python! Consider the following program and expected output. Rewrite it using a for loop and a conditional break statement such that it has the same output. HINT: Stay with i as the main loop counter and let j control the break behavior. i = 0 while i < 10 and j < 10 : j = i * 2 print(i, j) i += 1 # Output below, for reference - not part of the program! 0 0 1...
Need in Python! Consider the following program and expected output. Rewrite it using a for loop and a conditional break statement such that it has the same output. HINT: Stay with i as the main loop counter and let j control the break behavior. i = 0 while i < 10 and j < 10 : j = i * 2 print(i, j) i += 1 # Output below, for reference - not part of the program! 0 0 1...
Create a python program to play rock paper scissors using a while loop and if statements. I have started but have gotten stuck in a continuous loop. Please look over my code and help me find where I went wrong. Here it is: import random #Choice weapons=['Rock' ,'Paper' ,'Scissors'] print('Rock, Paper, Scissors!') print('Rock, Paper, Scissors!') print('Shoot!') human=input('Choose Rock, Paper, Scissors, or Quit! ') print('')#Blank Line while human != 'Quit': human_choice=human computer=random.choice(weapons) print(computer) if human==computer: print("It's a tie!") elif human=='Rock': if...
[Using Python] Create a program that uses import random and while loops to generate one random question, from a list of 5, for each repeated run. Report the number of the correct answers after all five questions have been answered. Your 5 questions should be a simple math problem, like "what is 7-5?"
PYTHON CODE: Write a program that uses a while loop to examine every integer from 999 down to zero and generate the exact same output as shown blow. The range function is not required. NOTE: you must use a while loop. Required Output 960 920 880 840 800 760 720 680 640 600 560 520 480 440 400 360 320 280 240 200 160 120 80 40
NOTE: Write the Program in python as mentioned below and use while loop and flags as necessary. And please write the code following the program description given below. DON'T use Function concept or any other concept except while loop,if-else. Directions: Read the program description below carefully. All requirements must be met by your program to receive full credit. Thus, pay particular attention to input and output requirements, structural requirements, and so on. Furthermore, code that contains syntax errors will not...
Using python, Write a while loop that counts from 1 to 20 by 4s.