If the Repl Code does not open properly below, click this link to open it in a new tab.

CodeSports

Formal Calculations 4-1-Predict-and-Run

Task – Predict and Run

Students will learn how to perform simple calculations using fixed numbers, numbers in variables, and numbers from the user

Instructions:

This task contains three code examples.

Examine and study each example carefully. Predict about what each example will do when it runs. Your prediction should be included as comments to the code (#). In your prediction, you should use the key terms variable, operations, converting data types, and user input in your prediction.. If you need a reminder on these key terms, click on the links below.

Learning Goals


Be able to read, understand, trace, adapt and create Python code using selection that:

  • Perform simple operations such as addition (+), subtraction (-), multiplication (*), and division (/) on fixed numbers
  • Perform simple operations on numbers stored in variables
  • Convert strings into integers
  • Gets number input from user and use it within calculations

Division Differences


If one / is used for division, the result will be a decimal, known as a float. If two // are used for division, the esult will be a whole number, known as an integer (or int for short).

Casting


Input from the user in Python code is usually a string(text), but calculations required an integer(whole number) or a float(decimal number). Casting is the act of changing the data type of a variable to match a need.

Input Methods


To save the user’s inputted number as a whole number in a variable, use **int(input(“Ask user to enter something -> “))

To save the variable as a decimal number, use **float(input(“Ask user to enter something -> “))

Slide Deck


The link to the slide deck for this topic can be found here:

https://docs.google.com/presentation/d/1choFBbf7jyjNm-h37Z4Qyo1xOVVTj0W6mj6PCLRjbYw/edit?usp=sharing

Help! My Code Does Not Work!


When you run into an error with your code, ensure you have checked the following:

  • The variable name you have declared is the exact same throughout the program
  • You have used a single = to assign variables
  • You have used the correct operators for:
    • + addition
      
    • - subtraction
      
    • * multiplication
      
    • / float division
      
    • // integer division
      
  • You have placed brackets around input()

Hints