Even or Odd
Ask the user for a number. Print:
Code:
# Ask the user for a number
user_input = input("Enter a number: ")
# Convert the input string to an integer
number = int(user_input)
# Check if the number is divisible by 2
if number % 2 == 0:
print("Even")
else:
print("Odd")
Count to N
Ask the user for a number.
If the user enters 5, print:
1
2
3
4
5
# Ask the user for a number
user_input = input("Enter a number: ")
# Convert the input string to an integer
n = int(user_input)
# Loop from 1 up to (and including) n
for i in range(1, n + 1):
print(i)
Positive, Negative, or Zero
Ask the user for a number.
Print whether it is:
# Ask the user for a number
user_input = input("Enter a number: ")
# Convert the input string to a float (allows both whole numbers and decimals)
number = float(user_input)
# Check the value of the number
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
Bigger Number
Ask the user for two numbers.
Print the larger number.
Example:
Enter first number: 8 Enter second number: 12 The larger number is 12.
# Ask the user for two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Compare the two numbers
if num1 > num2:
print("The larger number is + str(num1).")
elif num2 > num1:
print("The larger number is + str(num2).")
else:
print("Both numbers are equal.")
Password Check
The password is:
python
Ask the user for a password.
If correct:
Access Granted
Otherwise:
Wrong Password
# Set the correct password
SECRET_PASSWORD = "python"
# Ask the user for input
user_password = input("Enter password: ")
# Check if the entered password matches
if user_password == SECRET_PASSWORD:
print("Access Granted")
else:
print("Wrong Password")
Count Even Numbers
Ask the user how many numbers they want to enter.
After they enter all the numbers, print how many were even.
Example:
How many numbers? 5 3 8 10 7 2 There are 3 even numbers.
# Ask how many numbers the user wants to enter
count_input = input("How many numbers? ")
total_numbers = int(count_input)
even_count = 0
# Loop to collect each number
for i in range(total_numbers):
num = int(input())
if num % 2 == 0:
even_count += 1
# Print the final count using string concatenation
print("There are " + str(even_count) + " even numbers.")
Reverse a Word
Ask the user for a word.
Print it backwards.
Example:
Python nohtyP
(Without using [::-1].)
# Ask the user for a word
word = input("Enter String")
# Initialize an empty string to build the reversed word
reversed_word = ""
# Loop through each character in the original word
for char in word:
# Add the current character to the front of reversed_word
reversed_word = char + reversed_word
print(reversed_word)
Count the Digits
Ask the user for a number.
Print how many digits it has.
Example:
Enter a number: 48291 Digits: 5
# Ask the user for a number
user_input = input("Enter a number: ")
# Build the list of digits using .append()
digit_list = []
for char in user_input:
digit_list.append(char)
# Print the length of the list
print("Digits: " + str(len(digit_list)))
Print Every Other Letter
Ask the user for a word.
Print every other letter.
Example:
Enter a word: Python P t o
# Ask the user for a word
word = input("Enter a word: ")
# Loop through every other index in the word
for i in range(0, len(word), 2):
print(word[i])
Alphabet Challenge
Ask the user for a word.
Print the first and last letter.
Example:
Computer First: C Last: r
# Ask the user for a word
word = input()
# Get the first letter using index 0
first_letter = word[0]
# Get the last letter using negative index -1
last_letter = word[-1]
# Print the result using string concatenation
print("First: " + first_letter + " Last: " + last_letter)
Majority Even
Ask the user for 5 numbers.
Print:
even_count = 0
odd_count = 0
# Loop 5 times to get 5 numbers
for i in range(5):
num = int(input("Enter a number: "))
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
# Compare the counts
if even_count > odd_count:
print("Mostly Even")
elif odd_count > even_count:
print("Mostly Odd")
else:
print("Tie")
Target Sum: 9
Given:
numbers = [1, 2, 3, 4, 5] target = 9
Print the two numbers that add up to the target.
Output:
4 5
array = [1,2,3,4,5]
sum = 9
for i in range(len(array)-1):
numOne = array[i]
numTwo = array[i + 1]
if sum == numOne + numTwo:
print(numOne)
print(numTwo)
break
else:
continue
Find the Difference Between Largest and Smallest
Given:
numbers = [8, 3, 15, 6, 10]
Output:
Difference: 12
numbers = [8, 3, 15, 6, 10]
# Initialize largest and smallest with the first item in the list
largest = numbers[0]
smallest = numbers[0]
# Iterate through the list to find largest and smallest
for num in numbers:
if num > largest:
largest = num
if num < smallest:
smallest = num
difference = largest - smallest
print("Difference: " + str(difference))
Count Steps
A person walks up stairs.
Given:
steps = [1,0,1,1,0,1]
Where:
Print:
Steps taken: 4
steps = [1, 0, 1, 1, 0, 1]
steps_taken = 0
# Loop through each item in the list
for step in steps:
if step == 1:
steps_taken += 1
# Print output using string concatenation
print("Steps taken: " + str(steps_taken))
Count Grades
Given:
grades = [90,65,72,88,40]
Print:
A grades: 2 B grades: 1 C grades: 1 F grades: 2
grades = [90, 65, 72, 88, 40]
a_count = 0
b_count = 0
c_count = 0
f_count = 0
# Loop through each grade and check the range
for grade in grades:
if grade >= 85:
a_count += 1
elif grade >= 75:
b_count += 1
elif grade >= 65:
c_count += 1
else:
f_count += 1
# Print the results
print("A grades: " + str(a_count))
print("B grades: " + str(b_count))
print("C grades: " + str(c_count))
print("F grades: " + str(f_count))
Find the Missing Number
Given:
numbers = [1,2,3,5,6,7]
The list should contain numbers 1-7.
Find the missing number.
Output:
Missing number: 4
numbers = [1, 2, 3, 5, 6, 7]
# Loop through the expected numbers 1 to 7
for i in range(1, 8):
if i not in numbers:
missing = i
break
# Print using string concatenation
print("Missing number: " + str(missing))
Find the Longest Word
Given:
words = ["cat", "elephant", "dog", "butterfly", "fish"]
Find the longest word.
Output:
Longest word: butterfly Length: 9
words = ["cat", "elephant", "dog", "butterfly", "fish"]
longest_word = words[0]
# Loop through each word to find the longest one
for word in words:
if len(word) > len(longest_word):
longest_word = word
# Print using string concatenation
print("Longest word: " + longest_word + " Length: " + str(len(longest_word)))
Find the Median Value
Given:
numbers = [7, 2, 9, 4, 5]
Find the median value.
Remember:
The median is the middle number after the list is arranged from smallest to largest.
Expected output:
Median: 5
Hint: numbers. sort will sort the list for you
numbers = [7, 2, 9, 4, 5]
# Sort the original list
numbers.sort()
# Calculate the middle index
middle_index = len(numbers) // 2
# Extract the middle element
median = numbers[middle_index]
print("Median: " + str(median))
Find Duplicate Words
Given:
words = ["cat","dog","bird","cat","fish"]
Output:
Duplicate word: cat
words = ["cat", "dog", "bird", "cat", "fish"]
seen = []
duplicate = ""
# Loop through each word in the list
for word in words:
if word in seen:
duplicate = word
break
seen.append(word)
# Print output using string concatenation
print("Duplicate word: " + duplicate)
Largest Jump
Given:
numbers = [4, 10, 7, 20, 18]
Find the largest difference between two consecutive numbers.
Output:
Largest jump: 13 Between: 7 and 20
numbers = [4, 10, 7, 20, 18]
max_jump = 0
num1 = 0
num2 = 0
# Loop through the list to compare adjacent pairs
for i in range(len(numbers) - 1):
current_num = numbers[i]
next_num = numbers[i + 1]
# Calculate absolute difference to measure jump size
jump = abs(next_num - current_num)
if jump > max_jump:
max_jump = jump
num1 = current_num
num2 = next_num
# Print results using string concatenation
print("Largest jump:")
print(str(max_jump))
print("")
print("Between:")
print(str(num1) + " and " + str(num2))