Here is another example of an "if statement", implemented with Python.
You need to think carefully about how you can adapt this "if statement" to meet the needs of your program.
shoesize = int(input("Please input your shoe size: "))
def bigfeet():
print("You have very big feet!")
def smallfeet():
print("You have small feet!")
if shoesize > 10:
bigfeet
if shoesize < 4:
smallfeet
The code above asks a user to input their shoe size. The program has two sections of code stored, one outputs (or prints) the message that they have big feet, the other outputs that they have small feet. Neither of these programs run at the start, they are just defined (that means you have just said what they are made up of).
How the program works
The user is asked to input their shoe size. Python will only accept an integer (whole number). Whatever the user types in (assuming that it is a number) is saved in the variable "shoesize".
The program then checks the number that is stored in the variable "shoesize" against some criteria. If the number stored in shoesize is bigger than 10, run the bit of code "bigfeet"; if the number stored in shoesize is less than 4, run the bit of code "smallfeet".
No comments:
Post a Comment