Python Forum

Full Version: Need assistance with this code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Gurus,

I need some help with creating a program for a parrot. Here is the information below.

1. Create a program that is a parrot. The program should begin by printing "Polly wants a cracker" on the screen. Then wait for the user to enter something (text). The program should then repeat whatever was entered back on the screen, followed by "Now give me a cracker.". Here is an example:

Polly wants a cracker.
>> Polly is nice.
Polly is nice. Now give me a cracker.


This is my program but I need some help with this code. I'm not sure how to complete this code. I'm not sure how to let the user enter text and what exactly are they entering. Lastly, I am not sure how to create the last two sentence.

parrot = "polly wants a cracker"
print(parrot)
polly= "polly is nice"
print(polly)

Thanks in advance for your help in this matter.
aka2d7
Try this:

# Print initial request for a cracker
print("Poly wants a cracker.")
# We will repeat further code forever
while True:
    # Save keyboard input to the variable "input_value"
    input_value = input()
    # Print keyboard input and "Now give me a cracker"
    print(input_value + " Now give me a cracker.")
    # Repeat
Lines starting with "#" are just comments with explanations.
Hi Tim,

I really appreciate your help but I need a little more details to help me understand this code. when I run this the "Now give me a cracker doesn't print. also can you please explain how does it know to print the Polly is nice. What happens there? I truly appreciate your quick response.

Thanks,
aka2d7
Well, the program do exactly what you want from it:

1) Prints "Poly wants a cracker." with command print("Poly wants a cracker.")
2) Waits for your input - you can type whatever you want. The input will be saved to variable input_value. You must hit "enter" after you'll finish with your input.
3) When you hit enter, your input will be printed on output and the " Now give me a cracker." string will be put after it.
4) Program again waits for your input - this is repeating forever because of the while True: loop.
Hi Tim,

Your AWESOME! Thank you so much!! This what I needed to understand your code and I appreciate your help in this example. I need help with a few more things do I post each thing that I need help with.