Home > Back-end >  Python For Vector
Python For Vector

Time:08-27

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.

CodePudding user response:

As the above comments said you have to append the value in order to store the values.

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

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

CodePudding user response:

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")
  • Related