Mad Lib program using Python -
automate boring stuff python has project in chapter 8: create mad libs program reads in text files , lets user add own text anywhere word adjective, noun, adverb, or verb appears in text file.
it needs create new text file , print results screen.
my solution causes blank new file created , shows nothing in terminal. using python 3.5.1 on os x.
#! /usr/bin/env python3 open("/users/maverick/madlibproject1.txt", "wt") fout: open("/users/maverick/madlibproject.txt", "r") fin: line in fin: if line == "adjective": adj == input('enter adjective:\n') fout.write(line.replace('adjective', adj)) elif line == "noun": nou == input('enter noun:\n') fout.write(line.replace('noun', nou)) elif line == "adverb": adv = input('enter adverb:\n') fout.write(line.replace('adverb', adv)) elif line == "verb": ver = input('enter verb:\n') fout.write(line.replace('verb', ver)) fname = "/users/maverick/madlibproject1.txt" project = open(fname, 'r') data = project.read() print(data)
take @ code:
data = "a adjective noun verb adverb noun verb noun" part_of_speech in ["adjective", "noun", "adverb", "verb"]: while data.find(part_of_speech) > -1: data = data.replace(part_of_speech, input("enter %s: " % (part_of_speech.lower())), 1) print(data)
some things understand exmaple:
also try adding print(line)
under loop understand program better. others have said, debugging skills 1 of important things you'll learn develop programming.
Comments
Post a Comment