Python Forum
Could Someone help me with List problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Could Someone help me with List problem
#1
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???

Attached Files

.py   _1.py (Size: 296 bytes / Downloads: 232)
Reply
#2
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']
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with "Number List" problem on HackerRank Pnerd 5 2,113 Apr-12-2022, 12:25 AM
Last Post: Pnerd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020