Python Forum
Could Someone help me with List problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Could Someone help me with List problem (/thread-34137.html)



Could Someone help me with List problem - alexsendlegames100 - Jun-30-2021

import math

A = list(map(int, input()))

B = list(map(int, input()))

#C = list(map(int, input()))

if A,B < len(3):
   print(D_1)
else:
    print(D_2)


D_1 = math.sqrt( (B[0]-A[0])**2 + (B[1]-A[1])**2 + (B[2]-A[2])**2 )

D_2 = math.sqrt( (B[0]-A[0])**2 + (B[1]-A[1])**2 )
i tried to create a code that will calculate the distance between two points and it succeed but when i try to do the conditional part to make two option if the vector has two values x and y it will print D_2 and if it has x y z it will print D_1 but it shows a lot of mistakes i dont know how to state the a condition which will check the amount of values in the lists and print one of two solution according the amount of values there 3 or less???


RE: Could Someone help me with List problem - menator01 - Jun-30-2021

One way to check the length of a list
#! /usr/bin/env python3

print('Enter two or three numbers seperated by a space')
while True:

    A = input('>> ').split()

    if len(A) == 3:
        print(f'A has {len(A)} elements')
        print(A)
        break;
    elif len(A) == 2:
        print(f'A has {len(A)} elements')
        print(A)
        break
    else:
        print('Please enter either 2 or three numbers seperated by a space')
        continue
Output:
Enter two or three numbers seperated by a space >> 5 Please enter either two or three numbers seperated by a space >> 6 2 A has 2 elements ['6', '2']