Skip to content

Basic Syntax

  • Project
  • 1 hint
  • 6 of 6 in this chapter

Project time!

This is a project section. These operate a bit differently than lessons or quizzes, as they allow you to use what you have learned. We will give a few rules for you to follow, and you will program accordingly. Don't worry, there's no penalty or problem if you mess up. It's all about learning. At the end of this project, you can see the answer equivalent.

Assignment & User Input

For our first mini project, you will need to create a new Python program that:

  • takes in as input, a number of movie tickets
  • outputs a total cost in sentence form

Each ticket costs $11.99.

To do this, you will need to use the input() function. Basically, set a variable’s value to input() and in between the (), type the question in quotes.

Then, you can use the user’s input throughout the program using the assigned variable.

Python
age = input("Enter your age: ")
print("You are " + age + " years old!")

OUTPUT

Python
Enter your age: 
# When input is given:

Enter your age: 6
You are 6 years old! 

The input() function always gives you a string value, not an integer or float, even if you enter only a number as input. To change the datatype to an integer or float from a string, use int() or float() functions and put the value to be converted (can be a variable) between the ().

Hints

Hint 1hint 1

hint 2

Solution

Python
tickets = input("Enter number of tickets: ")
total_cost = int(tickets) * 11.99

print("The total cost of " + tickets + " tickets is $" + str(total_cost) + ".")  

OUTPUT

Text
Enter number of tickets: 7
The total cost of 7 tickets is $83.93.    

The int() is used to convert from data from a string type to an integer type. This allows us to then perform math operations on the number.

The str() is used to change data from an integer or float type to a string data type. This basically converts the data to text that we can use in strings, print statements, etc. It removes any confusion concerning whether the code should add the value to something as a number, or attach it to the end of another piece of text (i.e. string).

Give the project a try first.