Kodlar :
gcd = __import__('math').gcd
pi = __import__('math').pi
factorial = __import__('math').factorial
def checkPrime(number):
primeList = list()
divisors = [i for i in range(number+1)]
del divisors[0], divisors[0], divisors[number-2]
figure = [i for i in range(10)]
del figure[0]
for divisor in divisors:
y = number / divisor
for i in figure:
if y % i in figure:
primeList.append(y)
if primeList == []:
return fr"|{number}| Is a Prime Number \ "
else:
return fr"|{number}| Is not a Prime Number \ "
def sqrt(number):
return number ** 0.5
def expNum(coefficient, exponential):
superscript = {
"0": "\u2070",
"1": "\u00b9",
"2": "\u00b2",
"3": "\u00b3",
"4": "\u2074",
"5": "\u2075",
"6": "\u2076",
"7": "\u2077",
"8": "\u2078",
"9": "\u2079"
}
equals = coefficient ** exponential
return str(coefficient) + superscript[str(exponential)] + f" = {equals}"
def findDivisors(x):
divisors = []
numbers = [i for i in range(1, x + 1)]
for number in numbers:
if x % number == 0: divisors.append(number)
return divisors
def lcm(x,y):
return x*y/gcd(x,y)
optionMenu = (
"""
Chose an Action and let me help you !
(enter the operation's number)
[1] basic mathematical operations (addition, divison ...)
[2] prime checking
[3] exponential calculation
[4] calculate square root
[5] calculate divisors
[6] calculate factorial
[7] calculate greatest common divisor
[8] calculate least common multiple
'Q' for exit
"""
)
print (
"""
========================================
Hello ! I am a smart mathematician bot.
I am here for helping you.
Let's Go On !
Warning : You Should Use '*' Operator for Multiplication and '/' For Division
========================================
""", end="\n\n\n"
)
while True:
req = input(optionMenu)
request = req.lower()
if request == 'q':
print("Good Bye !")
break
elif request == '1':
while True:
var = input("Write Your Operation As : 5 + 5 : \t")
try:
evalution = eval(var)
print(f"{var} = {evalution}")
except NameError: print("You Should Enter a Mathematical Operation")
finish = input("Wanna Quit ? Or continue ? \t q = quit | c = continue : ")
if finish.lower() == "q":
break
elif finish.lower() == "c":
continue
else:
print("You Must Dial 'q' or 'c'")
elif request == '2':
while True:
try:
number = int(input('Enter a Number : '))
print(checkPrime(number))
except:
print('Just Enter a Number')
finish = input("Wanna Quit ? Or continue ? \t q = quit | c = continue : ")
if finish.lower() == "q":
break
elif finish.lower() == "c":
continue
else:
print("You Must Dial 'q' or 'c'")
elif request == '3':
while True:
try:
coefficient = int(input("Enter a Coefficient : \t"))
exponential = int(input("Enter an Exponential : \t"))
print(expNum(coefficient, exponential))
except: print("You Must Use Integers")
finish = input("Wanna Quit ? Or continue ? \t q = quit | c = continue : ")
if finish.lower() == "q":
break
elif finish.lower() == "c":
continue
else:
print("You Must Dial 'q' or 'c'")
elif request == '4':
while True:
try:
number = int(input('Enter a Positive Number : \t'))
response = sqrt(number)
print(f'√{number} =', response)
except:
print('Please Enter a Natural Number')
finish = input("Wanna Quit ? Or continue ? \t q = quit | c = continue : ")
if finish.lower() == "q":
break
elif finish.lower() == "c":
continue
else:
print("You Must Dial 'q' or 'c'")
elif request == '5':
while True:
try:
number = int(input('Enter a Number : \t'))
response = findDivisors(number)
print(f'Divisors Of {number} : {response}')
except:
print("Please Enter a Number")
finish = input("Wanna Quit ? Or continue ? \t q = quit | c = continue : ")
if finish.lower() == "q":
break
elif finish.lower() == "c":
continue
else:
print("You Must Dial 'q' or 'c'")
elif request == '6':
while True:
try:
number = int(input("Enter a Positive Number : \t"))
response = factorial(number)
print(f'The Factorial Of {number} is : {response}')
except:
print("Please Enter Natural Number")
finish = input("Wanna Quit ? Or continue ? \t q = quit | c = continue : ")
if finish.lower() == "q":
break
elif finish.lower() == "c":
continue
else:
print("You Must Dial 'q' or 'c'")
elif request == '7':
while True:
try:
num1 = int(input("Enter the First Number : \t"))
num2 = int(input("Enter the Second Number : \t"))
response = gcd(num1, num2)
print(f'gcd(|{num1}|,|{num2}|) = {response}')
except:
print("Please Enter A Number")
finish = input("Wanna Quit ? Or continue ? \t q = quit | c = continue : ")
if finish.lower() == "q":
break
elif finish.lower() == "c":
continue
else:
print("You Must Dial 'q' or 'c'")
elif request == '8':
while True:
try:
num1 = int(input("Enter the First Number : \t"))
num2 = int(input("Enter the Second Number : \t"))
response = lcm(num1, num2)
print(f'lcm(|{num1}|,|{num2}|) = {response}')
except:
print("Please Enter A Number")
finish = input("Wanna Quit ? Or continue ? \t q = quit | c = continue : ")
if finish.lower() == "q":
break
elif finish.lower() == "c":
continue
else:
print("You Must Dial 'q' or 'c'")
else:
print("Please enter a number from option list")
Merhabalar, yukarıdaki kodları incelerseniz
finish = input("Wanna Quit ? Or continue ? \t q = quit | c = continue : ")
if finish.lower() == "q":
break
elif finish.lower() == "c":
continue
else:
print("You Must Dial 'q' or 'c'")
buradaki kodların sürekli tekrar ettiğini göreceksiniz. Bu kodları bir fonksiyona aldığımda herhangi bir loop olmadığı için break ve continue deyimleri hataya yol açıyor. Break ve continue deyimlerini içeren bu kodları bir fonksiyona almak mümkün müdür ? Yoksa tekrar etmek zorunda mıyız ?