How Do You Write a Python Program to Create a .txt File with 50 Key-Value Pairs?

Problem scenario
You want to create a .txt file that has 50 key-value pairs. You want the keys to be whole numbers. You want the values to be five-character strings. What should you do?

Solution

import random
import string
import sys

def randomword(length):
   letters = string.ascii_lowercase
   return ''.join(random.choice(letters) for i in range(length))

def printer(counter, webstera):
  webstera[counter] = randomword(5)
  if (counter < 50):
    counter = counter + 1
    return printer(counter, webstera )
  else:
    print("____________________")
    return webstera

a = printer(0, {})
print(a)

file = open("contint.txt","w")
file.write(str(a))
file.close()

Leave a comment

Your email address will not be published. Required fields are marked *