0

I'm trying to make a Madlibs-type thing in python, as I'm new to coding and wanted a sort of simple project to work on for a first thing to make. I'm trying to make it so that the verbs/nouns/adjective variables appear in the string, but they, instead of being called, are just right in the code. This is the first actual program that I've written, so this is probably a super easy fix, but I have no idea how to make this work.

print("Welcome To Madlibs")

choosepage = input("Choose Page, 1, 2 or 3. :\n")
choosepage = int(choosepage)

if choosepage == 1:
    print("Welcome To Madlibs, Page 1")
    adjective1 = input("Give One Adjective: ")
    noun1 = input("Give One Noun: ")
    noun2 = input("Give Another Noun: ")
    verb1 = input("Give A Verb: ")
    adjective2 = input("Give Another Adjective: ")
    print("One Day, there was a "%adjective1" woman. This woman, was on a walk one day, when she saw a " %noun1 " She looked at the " %noun1 " and thought to herself, " %noun1 " looks like a " %noun2 "."

elif choosepage == 2:
    print("Welcome To Madlibs, Page 2")

(The Code in Question)

The reason I'm putting % signs in front of the variables is because I saw that as a fix somewhere online.

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

0

You're looking for string formatting, which can use the .format() method or can be an "f-string" .. many helpful options exist for formatting numbers (such as padding)

>>> word = "airport"
>>> "they went to the {}".format(word)
'they went to the airport'
>>> f"they went to the {word}"
'they went to the airport'
ti7
  • 16,375
  • 6
  • 40
  • 68
0

Use f-strings:

print(f"One Day, there was a {adjective1} woman. This woman, was on a walk one day, when she saw a {noun1}. She looked at the {noun1} and thought to herself, {noun1} looks like a {noun2}."
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

Python 3.6 introduced f-strings, which makes it super easy to use variables in-line in a string. All you have to do is add the 'f' flag prior to your opening quote. They work as follows:

print(f"One Day, there was a {adjective1} woman. This woman, was on a walk one day, when she saw a {noun1} She looked at the {noun1} and thought to herself, {noun1} looks like a {noun2}.")

These have the advantage of being much clearer and easier to format than traditional string formatting in cases like this.

whege
  • 1,391
  • 1
  • 5
  • 13