Understand how to input strings and numbers into variables: Difference between revisions
Mr. MacKenty (talk | contribs) No edit summary |
Mr. MacKenty (talk | contribs) No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
<br /> | <br /> | ||
This page is meant to be used in conjunction with [[media:Programming companion Python.pdf | our excellent resource for learning python]]. This page tracks with '''objective 2'''. | This page is meant to be used in conjunction with [[media:Programming companion Python.pdf | our excellent resource for learning python]]. This page tracks with '''objective 2'''. | ||
Please remember: | |||
# Data is input by a user into a variable. | |||
# 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. | |||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 7: | Line 13: | ||
== Simple adder challenge == | == Simple adder challenge == | ||
Write a program that asks the user for two numbers. | 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. | 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. | ||
Line 15: | Line 21: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
number_1 = int(input("enter first number: ")) | number_1 = int(input("enter first number: ")) | ||
number_2 = int(input("enter second number: ")) | number_2 = int(input("enter second number: ")) | ||
answer = number_1 + number_2 | answer = number_1 + number_2 | ||
print("The answer is ", str(answer)) | print("The answer is ", str(answer)) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 27: | Line 30: | ||
</div> | </div> | ||
== Test marks challenge == | |||
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! | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
Click the expand link to see one possible solution, but NOT before you have tried and failed at least three times | |||
<div class="mw-collapsible-content"> | |||
<syntaxhighlight lang="python"> | |||
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)) | |||
</syntaxhighlight> | |||
</div> | |||
</div> | |||
Latest revision as of 10:13, 25 August 2021
![](/images/e/e4/Python.png)
This page is meant to be used in conjunction with our excellent resource for learning python. This page tracks with objective 2.
Please remember:
- Data is input by a user into a variable.
- 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.