L
L
Lexasll2021-11-24 18:20:25
Python
Lexasll, 2021-11-24 18:20:25

Hello, I have two functions, the first one calculates the discriminant, the second converts the numbers into text, how can I combine them?

ones = ("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")

twos = ("ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen")

tens = ("twenty","thirty","forty" ,"fifty","sixty","seventy","eighty","ninety")

suffixes = ('', 'thousand', 'million', 'trillion')

def discriminant():
print('Diskriminant a• x²+b•x+c=0')

a = float(input('Enter a value a:'))

b = float(input('Enter a value b:'))

c = float(input('Enter a value c:'))

discriminant = b**2 - 4*a*c

print('Discriminant:' + str(discriminant))

if discriminant < 0:

print('No roots')

elif discriminant == 0:

x = -b / (2 * a)

print('x = ' + str(x))

else:

x1 = (-b + discriminant ** 0.5) / (2 * a)

x2 = (-b - discriminant ** 0.5) / (2 * a)
print( 'x₁ = ' + str(x1))
print('x₂ = ' + str(x2))

def process(number, index):

if number == '0':

return 'Zero'

length = len(number)

if ( length > 3):
return False

number = number.zfill(3)
words = ''

hdigit = int(number[0])
tdigit = int(number[1])
odigit = int(number[2])

words += ' ' if number[0] == '0'else ones[hdigit]
words += ' hundred ' if not words == '' else ''

if (tdigit > 1):
words += tens[tdigit - 2]
words += ' '
words += ones[odigit]

elif (tdigit == 1 ):
words += twos[(int(tdigit + odigit) % 10) - 1]

elif (tdigit == 0):
words += ones[odigit]

if (words.endswith('Zero')):
words = words [:-len('Zero')]
else:
words += ' '

if (not len(words) == 0):
words += suffixes[index]

return words;

def getWords(number):
length = len(str(number))

if length > 12:
return 'This program supports upto 12 digit numbers.'

count = length // 3 if length % 3 == 0 else length // 3 + 1
copy = count
words = []

for i in range(length - 1, -1, -3):
words.append(process(str (number)[0 if i - 2 < 0 else i - 2: i + 1], copy - count))
count -= 1;

final_words = ''
for s in reversed(words):
temp = s + ' '
final_words += temp

return final_words

number = int(input('number from 0-999: '))
print('%d in words is: %s' % (number, getWords(number)))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question