Skip to content
Advertisement

Python For Vector

n = int(input("Enter how many numbers will be stored : "))
vet1 = [n]
vet2 = [n]

print("Enter the numbers to be stored")
for i in range(n):
    vet1 = int(input(f"number {i}: "))
for i in range(n):
    if (vet1%2 == 0):
        vet2 = vet1 + 1
        print(f"nThe number entered was {vet1}nYour successor is {vet2}n")
    else:
        vet2 = vet1 - 1
        print(f"nThe number entered was {vet1}nYour predecessor is {vet2}n")

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.

for i in range(n):#Line 5
    

and copy input vet1 = int(input(f"number {i}: "))line and paste in an other loop

n = int(input("Enter how many numbers will be stored : "))
vet1 = [n]
vet2 = [n]

print("Enter the numbers to be stored")

   
for i in range(n):
    vet1 = int(input(f"number {i}: "))
    if (vet1%2 == 0):
        vet2 = vet1 + 1
        print(f"nThe number entered was {vet1}nYour successor is {vet2}n")
    else:
        vet2 = vet1 - 1
        print(f"nThe number entered was {vet1}nYour predecessor is {vet2}n")
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement