Input and output
Program 1
Write a program to take in two numbers and display the result of them being added together.
print(int(input("Insert number 1")) + int(input("Insert number 2")))
Program 2
Write a program to take in a users name and then display "Hello " and the person's name.
print("Hello", input("Please insert your name"))
Variables and data types
Program 3
Adapt the program in Program 2 so that it now stores the user's name in a variable.
name = input("Please insert your name") print("Hello", name)
Program 4
Fix this program to ensure that it runs correctly.
var-1 = 1 2ndvar = 2 Β£var3 = 3 class = 4
# Cannot have - in the name of the variable var1 = 1 # Cannot have start a variable name with a number var2 = 2 # Cannot have Β£ in the name of the variable var3 = 3 # class is a keyword and cannot be used classVar = 4
Selection
Program 5
Write a program to take in two numbers, number1 and number2. If the first number typed in is larger than the second number it should add both numbers together. If the second number is larger, it should take the first number away from the second. If both numbers are equal it will multiple both numbers together. It should then display the result.
number1 = int(input("Please number 1")) number2 = int(input("Please number 2")) result = 0 if(number1 > number2): result = number1 + number2 elif(number2 > number1): result = number2 - number1 else: result = number1 * number2 print(result)
Program 6
Write a program to ask the user for a user name and password. The program should check that the user has typed in the correct password. If they have the program should display "Welcome to the system." otherwise it should display "Your details are incorrect and you cannot be logged in.".
username = input("Please enter a username") password = input("Please enter a password") if(username == "johnsmith" and password == "pass"): print("Welcome to the system.") else: print("Your details are incorrect and you cannot be logged in.")
Fixed loops
Program 7
Write a program to display all numbers from the two times table up to 2006.
for counter in range(1003): print(counter * 2)
Program 8
Write a program to take in 5 numbers and add them all together.
for counter in range(5): number = int(input("Enter a number")) total = total + number print(total)
Conditional loops
Program 9
Write a program to loop until the user types in a number that is greater than 100.
number = int(input("Enter a number over 100")) while number < 101: number = int(input("The number you entered is less than 100. Enter a number over 100")) print(total)
Program 10
Write a program which requires the user to login. The user must enter both a username and password which both need to be correct for the user to login.
username = input("Enter your username") password = input("Enter your password") while username != "username" or password != "password": print("Either your username or password (or both) was wrong.") username = input("Enter your username") password = input("Enter your password") print("Welcome!")