+ 1

How do I add a and i in the loop time?

tem=int(input("How many topics?: " )) for i in range(tem+1): if i!=0: question_name=input("the " + str(i) + " topic name?: ") question_count=int(input("How many questions do you want on the " +str(i) + " topic: ")) for a in range(question_count+1): if a!=0: d=dict() qus=input(str(i) +" Topic " + str(a) +" Question: ") d.update({a:i}) print(d)

22nd May 2019, 6:00 PM
kokito
1 Réponse
+ 1
Hi. I would suggest to move the initialisation of the dictionary out of the for loop. Place d = dict() before for a in .... Btw. you can use for i in range(1, upper + 1): so you can avoid testing against being greater than Zero every time the loop runs. The obvious would be to create two lists and fill one list in each loop. Say the key_list = () in the a loop and the value_list = () in the i loop. You can get the dict from there using d = dict(zip (key_list, value_list)) Cheers.C
22nd May 2019, 7:04 PM
ChrA
ChrA - avatar