lista = []
for i in range(5):
i = int(input("Digite um valor para o vetor: "))
lista = lista + [i]
x = int(input("Digite um valor para ver sua posição: "))
counter = 0
for j in range(5):
if lista[j] == x:
counter =+ 1
print(j)
if counter == 0:
print(x-1)
In the above program you put any 5 numbers in the list, then you look for the position of the number you inputted in the list, if the number inputted is not in the list it will print x-1.
For example List = [1, 2, 3, 4, 5]
x = 5 then it will print 5
x = 7 it will print 6
How do I make it print x-1 without counter? I tried using:
else:
print(x-1)
but then it will print x-1 5 times, I only want to print it once.
You don't need a counter at all, as you're only using it to check that there were no matches. You can use the for..else structure to check whether the loop completed without exiting from a break.
for j in range(5):
if lista[j] == x:
print(j)
break
else:
print(x-1)
If you want to print each index that matches your search query, I would move to a different approach:
>>> x = 1
>>> print(*(i for i,v in enumerate(map(int, input('Enter values separated by space:\n').split())) if v==x))
Enter values separated by space:
1 1 1 1 1
0 1 2 3 4
>>> print(*(i for i,v in enumerate(map(int, input('Enter values separated by space:\n').split())) if v==x))
Enter values separated by space:
2 2 1 10 1
2 4
>>> print(*(i for i,v in enumerate(map(int, input('Enter values separated by space:\n').split())) if v==x))
Enter values separated by space:
2 2 1 10
2
Related
This is my code for getting a 3 digit combination.
a = input("Enter first number: ")
b = input("Enter second number: ")
c = input("Enter third number: ")
d = []
d.append(a)
d.append(b)
d.append(c)
for i in range(0, 4):
for j in range(0, 4):
for k in range(0, 4):
if(i !=j & j!=k & k!=i):
for count, i in enumerate(range(4),1):
print(i,j,k)
Input:
1,2,3
and
Output:
0 1 2
1 1 2
2 1 2
3 1 2
0 2 0
1 2 0
2 2 0
3 2 0 & more....
My question is how can I get the count of how many combinations I have got?
Thank you so much for your attention and participation.
Outside of your loop, create an integer variable and set it equal to 0. Where you print out the combinations (within the loop), add 1 to this integer variable. Finally at the end of your program outside the loop, print the value of the counter variable.
For example:
counter = 0
for i in range(0, 4):
for j in range(0, 4):
for k in range(0, 4):
if (i != j & j != k & k != i):
for count, i in enumerate(range(4), 1):
print(i, j, k)
counter += 1
print(counter)
Im trying to print the maximum number of consecutive 1's in python...but im getting stuck here....IDK why im getting a syntax error...strange...can anyone help me out
li2 = []
t = int(input())
for i in range(0, t): //testcases
n = int(input())
for i in range(n): //length of list(binary array)
li = list(map(int, input().strip().split())
count = 0
max_count=0
for i in range(len(li)):
if (li[i] == 0):
count = 0
else:
count += 1
max_count = max(max_count,count)
li2.append(max_count)
for i in range(len(li2)):
print(li2[i])
File "<ipython-input-2-f159cb61e247>", line 7
count = 0
^
SyntaxError: invalid syntax
Corrections:
li2 = []
t = int(input())
for i in range(0, t): #testcases
n = int(input())
li = list(map(int, input().strip().split())) #<--- before that loop is removed.
count = 0
max_count=0
for i in range(len(li)):
if (li[i] != 1): #<------- Here
count = 0
else:
count += 1
max_count = max(max_count,count) #<--- here
li2.append(max_count)
print()
for i in range(len(li2)):
print(li2[i])
3
5
2 1 1 1 1
3
2 3 4
7
1 2 3 1 1 1 1
4
0
4
Improve the above answer using inline function
li2 = []
t = int(input())
for i in range(0, t): #testcases
n = int(input())
li = ''.join(input().split())
li = [n if n == '1' else '0' for n in li] # replace the numbers not '1' to '0'
max_count = max(map(len, ''.join(li).split('0'))) # split by '0' and get max length from each 1's
li2.append(max_count)
print()
for i in range(len(li2)):
print(li2[i])
Basically, I need a code that takes an integer and then prints out the string of numbers with the certain range.
For example:
n = 11
1 2 2 3 3 3 4 4 4 4
n = 7
1 2 2 3 3 3 4
a = []
n = int(input())
if n == 0:
print(n)
for i in range(int(n) + 1):
a += [i] * i
a = ' '.join(map(str, a))
print(a[:n])
This does the job but it counts spaces as characters, so I tried to convert it to an int
n = int(n)
print(' '.join(a[:n]))
But when the n >= 47, it starts to print out 10 as 1 0 which is incorrect
I also tried this code
n = int(input())
for i in range(n):
b = (str(i) * i)
print(b, end = ' ')
But I don't understand how to separate the b with spaces without converting the string to a list and printing it in one line either.
I am not sure if it is even possible.
Maybe something like this?
# initialize variables:
number = 11
counter = 0
for i in range(number):
for j in range(i):
# print number:
print('%d ' %i, end='')
# increment counter (tracks how many numbres to print):
counter += 1
# exit inner loop if counter reaches number:
if counter >= number-1: break
# exit outer loop if counter reaches number:
if counter >= number-1: break
Output:
1 2 2 3 3 3 4 4 4 4
Here's a solution using itertools. Generators, chain, repeat and islice are all lazily evaluated, so this uses O(1) space.
>>> n = 7
>>> from itertools import chain, repeat, islice
>>> gen = chain.from_iterable(repeat(i, i) for i in range(1, n+1))
>>> for i in islice(gen, n):
... print(i, end=' ')
...
1 2 2 3 3 3 4
This seems simple. But this does solve the problem?
>>> for i in t:
... if i!=" ": print i
...
1
2
2
3
3
3
4
4
4
4
or even string replace like,
print t.replace(" ","\n")
1
2
2
3
3
3
4
4
4
4
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