Home > Software design >  how data array input
how data array input

Time:01-09

pyton 3.x i'm trying to do append,pop,insert in this ,in insert you need 2 integer to make it work so it need 3 input(1.tambah/buang/sisip | 2.first string(for append)/integer | 3.second string/integer(for insert)

def isNumber(s):
    for i in range(len(s)):
        if s[i].isdigit() != True:
            return False

    return True


deret = [1,3,5]
#driver code
if __name__ == "__main__":
    
    j,a,b =(input("H :")).split()
    if isNumber(a):
        a = int(a)
        b = int(b)
    else:
        a = a

so if i want to do the append one,i only need 2 input (j and a) so i will not insert the (b) one,but it will become an error.and if i want to do the insert one i need 3 input(j,a,b) j,a,b =(input("H :")).split(),but if i want to do the append or pop one it will become an error because it need 3 value,how i can fix this?

try:
    if j == "tambah":
        deret.append(a)
        print(deret)
    elif j == "buang":
        deret.pop(a)
        print(deret)
    elif j == "sisip":
        deret.insert(a,b)
        print(deret)
    else:
        print("Perintah tidak dapat dieksekusi!")
except IndexError:
    print("Perintah tidak dapat dieksekusi!")

any help and solution for this situation? if you don't mind could you give the full fix code pls?

CodePudding user response:

I believe that the problem you have is that you're expecting to receive 3 inputs every time although that you may receive 2, or 3 inputs depending on your choice ("tambah" / "buang" / "sisip").

so I think you firstly need to receive the input as follows:

input_line = input("H :")
command = input_line.split()[0]  # to get the command
try:
    if command == "tambah":
        a = input_line.split()[1]
        if isNumber(a):
            deret.append(int(a))
            print(deret)
        else:
            print("Only accepts numbers")
    elif command == "buang":
        a = input_line.split()[1]
        if isNumber(a):
            deret.pop(int(a))
            print(deret)
        else:
            print("Only accepts numbers")
    elif command == "sisip":
        a = input_line.split()[1]
        b = input_line.split()[2]
        if isNumber(a) and isNumber(b):
            deret.insert(int(a), int(b))
            print(deret)
        else:
            print("Only accepts numbers")
    else:
        print("Perintah tidak dapat dieksekusi!")
except IndexError:
    print("Perintah tidak dapat dieksekusi!")

CodePudding user response:

According to what I get from your question is, you want to take two inputs from the user in case you want to append or pop an element from the array, and three inputs in case you want to insert an element. For this I have done:

deret = [1,3,5]
l = list(input().split())
if len(l) == 2:
    j = l[0]
    a = int(l[1])
elif len(l) == 3:
    j = l[0]
    a = int(l[1])
    b = int(l[2])

appending an element is easy, as whatever element you give as a parameter gets added to the list in the end. But in the case of pop() the parameter you give is not an element, it is the index who's element you want out. It can be left empty. (To deal with this, in that case except will run!)

try:
if j == 'tambah':
    deret.append(a)
elif j == 'buang':
    deret.pop(a)
elif j == 'sisip':
    deret.insert(a,b)
else:
    print("Perintah tidak dapat dieksekusi!")
print(deret)
except IndexError:
    print("Perintah tidak dapat dieksekusi!")

Hope, this worked for you!

  •  Tags:  
  • Related