What Does this code do?
def sum(a, b):
return (a + b)
a = int(input('Enter 1st number: '))
b = int(input('Enter 2nd number: '))
print(f'Sum of {a} and {b} is {sum(a, b)}')
It will ask the user to enter 2 numbers and the code will find the sum of both numbers
why does this code start at 1 and end at 6?
for i in range(1, 6):
print("Number:", i)
Because it starts at 0 for 1, then goes up 1 for 2, until it reaches 5 for 6.
What does the 'else' do in this code?
user = input("Enter Username: ")
if user == "Admin":
print("Vip user")
else:
print("Standard user")
Fix the following:
color = input("Color: ")
if color == "red" or "blue":
print("Primary")
else:
print("Other")
The 4th line should be
"if color == "red" or color == "blue":
What does if/else do in code.
if/else lets a program make decisions: it runs one block of code if a condition is true, and a different block else if the condition is false.
What will happen is you enter "secret234" as the password?
password = "secret123"
guess = input("Enter password: ")
if guess == password:
print("Welcome")
else:
print("Invalid password")
The code will show "Invalid password"
total = 0
what is happening in this code?
for i in range(1, 11):
total += i
print("The total is:", total)
The code adds up the numbers from 1 to 10 by looping through each number and adding it to total. After the loop finishes, it prints the final sum, which is 55.
What is the if statement do in this code?
number= int(input("Enter number: ))
if number % 2 == 0:
print("Even")
else:
print("Odd")
The code will divide the number the user entered and if there is no remainder, it shows even.
Fix the Following:
age = input("Age: ")
if age > 18:
print("Adult")
if age <= 18:
print("Child")
The first line is missing the 'Int' in front of input, and should replace the second if with elif.
What does Loop logic do in code?
Loop logic lets a program repeat the same block of code multiple times, usually until a condition is met or a set number of repetitions is reached.
What will the code print if you enter 70?
temp = int(input("Enter temperature: "))
if temp >= 90:
print("Hot")
elif temp >= 70:
print("Warm")
elif temp >= 50:
print("Cool")
else:
print("cool")
The code will say "warm"
count = 5
while count > 0:
print(count)
count -= 1
print("Done!")
This code counts down from 5 to 1, printing each number as it decreases count by 1 each time. When the loop ends, it prints "Done!".
What does the if statement do in this code?
password = ("secret123")
guess = input("Guess password")
if guess == password:
print("Correct!")
else:
print("Incorrect")
when the user enter the password, if it is "secret123" then it will display correct.
Fix the Following:
temp = input("Temp: ")
if temp < 0 or temp > 100:
print("Extreme")
elif temp > 32:
print("Warm")
else
print("Cold")
The code is missing 'Int' on the first line
'else' is missing ':'
What does 'int' do in python code?
int gives numbers numerical values.