0

Mad Libs are activities that have a person provide various words, which are then used to complete a short story in unexpected (and hopefully funny) ways.

Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is quit.

Ex: If the input is:

apples 5
shoes 2
quit 0

the output is:

Eating 5 apples a day keeps the doctor away.
Eating 2 shoes a day keeps the doctor away.

Note: This is a lab from a previous chapter that now requires the use of a loop.

my code:

user_text = input().split()
word = (user_text[0])
number = (user_text[1])

if word != 'quit':
    print('Eating {} {} a day keeps the doctor away.'.format(number, word))
    user_text= input()

outputs: Eating 5 apples a day keeps the doctor away.

1
  • 1
    Hi there. Can you specify what you want? It seem like you are copy paste a question from textbook here? Commented Jan 31, 2021 at 22:29

5 Answers 5

1

You are not looping, so your program will only ask for input once, and end. If you want to keep inputting data, and printing new words, you should do the following:

word = ""

while True:
    user_text = input().split()
    word = (user_text[0])
    if word == 'quit':
        break
        
    number = (user_text[1])
    print('Eating {} {} a day keeps the doctor away.'.format(number, word))

As you can see by the code, while the user doesn't input the word quit, the program will ask for a new input, and print a new sentence.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help. When I try to use the code, it is still outputing all of the inputs even if the word quit is inputted.
I see what you mean. Check my updated answer, it should have solved your problem. If it's what you needed, mark it as correct! @DanielGardner
0

Here is another very similar approach to this lab. Some school professors disapprove of using the while True: approach due to infinite loops, so here is a way around it.

user_input = input().split()
word = user_input[0]
number = user_input[1]

while (word != "quit"):
    print("Eating {0} {1} a day keeps the doctor away.".format(number, word))
    user_input = input().split()
    word = user_input[0]
    number = user_input[1]

Comments

0

The concept John B. used earlier in the thread gave me some inspiration and my code ended up somewhat similar. This code will only work for the lab or code with a similar concept if quit is never inputted there will be an error.

word_num = input().split( ) #first get the input and split it into 2 
word = word_num[0] # the word always comes first according to the lab
num = word_num[1] # then number

while word != "quit": # to get full marks do not loop if input is quit
    print("Eating", (num), (word), "a day keeps you happy and healthy.") # didn't use f'string contrary to other threads
    word_num = input().split( ) # this step ensures the program is looping
    word = word_num[0] #gets word again and will loop until word == "quit"
    num = word_num[1] # gets num and loops

Comments

0

I've figured out the code, for it to loop the question and end the program if the input is 'quit 0' write the following:

while True:
    user_input = input().split()
    if user_input[0] == "quit":
        break

    word = user_input[0]
    number = int(user_input[1])

    sentence = f"{number} {word}"

    print(f"Eating {sentence} a day keeps the doctor away.")

Comments

-2

(REVISED) Here is an answer that is more similar to your code setup:

user_input = input()
splitstring = user_input.split()
while splitstring[0] != 'quit': # this will initiate the loop
    if splitstring == 'quit': # this will immediately quit the loop upon discovery of 'quit' in splitstring list position 0
        break # breaks the loop to quit
    else: # runs the loops until all input has been looped that does not include 'quit'
        print('Eating', splitstring[1], splitstring[0], 'a day keeps the doctor away.')
        user_input = input()
        splitstring = user_input.split()

or

user_input = input()
splitstring = user_input.split()
while splitstring[0] != 'quit': # this will initiate the loop
    print('Eating', splitstring[1], splitstring[0], 'a day keeps the doctor away.')
    user_input = input()
    splitstring = user_input.split()

2 Comments

The inner if you have never works as you intend, as the variable splitstring is always going to be a list, not a string. Get rid of it and just do the else part unconditionally, and you'll have the same code as several other answers on here.
Good call! Updated the original answer to reflect your advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.