pytm
pytm

Reputation: 1

Print list1 with list2 python

I dont know how to search this code in internet so I ask here My code :

# This code is in Tes.py
n = [str]*3 
x = [int]*3
MyLib.name(n)
MyLib.number(x)
MyLib.Result(n,x)

# This code in MyLib.py with 3 def
def name(data) :
    for i in range (3) :
        n[i] = str(input("Enter Name : ")

def number(data) :
    for s in range (3) :
        x[i] = int(input("Enter Number : ")

def result(data1,data2) :
    for i in data1 :
        for i in data2 :
            print("Your Name",n,"Your Number",x)

examples :
input 1 : Jack
          Rino
          Gust
input 2 : 1232
          1541
          2021
output what I want : Your Name Jack Your Number 1232
                     Your Name Rino Your Number 1541
                     Your Name Gust Your Number 2021

output that i got :  Your Name Jack Your Number 1232
                     Your Name Jack Your Number 1541
                     Your Name Jack Your Number 2021
                     Your Name Rino Your Number 1232
                     Your Name Rino Your Number 1541
                     Your Name Rino Your Number 2021
                     Your Name Gust Your Number 1232
                     Your Name Gust Your Number 1541
                     Your Name Gust Your Number 2021

How to get output like what I want, I want to search it in the google but I dont know what I must type.

Upvotes: 0

Views: 304

Answers (2)

Mick
Mick

Reputation: 796

Is this what you mean?

for i in range(min(len(n), len(x))):
    print("Your Name",n[i],"Your Number",x[i])

Upvotes: 1

Md Sajid
Md Sajid

Reputation: 131

total = 3
n = [str]*total
x = [int]*total
for i in range (total):
    n[i] = str(input("Enter Name : "))

for i in range (total):
    x[i] = int(input("Enter Number : "))

for i in range (total):
        print("Your Name",n[i],"Your Number",x[i])

If you give this code the input you mentioned, it will show the desired result. As you wrote two loops,

 for i in n :
    for i in x :

your print will be triggered 3*3 = 9 times! You just need one single loop, since at the both input you are taking same number of inputs (3 names and 3 numbers!). Even I would say why do you need 3 loops? why not just this:

total = 3
n = [str]*total
x = [int]*total
for i in range (total):
    n[i] = str(input("Enter Name : "))
    x[i] = int(input("Enter Number : "))
    print("Your Name",n[i],"Your Number",x[i])

    

Upvotes: 0

Related Questions