I have a list of bit. The problem is I need to update the value of the bit according to the new bit that I have. This is the example of my code:
count=1
cycle=3
bit_list = ['1','0','1','0']
new_bit=['1','0','1']
no=''.join(bit_list)
bit=''.join(new_bit)
while (count<=cycle):
for b in no:
print (b)
print ("end of cycle", count)
def bin_add(*args): return bin(sum(int(x, 2) for x in args))[2:]
update=bin_add(no,bit)
count=count+1
print ("updated list",update)
I need the following output:
1
0
1
0
updated list 1011 #1010 + 1
end of cycle 1
1
0
1
1
updated list 1011 #1011 + 0
end of cycle 2
1
0
1
1
updated list 1100 #1011 + 1
end of cycle 3
Please help me to solve this problem. Thank you.
You want the output to be in the variable update and yet your loop keeps using no and bit for operations, so update doesn't evolve after each iteration. You should also add only the bit of the current index to the output. You should also output your "end of cycle" messages at.. the end of the iteration, not in the beginning:
count=1
cycle=3
bit_list = ['1','0','1','0']
new_bit=['1','0','1']
no=''.join(bit_list)
bit=''.join(new_bit)
while (count<=cycle):
def bin_add(*args): return bin(sum(int(x, 2) for x in args))[2:]
no=bin_add(no,bit[count - 1])
for b in no:
print (b)
print ("end of cycle", count)
count=count+1
print ("updated list",no)
This outputs:
1
0
1
1
end of cycle 1
1
0
1
1
end of cycle 2
1
1
0
0
end of cycle 3
updated list 1100
Related
I can understand first output of first example, that is 2 as the i += 1 makes value to 2 than print calls, I also understand the second example's first output that is 1 because print calls i then increment begins. But, what about the end, as we have already defined "while i < 6:" then why first example returns last output as 6 (why don't it breaks to 5)
I'm a beginner so treat me like a kid and write the answer that is easy to understand. Thank you! :)
i = 1
while i < 6:
i += 1
print(i) # Print output after i increment, see the result
>> The output is -
2
3
4
5
6
i = 1
while i < 6:
print(i) # Print output before i increment, see the result
i += 1
>> The output is -
1
2
3
4
5
I was expecting the output should be limited to 5 but it returns to 6 (in first example)
The while condition is only checked before each iteration, not after every statement inside the loop body. So on the last iteration, i == 5 and the condition i < 6 succeeds. It goes into the loop body, increments i to 6, and then prints that value.
i = 1
while i < 6:# initially i value is 1.
i += 1 # here you are incrementing. i value becomes 2.
print(i) # print starts from 2
in the last check i value becomes 5 then it increments to 6 and prints.
so the output starts from 2 and goes till 6.
>> The output is -
2
3
4
5
6
i = 1
while i < 6:
print(i) # Print output before i increment, see the result
i += 1
here you print the i value first and increment later .
you print the i value first
initially 1
then you print it . then you are incrementing it.
in the final check 5<6. it prints 5 and in the next increment as i value equals to 6. the while statement becomes false.
>> The output is -
1
2
3
4
5
for i in range(0,x):
for j in range(0,y):
if (i+j)%2 == 0:
Think of something like tossing two dices at the same time and finding if the sum on the dices is an even number but here's the catch, a dice has 6 sides but here the two can have any number of sizes, equal and not equal even!
Can anyone suggest how to merge it under one loop because I can't think of any?
based on Python combine two for loops, you can merge two for loops in a single line by importing itertools as below:
import itertools
for i, j in itertools.product(range(0,x), range(0,y)):
if (i+j)%2 == 0:
You can't get rid of the nested loop (you could hide it, like by using itertool.product, but it would still be executed somewhere, and the complexity would still be O(x * y)) but you can get rid of the condition, if you only need to generate the values of j that satisfy it, by adapting the range for j.
This way, you'll have about twice as less loops by avoiding the useless ones.
for i in range(0,x):
for j in range(i%2,y, 2):
print(i, j, i+j)
Output:
0 0 0
0 2 2
1 1 2
1 3 4
2 0 2
2 2 4
For me its much cleaner to leave it as two loops. Its much more readable and easier to understand whats happening. However you could essentially do x * y then use divmod to calculate i and j
x = 2
y = 3
for i in range(0,x):
for j in range(0,y):
print(i, j, i+j)
print("###")
for r in range(x*y):
i, j = divmod(r, y)
print(i, j, i + j)
OUTPUT
0 0 0
0 1 1
0 2 2
1 0 1
1 1 2
1 2 3
###
0 0 0
0 1 1
0 2 2
1 0 1
1 1 2
1 2 3
I have a triple for loop that creates a 1 row and 2 column collection of numbers starting at 0 0 and going up to 2 2. The third for loop counts from 0 to 8. The code looks as follows:
for N in range(0,3):
for K in range(0,3):
print(N,K)
for P in range(0,9):
print(P)
If you run this code you get the obvious output:
0 0
0
1
2
3
4
5
6
7
8
0 1
0
1
2
3
4
5
6
7
8
0 2
0
1
2
3
4
5
6
7
8
...
And so on. I want instead of the output of 0 to 8 after the N K printout, instead something that looks like:
0 0
0
0 1
1
0 2
2
1 0
3
1 1
4
1 2
5
2 0
6
2 1
7
2 2
8
My first guess was an if statement that said:
if P == Q:
break
where Q was several sets of sums and even the N,K array. However, I couldn't figure out the best way to get my
wanted output. I do think an if statement is the best way to achieve my wanted result, but I'm not quite sure of how to approach it. P is necessary for the rest of my code as it will be used in some subplots.
As this is just an increment by one at each print, you can just do compute the index with N * 3 + K
for N in range(0, 3):
for K in range(0, 3):
print(N, K)
print(N * 3 + K)
CODE DEMO
You can use zip to traverse two iterables in parallel. In this case, one of the iterables is the result of a nested list. That can be handled by using itertools.product, as follows:
import itertools
for (N, K), P in zip(itertools.product(range(3), range(3)), range(9)):
print(N, K)
print(P)
Please help
Below is the code
start = 3
end = 5
for x in range(start, end + 1):
print x
#and
#print iterate from 0
I am looking here, x will print 3 4 5
and I also need to print 0 1 2 that first time enter to loop print 0 and second time enter to loop print 1 and so on.
Please help
python has enumerate for just this:
start = 3
end = 5
for i, x in enumerate(range(start, end + 1)):
print(i, x)
which prints:
0 3
1 4
2 5
start = 3
end = 5
counter = 0
for x in range(start, end + 1):
print x
#and
#print iterate from 0
print counter
counter += 1
I use the for loop to input the n words
n = int(input(""))
for i in range(n):
a = input("")
print(a)
when I input:
3
1
1
1
2
It allow me to input the n+1 words
And the n+1 word can not be output
I just want to output n words then equal with the syntax in C:
int a = 0;
for(int i=0; i<n; i++)
scanf("%d",&a);
[Update]
Actually it is a problem with Pycharm. And I don't know why.
In terminal,the code can work.
So,plz not downvote....
I don't understand why this isn't working for you. Try this modified version that makes it clearer what is happening:
n = int(input("Enter number of values: "))
for i in range(n):
a = input("Enter value {} ".format(i+1))
print("Value {0} was {1}".format(i+1, a))
The ouput from this was:
Enter number of values: 3
Enter value 1 1
Value 1 was 1
Enter value 2 1
Value 2 was 1
Enter value 3 2
Value 3 was 2
It ran exactly 3 times when I tried it.
If you want to make it more explicit what you're doing you could set it to for i in range(0,n): but that won't really change anything.
The loop for i in range(n): will run from 0 to n-1.
So if you put in 3 it, it will generate 3 runs, with the values of i being 0, 1, 2.
n = int( input( "Enter the number of runs: " ) )
for item in range( 0, n ):
a = input( "\tPlease Input value for index %d: "%item )
print( a )
It generated the output:
Enter the number of runs: 3
Please Input value for index 0: 1
1
Please Input value for index 1: 1
1
Please Input value for index 2: 1
1
I think you are confusing with the output printed by the loop.
If you enter 3 in the first n = int(input(""))" the loop will go from 0 to 2 (inclusive).
In every loop you ask for a new value of a and print it. So, after the first loop, you input 1 and it outputs 1 (because it prints it). In the second loop you input another 1 and it prints it. Finally you input a 2 and it also prints it.
First loop:
input: 1
output: 1
Second loop:
input: 1
output: 1
Third loop:
input: 2
output: 2
That's why if I run the following
>>> n = int(input(""))
3
>>> for i in range(n):
... a = input("")
... print a
...
1
1
2
2
3
3
I get 6 numbers (inputs and outputs). You can see this more clearly with the following example:
>>> n = int(input("Input: "))
Input: 3
>>> for i in range(n):
... a = input("Input: ")
... print "Output: " + str(a)
...
Input: 1
Output: 1
Input: 2
Output: 2
Input: 3
Output: 3