def factorial(n):
# Selection
if n < 0:
return "Factorial is not defined for negative numbers"
# in case negative numbers are inputted...
elif n == 0 or n == 1:
# "else if" sets a condition to be met, and if it is met, the code should return 1
return 1
else:
result = 1
for i in range(2, n + 1):
#iterates through the list, from 2 to n inclusive - 1 is not counted, n*1 = n
result *= i
#multiplies the current value of result by i
# i is sort of like the index, or the number assigned to each value in the list, starting from 0
return result
def main():
try:
# Get user input for the numbers as a comma-separated list
user_input = input("Enter a list of non-negative integers (comma-separated): ")
numbers = [int(num.strip()) for num in user_input.split(',')]
# Calculate and print factorial for each number
for number in numbers:
# Calculate factorial for each number, iterates through the list
result = factorial(number)
if isinstance(result, int):
print(f"The factorial of {number} is: {result}")
# Selection, makes sure the result is actually an integer before printing
else:
print(f"The factorial for {number} is not defined (negative input).")
except ValueError:
print("Invalid input. Please enter a valid list of non-negative integers.")
if __name__ == "__main__":
main()
The factorial of 5 is: 120
Scenario: You’re in your math test and you forgot how to graph the parent function of 3(x+2)-4. Input a list of x-values to figure out the y-values and print the results of what you get so you can graph the function and get an A on your math test!
yn = "y"
while yn == "y":
num = input("what number would u like to compute for: ")
num = [int(num.strip()) for num in num.split(',')]
for j in range(len(num)):
num[j] = 3*num[j]+2
for i in num:
print("The value of 3x+2 for this x-value is:", str(i))
yn = input("continue?")
The value of 3x+2 for this x-value is: 17
The value of 3x+2 for this x-value is: 20
The value of 3x+2 for this x-value is: 20
num1 = 40
num2 = num1 / 2
num3 = 5 * num2 + 3
result = num2 % 3 * num1 + 4 - num3 / 2
print (result)
32.5
What will the code below print?
num1 = 20
num2 = num1 /2
num3 = num2 * 10 + 3
print(num3)
#This code will print out 103.0 because the division operator converts the integer to a float
103.0
Write a code that can add, subtract, multiply, divide, and MOD numbers. Try adding them in the same line and seeing what they do. Try adding variables to your hack #2.
num = int(input("write a number: "))
num = num*2/5%4+7-18
print(num)
-9.0
len ("APCSP")
5
concat = "AP" + "CSP"
# Concat is simply a variable
print(concat)
APCSP
Find the number of characters in your last name using len. Use concat to merge your first and last name together. Use Substring to show only the 3rd to 6th characters.
name = "SrijanAtti"
print(len(lastname))
print(name[2:6])
4
ijan
#Find the number of characters in your last name using len.
length = len("Bharadwaj")
print(length)
# Use concat to merge your first and last name together.
concat = "Aditi" + "Bharadwaj"
print (concat)
# Use Substring to show only the 3rd to 6th characters.
# be sure to start at 2 because it starts counting at 0
substring = concat[2:6]
print (substring)
9
AditiBharadwaj
itiB
A palindrome is a string that reads the same from right to left as left to right. For example, 2112 and mom are palindromes
def palindrome(input_str):
# Remove spaces and convert the string to lowercase
clean_str = input_str.replace(" ", "").lower()
# Check if the cleaned string is equal to its reverse
return clean_str == clean_str[::-1]
print(palindrome("taco cat")) # True
print(palindrome("hi")) # False
True
False
Python is a high-level programming language. When you write an algorithm in Python, you are essentially creating a set of instructions that a computer can follow to solve a specific problem.
name = "Eshika"
age = 15
print("Name:", name)
print("Age:", age)
College Board Pseudo Code is a simplified, high-level representation of an algorithm that is designed to be easy to understand. Pseudo code uses a mixture of natural language and programming-like constructs to describe the algorithm’s logic without specifying a particular programming language.
OUTPUT “Name:”, name OUTPUT “Age:”, age </span>
If the robot leaves the grid, the program will end Command MOVE_FORWARD() moves the robot one block in the direction its facing Command ROTATE_LEFT() moves the robot left in place Command ROTATE_RIGHT() makes the robot do an in place right turn Command CAN_MOVE(direction) makes it so that if there is an open space in the relative direction the robot is facing, the robot goes there, can be any direction
Move forward Move forward Move forward Rotate left Move forward Move forward rotate right move forward move forward move forward
AND gate, OR gate, NOT gate, NAND gate, NOR gate, XOR gate, and XNOR gate.
What is a logic gate?: Logic gates are used in circuits to make decisions based on a combination of digital signals beign inputed. The concept of logic gates revolves around Boolean algebra as well as binary. It is basically how zero represents the false condition and 1 represents True. Think of it as a switch where if it is at 0, the switch is turned off while if it is 1, then the lightbulb will turn on.
The AND gate requires both inputs to be 1 or true so the end output is true. In other words, the output is 1 only when both inputs one AND two are 1.
age= 15
if age>2 and type(age)==int:
print(age)
age=13
if age>2 and type(age)==str:
print(age)
15
Use an algorithm to find certain values of the Fibonacci sequence. For example, your code should output the nth index in the sequence. An example of how the sequence works is 0,1,1,2,3,5,8,13. The pattern is you add the previous 2 numbers together to get the new number.
def fibonacci_sequence(n):
if n <= 0:
return "Provide a positive integer."
sequence = [0, 1]
while len(sequence) < n:
sequence.append(sequence[-1] + sequence[-2])
return sequence
# Example usage:
sequence_length = 20
fibonacci_list = fibonacci_sequence(sequence_length)
print(f"The first {sequence_length} values in the Fibonacci sequence are: {fibonacci_list}")
nth_value = int(input("enter index less than 20"))
result = fibonacci_sequence(nth_value)[-1] # Get the last value for the nth position
print(f"The {nth_value}th value in the Fibonacci sequence is: {result}")
The first 20 values in the Fibonacci sequence are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
The 10th value in the Fibonacci sequence is: 34