JavaScript
x
15
15
1
n = int(input("Enter how many numbers will be stored : "))
2
vet1 = [n]
3
vet2 = [n]
4
5
print("Enter the numbers to be stored")
6
for i in range(n):
7
vet1 = int(input(f"number {i}: "))
8
for i in range(n):
9
if (vet1%2 == 0):
10
vet2 = vet1 + 1
11
print(f"nThe number entered was {vet1}nYour successor is {vet2}n")
12
else:
13
vet2 = vet1 - 1
14
print(f"nThe number entered was {vet1}nYour predecessor is {vet2}n")
15
I want to achieve this result
The number entered was 1 Its predecessor is 0
The number entered was 2 His successor is 3
The number entered was 3 Its predecessor is 2
but I can’t store the numbers in the vector Please, help.
Advertisement
Answer
This code simply achieved the desired output. The mistake is the first loop which can be a hurdle, basically you don’t need another loop with same length. Simply remove this loop.
JavaScript
1
3
1
for i in range(n):#Line 5
2
3
and copy input vet1 = int(input(f"number {i}: "))
line and paste in an other loop
JavaScript
1
16
16
1
n = int(input("Enter how many numbers will be stored : "))
2
vet1 = [n]
3
vet2 = [n]
4
5
print("Enter the numbers to be stored")
6
7
8
for i in range(n):
9
vet1 = int(input(f"number {i}: "))
10
if (vet1%2 == 0):
11
vet2 = vet1 + 1
12
print(f"nThe number entered was {vet1}nYour successor is {vet2}n")
13
else:
14
vet2 = vet1 - 1
15
print(f"nThe number entered was {vet1}nYour predecessor is {vet2}n")
16