How do I fix my code for Python Mad lips loops?
Question:
I’m having some problems with my code, I have included the prompt and my code below.
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 an integer as input, and outputs a sentence using the input values as shown in the example below. The program repeats until the input string is quit and disregards the integer input that follows.
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.
My code:
word = ''
tokens = ''
while True:
print('Eating {} {} a day keeps the doctor away.'.format(tokens[0],tokens[1]))
word = input()
tokens = input().split()
if tokens == 'quit':
break
Answers:
According to your problem statement, you have to take the input over and over again until the input string is quit
.
Try the following piece of code:
while True:
word = input()
token = int(input())
if word == 'quit':
break
else:
print(f'Eating {token} {word} a day keeps the doctor away.')
I am not sure i understand but maybe this is what you are looking for.
while True:
word = str(input('Enter word: '))
tokens = int(input('Enter number: '))
print('Eating {} {} a day keeps the doctor away.'.format(tokens,word))
if word == 'quit':
break
I think this is what you are looking for:
tokens = input().split()
index = 0
while True:
print('Eating {} {} a day keeps the doctor away.'.format(tokens[index + 1],tokens[index]))
if tokens[index + 2] == 'quit':
break
index += 2
while this will do what you want. if you input "apples 5 shoes 2 quit 0" it will return
Eating 5 apples a day keeps the doctor away.
Eating 2 shoes a day keeps the doctor away.
I feel a better way which would be safer is to just stop after you reach the end of tokens which would look something like this:
tokens = input().split()
index = 0
maxIndex = len(tokens)
while index + 1 < maxIndex:
print('Eating {} {} a day keeps the doctor away.'.format(tokens[index + 1],tokens[index]))
index += 2
that way you don’t need to add quit and your code won’t run forever if you forget it.
while True:
tokens = input().split()
if tokens == ['quit']:
break
print('Eating {} {} a day keeps the doctor away.'.format(tokens[0],tokens[1]))
I’m having some problems with my code, I have included the prompt and my code below.
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 an integer as input, and outputs a sentence using the input values as shown in the example below. The program repeats until the input string is quit and disregards the integer input that follows.
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.
My code:
word = ''
tokens = ''
while True:
print('Eating {} {} a day keeps the doctor away.'.format(tokens[0],tokens[1]))
word = input()
tokens = input().split()
if tokens == 'quit':
break
According to your problem statement, you have to take the input over and over again until the input string is quit
.
Try the following piece of code:
while True:
word = input()
token = int(input())
if word == 'quit':
break
else:
print(f'Eating {token} {word} a day keeps the doctor away.')
I am not sure i understand but maybe this is what you are looking for.
while True:
word = str(input('Enter word: '))
tokens = int(input('Enter number: '))
print('Eating {} {} a day keeps the doctor away.'.format(tokens,word))
if word == 'quit':
break
I think this is what you are looking for:
tokens = input().split()
index = 0
while True:
print('Eating {} {} a day keeps the doctor away.'.format(tokens[index + 1],tokens[index]))
if tokens[index + 2] == 'quit':
break
index += 2
while this will do what you want. if you input "apples 5 shoes 2 quit 0" it will return
Eating 5 apples a day keeps the doctor away.
Eating 2 shoes a day keeps the doctor away.
I feel a better way which would be safer is to just stop after you reach the end of tokens which would look something like this:
tokens = input().split()
index = 0
maxIndex = len(tokens)
while index + 1 < maxIndex:
print('Eating {} {} a day keeps the doctor away.'.format(tokens[index + 1],tokens[index]))
index += 2
that way you don’t need to add quit and your code won’t run forever if you forget it.
while True:
tokens = input().split()
if tokens == ['quit']:
break
print('Eating {} {} a day keeps the doctor away.'.format(tokens[0],tokens[1]))