Your friend is about to have a birthday! Write a program that asks them how old they are. Once they enter their age, print how many candles they’ll need for their birthday cake. (This should be one more than their current age.)

You will have to use input to retrieve information from the user, and int to convert what they type into an integer. Finally, you’ll have to alter their age variable to tell the user how many candles they will need.

Here is what running your program should look like:

Enter your age: 117

You will need this many candles for your birthday cake:
118

1 answer

Here’s a simple Python program that accomplishes what you described:

# Ask the user for their age
age = input("Enter your age: ")

# Convert the input to an integer
age = int(age)

# Calculate the number of candles needed
candles_needed = age + 1

# Print the result
print("You will need this many candles for your birthday cake:")
print(candles_needed)

How it works:

  1. The program prompts the user to enter their age.
  2. It retrieves the input as a string and converts it to an integer using int().
  3. The program calculates the number of candles needed by adding one to the user's age.
  4. Finally, it prints the number of candles they’ll need for their birthday cake.

Example usage:

If the user enters 117, the output will be:

Enter your age: 117
You will need this many candles for your birthday cake:
118

You can run this program in any Python environment, such as an interpreter or an IDE that supports Python.