Understand how to input strings and numbers into variables: Difference between revisions

From Computer Science Wiki
No edit summary
No edit summary
Line 7: Line 7:
== 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 15:


<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 32: Line 29:




Test marks challenge
== Test marks challenge ==
Difficulty:  G
 
Write a program that will ask the user to enter three test marks out of 100 and output the average.
Write a program that will ask the user to enter three test marks out of 100 and output the average.
<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>
Temperature converter challenge
Temperature converter challenge
Difficulty:  G
Difficulty:  G

Revision as of 09:09, 25 August 2021

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.

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.

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))


Temperature converter challenge Difficulty:  G Write a program to enter a temperature in degrees Fahrenheit and display the equivalent temperature in degrees Centigrade. The formula for conversion is Centigrade = (Fahrenheit – 32) * (5/9) Height & weight challenge Difficulty:  G Write a program to convert a person’s height in inches into centimetres and their weight in stones into kilograms. [1 inch = 2.54 cm and 1 stone = 6.364 kg] Toy cars challenge Difficulty:  G A worker gets paid £9/hour plus £0.60 for every toy car they make in a factory. Write a program that allows the worker to enter the number of hours they have worked and the number of trains they have made. The program should output their wages for the day. Fish tank volume challenge Difficulty:  G Write a program that will ask the user to enter the length, depth and height of a fish tank in cm. Calculate the volume of water required to fill the tank and display this volume in litres and UK gallons. To calculate volume in litres, multiply length by depth by height and divide by 1000.


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

References[edit]