Python AAU Final Exam Answer
Python AAU Final Exam Answer
Part one
Error finding (10 pts.)
1. number = input(“please enter any number: ”) # the number
can not be interpreted as an integer.
2. For i in range(1,number):
3. if (number % i ==0):
4. sum += 1 # unsupported operand types(s)
5. if (sum == number):
6. print(number , ”is a perfect number”)
7. else:
8. print(number ,”is not a perfect number”)
by Neway.M 1 6/19/2025
Answer
by Neway.M 2 6/19/2025
Question 2
for (i in range(11.5)):
print(i, end=‘ ’)
by Neway.M 3 6/19/2025
Line Error type solution
error
1 Syntax error for i in range float(11.5)
by Neway.M 4 6/19/2025
Question 3
i +=2
while(i <== 9): # assignment operator syntax error
print(‘the value of i is:’)
Print(i+=1)
by Neway.M 5 6/19/2025
Answer
by Neway.M 6 6/19/2025
Error type Description
1) Forgetting to put a :
by Neway.M 9 6/19/2025
3) Using the wrong amount of indentation.
by Neway.M 11 6/19/2025
5) Trying to modify a string value.
by Neway.M 12 6/19/2025
6) Trying to concatenate a non-string value to a string
value.
(Causes “Type Error: Can't convert 'int' object to str
implicitly”)
by Neway.M 13 6/19/2025
7) Forgetting a quote to begin or end a string value.
by Neway.M 14 6/19/2025
8) A typo for a variable or function name.
by Neway.M 15 6/19/2025
9) A typo for a method name.
by Neway.M 16 6/19/2025
10) Going past the last index of a list.
by Neway.M 17 6/19/2025
11) Using a non-existent dictionary key.
by Neway.M 18 6/19/2025
12) Trying to use a Python keyword for a variable
name.
(Causes “Syntax Error: invalid syntax”)
by Neway.M 19 6/19/2025
13) Using an augmented assignment operator on a
new variable.
(Causes “Name Error: name 'foobar' is not defined”)
Do not assume that variables start off with a value
such as 0 or the blank string.
A statement with an augmented operator like spam
+= 1 is equivalent to spam = spam + 1.
This means that there must be a value in spam to
begin with.
by Neway.M 20 6/19/2025
14) Using a local variable (with the same name as a global
variable) in a function before assigning the local variable.
(Causes “UnboundLocal Error: local variable 'foobar'
referenced before assignment”)
Using a local variable in a function that has the same
name as a global variable is tricky. The rule is: if a
variable in a function is ever assigned something, it is
always a local variable when used inside that function.
Otherwise, it is the global variable inside that function.
This means you cannot use it as a global variable in the
function before assigning it.
by Neway.M 21 6/19/2025
15) Trying to use range() to create a list of integers.
(Causes “Type Error: 'range' object does not support
item assignment”)
Sometimes you want a list of integer values in
order, so range() seems like a good way to
generate this list.
However, you must remember that range() returns
a "range object", and not an actual list value.
by Neway.M 22 6/19/2025
16) There is no ++ increment or –- decrement
operator. (Causes “SyntaxError: invalid syntax”)
by Neway.M 23 6/19/2025
by Neway.M 24 6/19/2025
Part two
Code output(10pt.)
4. row = 6
String = “python”
for i in range(row,0,-1):
j= 0
for k in string:
if(j == i):
break
print(j,end=k)
j += 1
print(“ ”)
by Neway.M 25 6/19/2025
Question 5
def output(X(i)):
i += 1
print(i)
i = 19
for i in range(10):
output(X(i))
i+=5
output(X(i))
by Neway.M 26 6/19/2025
Part3
Completion(14pt.)
Fibonacci series
The first number start with 0 and the second number start with1
First,second=0,1
n= t-in---------------
Print(“Fibonacci series are: ”)
For i in---range(n):-------------------
if i <= 1:
result = I
else:
result =first + second----------------------
first =---second---------------------
second=-result-----------------
print(result)
by Neway.M 27 6/19/2025
Fibonacci Series In Python
by Neway.M 28 6/19/2025
The logic behind Fibonacci sequence in
python
by Neway.M 29 6/19/2025
Python Five method to Fibonacci
series
Table of Contents
1. Fibonacci series in python using for loop
2. Fibonacci series in python using while loop
3. Fibonacci series using recursion in python
4. Printing Nth term of Fibonacci series using
recursion
5. Printing Fibonacci series dynamically
by Neway.M 30 6/19/2025
1. Fibonacci series in python using for loop
Accept the number of terms from the user and pass that number in
the loop and implement the Fibonacci number’s logic in the loop.
n = int(input("Enter number of terms: "))
n1, n2 = 0, 1
i=0
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence up to",n,":")
print(n1)
else:
print("Fibonacci sequence:")
for i in range(n):
print(n1)
sum = n1 + n2
n1 = n2
n2 = sum
i += 1
by Neway.M 31 6/19/2025
Fibonacci series in python using while loop
Take a number of terms of the Fibonacci series as input from the user and iterate while
loop with the logic of the Fibonacci series.
n = int(input("Enter number of terms: "))
n1, n2 = 0, 1
i=0
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence up to",n,":")
print(n1)
else:
print("Fibonacci sequence:")
while (i <n):
print(n1)
sum = n1 + n2
n1 = n2
n2 = sum
i += 1
by Neway.M 32 6/19/2025
[Link] series using recursion in python
by Neway.M 33 6/19/2025
[Link] Nth term of Fibonacci series
using recursion
by Neway.M 34 6/19/2025
[Link] Fibonacci series
dynamically
Initialize an array with 0 and 1 and accept a number of terms from the user.
The function will append the values to the array which will reduce memory
wastage.
def fib(n):
a = [0,1]
if n == 1:
print('0')
elif n == 2:
print(a)
else:
while(len(a) < n):
[Link](0)
if(n == 0 or n == 1):
return 1
else:
a[0]=0
a[1]=1
for i in range(2, n):
a[i]=a[i - 1]+a[i - 2]
print("First {} terms of fibonacci series:".format(n),a)
return a[n - 2]
n = int(input("Enter number of term: "))
fib(n)
by Neway.M 35 6/19/2025
Question7
check Armstrong number
digit = 0
result = 0
number = 370
number1 = number
temp = number
#digit counting code
while number != 0:
number =--number//10--------
#the sum of each digit powered to the digit count
While-number1!=0------------:
number=----------------------
result=--------------------------
number1 = number1//10
If------------:
print(“number is Armstrong”)
else:
Print(“number is not Armstrong”)
by Neway.M 36 6/19/2025
Python Program to Check
Armstrong Number
by Neway.M 37 6/19/2025
Concept of Armstrong number
by Neway.M 38 6/19/2025
Python code to check weather a given
number is Armstrong number or not
# take input from the user
num = int(input("Enter a number: "))
# initialize sum
sum = 0
by Neway.M 39 6/19/2025
Cont.….
# initialize sum
sum = 0
by Neway.M 42 6/19/2025
Answer
python digits counting code
# Python Program to Count Number of Digits in a Number
using While loop
by Neway.M 43 6/19/2025
Sum of each digit powered to
number of digit
by Neway.M 44 6/19/2025
Question8
calculate number of occurrence of a given
character from given string
by Neway.M 46 6/19/2025
Cont.…
by Neway.M 47 6/19/2025
Python Program to Count Occurrence of a
Character method 2
by Neway.M 48 6/19/2025
Python Program to Count Total Occurrence of a
Character method 3
by Neway.M 51 6/19/2025
code
# Use String Indexing in Python to check if a String is a
Palindrome
a_string = 'Was it a car or a cat I saw'
def palindrome(string):
string = [Link]().replace(' ', '')
return string == string[::-1]
print(palindrome(a_string))
by Neway.M 52 6/19/2025
steps
by Neway.M 53 6/19/2025
[Link] the Python Reversed Function to Check if a String is a
Palindrome
by Neway.M 54 6/19/2025
Cont..
by Neway.M 55 6/19/2025
[Link] a For Loop to Check if a Python String is a
Palindrome
by Neway.M 56 6/19/2025
Cont..
by Neway.M 57 6/19/2025
[Link] a Python While Loop to Check if a String is a Palindrome
by Neway.M 59 6/19/2025
[Link] if a Number is a Python Palindrome
by Neway.M 60 6/19/2025