Understand how to input strings and numbers into variables

From Computer Science Wiki
Python programming language[1]


This page is meant to be used in conjunction with our excellent resource for learning python. This page tracks with objective 2.

Please remember:

  1. Data is input by a user into a variable.
  2. Variables have a data type: string, integer or float as examples, indicating how much memory they will use and the type of data they will store.




Simple adder challenge[edit]

Write a program that asks the user for two numbers. Part of the reason this one can be tricky is because you need to think about the Programming#Primitive data types of data you are using. Please remember in Python we can't mix integers and strings.


Click the expand link to see one possible solution, but NOT before you have tried and failed at least three times

number_1 = int(input("enter first number: "))
number_2 = int(input("enter second number: "))
answer = number_1 + number_2
print("The answer is ", str(answer))

Test marks challenge[edit]

Write a program that will ask the user to enter three test marks out of 100 and output the average. It might helpful to remember an old friend, PEMDAS!


Click the expand link to see one possible solution, but NOT before you have tried and failed at least three times

number_1 = int(input("enter the first test score: "))
number_2 = int(input("enter the second test score: "))
number_3 = int(input("enter the third test score: "))
answer = (number_1 + number_2 + number_3) / 3
print("The average of the three tests scores is: ", str(answer))


If you are still stuck, or you have other questions, you may want to ask a question on our discussion board.

References[edit]