How to make python multiple conditions into a single statement - python

How to make the method below be returned in a single line?
Prefer to the #Note ## remarks below.
def falsify(leftover):
#Note ## Your code here (replace with a single line) ###
def falsify(leftover):
false = []
for num in leftover:
if 30 > num > 20:
false.append(num - 10)
elif num >= 30:
false.append('1' + (str(num[1:])))
else:
false.append(num)
return false
I don't have any other idea except breaking into 2 methods.
leftover1 = [19.7, 20.0, 28.5, 30.0, 30.7]
def process(leftover):
false = []
for num in leftover:
print('num:' , num)
if 30 > num >= 20: false.append(num - 10)
elif num >= 30:
# (str(num[1]))
result = str(num)
#print('result:' , result)
false.append('1' + result[1:])
#
else:
false.append(num)
return false
def falsify(leftover):
#Note ## Your code here (replace with a single line) ###
return process(leftover)
print('result', falsify(leftover1))
Sample output as below
num: 19.7
num: 20.0
num: 28.5
num: 30.0
num: 30.7
result [19.7, 10.0, 18.5, '10.0', '10.7']

leftover = [19.7, 20.0, 28.5, 30.0, 30.7]
def falsify(leftover):
return [num - 10 if 30 > num >= 20 else ('1' + str(num)[1:]) if num >= 30 else num for num in leftover]
print('result', falsify(leftover))
# output
#result [19.7, 10.0, 18.5, '10.0', '10.7']

You can do it with list comprehension:
def falsify(leftover):
return [num - 10 if 30 > num > 20 else ('1' + str(num[1:])) if num >= 30 else num for num in leftover]

Related

Recursive function to reduce zeros from number

I have a mission to write a recursive function named Reduce. This function should reduce all the zeros from number and return new number.
for example:
Reduce(-160760) => -1676
Reduce(1020034000) => 1234
I started to to something but I got stuck in the condition. here's the code I wrote so far:
def Reduce(num):
while num != 0:
if num % 10 != 0:
newNum = (num % 10) +
Reduce(num//10)
def reduce(num):
if num == 0: return 0
if num < 0: return -reduce(-num)
if num % 10 == 0:
return reduce(num // 10)
else:
return num % 10 + 10 * reduce(num // 10)
String version of recursive function:
def reduce(n):
return int(reduce_recursive(str(n), ''))
def reduce_recursive(num, res):
if not num: # if we've recursed on the whole input, nothing left to do
return res
if num[0] == '0': # if the character is '0', ignore it and recurse on the next character
return reduce_recursive(num[1:], res)
return reduce_recursive(num[1:], res+num[0]) # num[0] is not a '0' so we add it to the result and we move to the next character
>>> reduce(1200530060)
12536

Passing data between a series of related functions

I am attempting to create an exam grading program. I successfully wrote each function to do what I need it to, but when I attempt to execute the entire program, I am running into issues with my return variables not being referenced.
Here is my code for context:
def userinput():
allinputs = []
while True:
try:
results = list(map(int, input('Exam points and exercises completed: ').split(' ')))
allinputs.append(results)
except:
ValueError
break
return allinputs
def points(allinputs):
exampoints = []
exercises = []
exercises_converted = []
gradepoints = []
counter = 0
for i in allinputs:
exampoints.append(i[0])
exercises.append(i[1])
for e in exercises:
exercises_converted.append(e//10)
for p in exampoints:
p2 = exercises_converted[counter]
gradepoints.append(p + p2)
counter += 1
return (gradepoints, exampoints, exercises_converted)
def average(gradepoints):
avg_float = sum(gradepoints) / len(gradepoints)
points_avg = round(avg_float, 1)
return points_avg
def pass_percentage(exercises_converted, exampoints):
failexam = []
passexam = []
passorfail = []
i = 0
while i < len(exampoints):
if exampoints[i] < 10 or exercises_converted[i] + exampoints[i] < 15:
failexam.append("fail")
passorfail.append(0)
else:
passexam.append("pass")
passorfail.append(1)
i += 1
percentage_float = len(passexam)/len(failexam)
percentage = round(percentage_float, 1)
return (percentage, passorfail)
def grades_distribution(passorfail, gradepoints):
grades = []
i = 0
l = 5
while i < len(gradepoints):
if passorfail[i] == 0:
grades.append(0)
elif passorfail[i] != 0 and gradepoints[i] >= 15 and gradepoints[i] <= 17:
grades.append(1)
elif passorfail[i] != 0 and gradepoints[i] >= 18 and gradepoints[i] <= 20:
grades.append(2)
elif passorfail[i] != 0 and gradepoints[i] >= 21 and gradepoints[i] <= 23:
grades.append(3)
elif passorfail[i] != 0 and gradepoints[i] >= 24 and gradepoints[i] <= 27:
grades.append(4)
elif passorfail[i] != 0 and gradepoints[i] >= 28 and gradepoints[i] <= 30:
grades.append(5)
i += 1
while l >= 0:
print(l, ": ", "*" * grades.count(l))
return
userinput()
print("Statistics:")
points(allinputs)
print(f"Points average: {average(gradepoints)}")
print(f"Pass percentage: {pass_percentage(exercises_converted, exampoints)}")
print("Grade distribution")
grades_distribution(passorfail, gradepoints)
I have no problems with the mechanics within each function; rather, my error lies where I try calling each of them from the main function.
The user input() function runs fine, and returns allinputs. However, when the second function points(allinputs) is called, the program states that the argument is undefined.
You should store the return value of a function before passing it as argument.
This should solve your problem
allinputs = userinput()
points(allinputs)

Multiples of 10 in a list

I am currently doing an assignment in my intro level CS class and just need a smidge of help.
They are asking me to write a program that reads a list of integers and determines if it has;
multiples of 10
no multiples of 10
mixed values.
It currently correctly outputs everything but mixed values. This is what I have:
n = int(input())
my_list =[]
for i in range(n):
num = int(input())
my_list.append(num)
def is_list_mult10(my_list):
mult10 = True
for i in range(len(my_list)):
if my_list[i] % 10 != 0:
mult10 = False
return mult10
def is_list_no_mult10(my_list):
no_mult10 = True
for i in range(len(my_list)):
if my_list[i] % 10 != 1:
no_mult10 = False
return no_mult10
if is_list_no_mult10(my_list) == True:
print("no multiples of 10")
elif is_list_mult10(my_list) == True:
print("all multiples of 10")
else:
print("mixed values")
def check_multiplier(my_list):
is_10_multiplier = []
for i in my_list:
if i % 10 == 0:
is_10_multiplier.append(True)
else:
is_10_multiplier.append(False)
if sum(is_10_multiplier) == len(my_list):
print("all multiples of 10")
elif sum(is_10_multiplier) == 0:
print("no multiples of 10")
else: print("mixed values")
# tests
mixed = [1, 20, 34, -10]
check_multiplier(mixed)
no_10 = [13, 22, 101, -5]
check_multiplier(no_10)
only_10 = [20, 140, 30, -50]
check_multiplier(only_10)
Function check_multiplier indexing all elements from my_list and saves booleans into is_10_multiplier. Then checks the sum of is_10_multiplier, if all items are True then sum is equal length of passed list, if all are False then sum is 0.
As mentioned in the comments, you have a couple of errors in your code (the return statements are inside the for loop).
Also, the logic seems a little too complicated :) No need to have 2 separate functions, you can try:
n = int(input('How many numbers?: '))
my_list =[]
for i in range(n):
num = int(input(f'Insert element {i}: '))
my_list.append(num)
def how_may_mult10(my_list):
# counting how many multiples of 10 you have in your list
number_of_m10 = 0
for num in my_list:
if num % 10 == 0:
number_of_m10 += 1
return number_of_m10
number_of_m10 = how_may_mult10(my_list)
if number_of_m10 == len(my_list):
print('All multiples of 10')
elif number_of_m10 == 0:
print('No multiples of 10')
else:
print('Mixed values')
I see you have done some logical as well as syntactic error, as mentioned in the comments also.
Below is your modified code :
n = int(input())
my_list =[]
for i in range(n):
num = int(input())
my_list.append(num)
def is_list_mult10(my_list):
mult10 = True
for i in range(len(my_list)):
if my_list[i] % 10 != 0:
mult10 = False
return mult10 #changed here
def is_list_no_mult10(my_list):
no_mult10 = True
for i in range(len(my_list)):
if my_list[i] % 10 == 0: #changed here
no_mult10 = False
return no_mult10 #changed here
if is_list_no_mult10(my_list) == True:
print("no multiples of 10")
elif is_list_mult10(my_list) == True:
print("all multiples of 10")
else:
print("mixed values")
It successfully prints the correct statement. However I'll suggest you to try to optimise e your program.

How do I create a new variable from the result of a for loop with an if else statement in it in python?

I have these temperatures:
temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4]
This works as a statement, but I can't get this in to a variable:
for i in temperatures:
if i < -2:
print('Cold')
elif i >= -2 and i <= 2:
print('Slippery')
elif i >2 and i < 15:
print('Comfortable')
else:
print('Warm')
I know that the following code works to get a variable from a loop:
x = [i for i in range(21)]
print (x)
So I tried this, but it doesn't work:
temp_class = [i for i in temperatures:
if i < -2:
print('Cold')
elif i >= -2 and i <= 2:
print('Slippery')
elif i >2 and i < 15:
print('Comfortable')
else:
print('Warm')]
But get this error:
File "", line 1
temp_class = [i for i in temperatures:
^
SyntaxError: invalid syntax
What would be the correct code to:
1. Get a variable from my statement
2. Get both the temperatures and classes in a table similar to a tibble or data.frame in R.
Thanks
If you goal is to get these strings into temp_class just append them instead of print
temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4]
temp_class = []
for i in temperatures:
if i < -2:
temp_class.append('Cold')
elif i >= -2 and i <= 2:
temp_class.append('Slippery')
elif i >2 and i < 15:
temp_class.append('Comfortable')
else:
temp_class.append('Warm')
print(temp_class)
# ['Cold', 'Slippery', 'Slippery', 'Cold', 'Comfortable', 'Slippery', 'Cold']
You can create a function and use it in your list comprehension:
temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4]
def feeling(temp):
if temp < -2:
return 'Cold'
elif -2 < temp <= 2:
return 'Slippery'
elif 2 < temp < 15:
return 'Comfortable'
else:
return 'Warm'
[feeling(temp) for temp in temperatures]
# ['Cold', 'Slippery', 'Slippery', 'Cold', 'Comfortable', 'Slippery', 'Cold']
Using map()
temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4]
def get_temp_class(i):
if i < -2:
return 'Cold'
elif i >= -2 and i <= 2:
return 'Slippery'
elif i >2 and i < 15:
return 'Comfortable'
else:
return 'Warm'
temp_class = map(get_temp_class, temperatures)

Finding the word version of a number [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Project Euler 17
So for this problem here of project euler:
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
I tried to figure this out myself with no help... but I think I am doing something wrong...
I have this code...
def convert(n):
if n < 10:
return placement(n,1)
elif n == 10:
return "ten"
elif n > 10 and n < 20:
return teen(n)
elif n >= 20 and n < 100:
if int(str(n)[1]) == 0:
return placement(str(n)[0],2)
else:
return placement(str(n)[0],2)+convert(int(str(n)[1]))
elif len(str(n)) == 3:
x = ""
h = placement(str(n)[0], 3)
if int(str(n)[1]) == 0 and int(str(n)[2]) == 0:
return h
else:
z = int(str(n)[1]+"0")+int(str(n)[2])
x = h + "and" + str(convert(z))
return x
elif len(str(n)) == 4:
x = ""
t = placement(str(n)[0], 4)
if int(str(n)[1]) == 0 and int(str(n)[2]) == 0 and int(str(n)[3]) == 0:
return t
def teen(n):
n = int(n)
if n == 11:
return "eleven"
if n == 12:
return "twelve"
if n == 13:
return "thirteen"
if n == 14:
return "fourteen"
if n == 15:
return "fifteen"
if n == 16:
return "sixteen"
if n == 17:
return "seventeen"
if n == 18:
return "eighteen"
if n == 19:
return "nineteen"
else:
return ""
def placement(n,p):
n = int(n)
if p == 1:
if n == 1:
return "one"
if n == 2:
return "two"
if n == 3:
return "three"
if n == 4:
return "four"
if n == 5:
return "five"
if n == 6:
return "six"
if n == 7:
return "seven"
if n == 8:
return "eight"
if n == 9:
return "nine"
else:
return ""
if p == 2:
if n == 1:
return "ten"
if n == 2:
return "twenty"
if n == 3:
return "thirty"
if n == 4:
return "fourty"
if n == 5:
return "fifty"
if n == 6:
return "sixty"
if n == 7:
return "seventy"
if n == 8:
return "eighty"
if n == 9:
return "ninety"
else:
return ""
if p == 3:
if n != 0:
return placement(n,1) + "hundred"
else:
return ""
if p == 4:
if n != 0:
return placement(n,1) + "thousand"
else:
return ""
z = 0
for x in range(1,1001):
z += len(convert(x))
print z
This spits out the answer 21224 but project euler says its wrong, so does anyone see whats wrong with what I am doing? and does anyone have a better way of doing this, if you do can you please explain it instead of just giving me some code?
sorry for this long bit... but if I print each line I get this...
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twentyone
twentytwo
twentythree
twentyfour
twentyfive
twentysix
twentyseven
twentyeight
twentynine
thirty
thirtyone
thirtytwo
thirtythree
thirtyfour
thirtyfive
thirtysix
thirtyseven
thirtyeight
thirtynine
fourty
fourtyone
fourtytwo
fourtythree
fourtyfour
fourtyfive
fourtysix
fourtyseven
fourtyeight
fourtynine
fifty
fiftyone
fiftytwo
fiftythree
fiftyfour
fiftyfive
fiftysix
fiftyseven
fiftyeight
fiftynine
sixty
sixtyone
sixtytwo
sixtythree
sixtyfour
sixtyfive
sixtysix
sixtyseven
sixtyeight
sixtynine
seventy
seventyone
seventytwo
seventythree
seventyfour
seventyfive
seventysix
seventyseven
seventyeight
seventynine
eighty
eightyone
eightytwo
eightythree
eightyfour
eightyfive
eightysix
eightyseven
eightyeight
eightynine
ninety
ninetyone
ninetytwo
ninetythree
ninetyfour
ninetyfive
ninetysix
ninetyseven
ninetyeight
ninetynine
onehundred
onehundredandone
onehundredandtwo
onehundredandthree
onehundredandfour
onehundredandfive
onehundredandsix
onehundredandseven
onehundredandeight
onehundredandnine
onehundredandten
onehundredandeleven
onehundredandtwelve
onehundredandthirteen
onehundredandfourteen
onehundredandfifteen
onehundredandsixteen
onehundredandseventeen
onehundredandeighteen
onehundredandnineteen
onehundredandtwenty
onehundredandtwentyone
onehundredandtwentytwo
onehundredandtwentythree
onehundredandtwentyfour
onehundredandtwentyfive
onehundredandtwentysix
onehundredandtwentyseven
onehundredandtwentyeight
onehundredandtwentynine
onehundredandthirty
onehundredandthirtyone
onehundredandthirtytwo
onehundredandthirtythree
onehundredandthirtyfour
onehundredandthirtyfive
onehundredandthirtysix
onehundredandthirtyseven
onehundredandthirtyeight
onehundredandthirtynine
onehundredandfourty
onehundredandfourtyone
onehundredandfourtytwo
onehundredandfourtythree
onehundredandfourtyfour
onehundredandfourtyfive
onehundredandfourtysix
onehundredandfourtyseven
onehundredandfourtyeight
onehundredandfourtynine
onehundredandfifty
onehundredandfiftyone
onehundredandfiftytwo
onehundredandfiftythree
onehundredandfiftyfour
onehundredandfiftyfive
onehundredandfiftysix
onehundredandfiftyseven
onehundredandfiftyeight
onehundredandfiftynine
onehundredandsixty
onehundredandsixtyone
onehundredandsixtytwo
onehundredandsixtythree
onehundredandsixtyfour
onehundredandsixtyfive
onehundredandsixtysix
onehundredandsixtyseven
onehundredandsixtyeight
onehundredandsixtynine
onehundredandseventy
onehundredandseventyone
onehundredandseventytwo
onehundredandseventythree
onehundredandseventyfour
onehundredandseventyfive
onehundredandseventysix
onehundredandseventyseven
onehundredandseventyeight
onehundredandseventynine
onehundredandeighty
onehundredandeightyone
onehundredandeightytwo
onehundredandeightythree
onehundredandeightyfour
onehundredandeightyfive
onehundredandeightysix
onehundredandeightyseven
onehundredandeightyeight
onehundredandeightynine
onehundredandninety
onehundredandninetyone
onehundredandninetytwo
onehundredandninetythree
onehundredandninetyfour
onehundredandninetyfive
onehundredandninetysix
onehundredandninetyseven
onehundredandninetyeight
onehundredandninetynine
twohundred
twohundredandone
twohundredandtwo
twohundredandthree
twohundredandfour
twohundredandfive
twohundredandsix
twohundredandseven
twohundredandeight
twohundredandnine
twohundredandten
twohundredandeleven
twohundredandtwelve
twohundredandthirteen
twohundredandfourteen
twohundredandfifteen
twohundredandsixteen
twohundredandseventeen
twohundredandeighteen
twohundredandnineteen
twohundredandtwenty
twohundredandtwentyone
twohundredandtwentytwo
twohundredandtwentythree
twohundredandtwentyfour
twohundredandtwentyfive
twohundredandtwentysix
twohundredandtwentyseven
twohundredandtwentyeight
twohundredandtwentynine
twohundredandthirty
twohundredandthirtyone
twohundredandthirtytwo
twohundredandthirtythree
twohundredandthirtyfour
twohundredandthirtyfive
twohundredandthirtysix
twohundredandthirtyseven
twohundredandthirtyeight
twohundredandthirtynine
twohundredandfourty
twohundredandfourtyone
twohundredandfourtytwo
twohundredandfourtythree
twohundredandfourtyfour
twohundredandfourtyfive
twohundredandfourtysix
twohundredandfourtyseven
twohundredandfourtyeight
twohundredandfourtynine
twohundredandfifty
twohundredandfiftyone
twohundredandfiftytwo
twohundredandfiftythree
twohundredandfiftyfour
twohundredandfiftyfive
twohundredandfiftysix
twohundredandfiftyseven
twohundredandfiftyeight
twohundredandfiftynine
twohundredandsixty
twohundredandsixtyone
twohundredandsixtytwo
twohundredandsixtythree
twohundredandsixtyfour
twohundredandsixtyfive
twohundredandsixtysix
twohundredandsixtyseven
twohundredandsixtyeight
twohundredandsixtynine
twohundredandseventy
twohundredandseventyone
twohundredandseventytwo
twohundredandseventythree
twohundredandseventyfour
twohundredandseventyfive
twohundredandseventysix
twohundredandseventyseven
twohundredandseventyeight
twohundredandseventynine
twohundredandeighty
twohundredandeightyone
twohundredandeightytwo
twohundredandeightythree
twohundredandeightyfour
twohundredandeightyfive
twohundredandeightysix
twohundredandeightyseven
twohundredandeightyeight
twohundredandeightynine
twohundredandninety
twohundredandninetyone
twohundredandninetytwo
twohundredandninetythree
twohundredandninetyfour
twohundredandninetyfive
twohundredandninetysix
twohundredandninetyseven
twohundredandninetyeight
twohundredandninetynine
threehundred
threehundredandone
threehundredandtwo
threehundredandthree
threehundredandfour
threehundredandfive
threehundredandsix
threehundredandseven
threehundredandeight
threehundredandnine
threehundredandten
threehundredandeleven
threehundredandtwelve
threehundredandthirteen
threehundredandfourteen
threehundredandfifteen
threehundredandsixteen
threehundredandseventeen
threehundredandeighteen
threehundredandnineteen
threehundredandtwenty
threehundredandtwentyone
threehundredandtwentytwo
threehundredandtwentythree
threehundredandtwentyfour
threehundredandtwentyfive
threehundredandtwentysix
threehundredandtwentyseven
threehundredandtwentyeight
threehundredandtwentynine
threehundredandthirty
threehundredandthirtyone
threehundredandthirtytwo
threehundredandthirtythree
threehundredandthirtyfour
threehundredandthirtyfive
threehundredandthirtysix
threehundredandthirtyseven
threehundredandthirtyeight
threehundredandthirtynine
threehundredandfourty
threehundredandfourtyone
threehundredandfourtytwo
threehundredandfourtythree
threehundredandfourtyfour
threehundredandfourtyfive
threehundredandfourtysix
threehundredandfourtyseven
threehundredandfourtyeight
threehundredandfourtynine
threehundredandfifty
threehundredandfiftyone
threehundredandfiftytwo
threehundredandfiftythree
threehundredandfiftyfour
threehundredandfiftyfive
threehundredandfiftysix
threehundredandfiftyseven
threehundredandfiftyeight
threehundredandfiftynine
threehundredandsixty
threehundredandsixtyone
threehundredandsixtytwo
threehundredandsixtythree
threehundredandsixtyfour
threehundredandsixtyfive
threehundredandsixtysix
threehundredandsixtyseven
threehundredandsixtyeight
threehundredandsixtynine
threehundredandseventy
threehundredandseventyone
threehundredandseventytwo
threehundredandseventythree
threehundredandseventyfour
threehundredandseventyfive
threehundredandseventysix
threehundredandseventyseven
threehundredandseventyeight
threehundredandseventynine
threehundredandeighty
threehundredandeightyone
threehundredandeightytwo
threehundredandeightythree
threehundredandeightyfour
threehundredandeightyfive
threehundredandeightysix
threehundredandeightyseven
threehundredandeightyeight
threehundredandeightynine
threehundredandninety
threehundredandninetyone
threehundredandninetytwo
threehundredandninetythree
threehundredandninetyfour
threehundredandninetyfive
threehundredandninetysix
threehundredandninetyseven
threehundredandninetyeight
threehundredandninetynine
fourhundred
fourhundredandone
fourhundredandtwo
fourhundredandthree
fourhundredandfour
fourhundredandfive
fourhundredandsix
fourhundredandseven
fourhundredandeight
fourhundredandnine
fourhundredandten
fourhundredandeleven
fourhundredandtwelve
fourhundredandthirteen
fourhundredandfourteen
fourhundredandfifteen
fourhundredandsixteen
fourhundredandseventeen
fourhundredandeighteen
fourhundredandnineteen
fourhundredandtwenty
fourhundredandtwentyone
fourhundredandtwentytwo
fourhundredandtwentythree
fourhundredandtwentyfour
fourhundredandtwentyfive
fourhundredandtwentysix
fourhundredandtwentyseven
fourhundredandtwentyeight
fourhundredandtwentynine
fourhundredandthirty
fourhundredandthirtyone
fourhundredandthirtytwo
fourhundredandthirtythree
fourhundredandthirtyfour
fourhundredandthirtyfive
fourhundredandthirtysix
fourhundredandthirtyseven
fourhundredandthirtyeight
fourhundredandthirtynine
fourhundredandfourty
fourhundredandfourtyone
fourhundredandfourtytwo
fourhundredandfourtythree
fourhundredandfourtyfour
fourhundredandfourtyfive
fourhundredandfourtysix
fourhundredandfourtyseven
fourhundredandfourtyeight
fourhundredandfourtynine
fourhundredandfifty
fourhundredandfiftyone
fourhundredandfiftytwo
fourhundredandfiftythree
fourhundredandfiftyfour
fourhundredandfiftyfive
fourhundredandfiftysix
fourhundredandfiftyseven
fourhundredandfiftyeight
fourhundredandfiftynine
fourhundredandsixty
fourhundredandsixtyone
fourhundredandsixtytwo
fourhundredandsixtythree
fourhundredandsixtyfour
fourhundredandsixtyfive
fourhundredandsixtysix
fourhundredandsixtyseven
fourhundredandsixtyeight
fourhundredandsixtynine
fourhundredandseventy
fourhundredandseventyone
fourhundredandseventytwo
fourhundredandseventythree
fourhundredandseventyfour
fourhundredandseventyfive
fourhundredandseventysix
fourhundredandseventyseven
fourhundredandseventyeight
fourhundredandseventynine
fourhundredandeighty
fourhundredandeightyone
fourhundredandeightytwo
fourhundredandeightythree
fourhundredandeightyfour
fourhundredandeightyfive
fourhundredandeightysix
fourhundredandeightyseven
fourhundredandeightyeight
fourhundredandeightynine
fourhundredandninety
fourhundredandninetyone
fourhundredandninetytwo
fourhundredandninetythree
fourhundredandninetyfour
fourhundredandninetyfive
fourhundredandninetysix
fourhundredandninetyseven
fourhundredandninetyeight
fourhundredandninetynine
fivehundred
fivehundredandone
fivehundredandtwo
fivehundredandthree
fivehundredandfour
fivehundredandfive
fivehundredandsix
fivehundredandseven
fivehundredandeight
fivehundredandnine
fivehundredandten
fivehundredandeleven
fivehundredandtwelve
fivehundredandthirteen
fivehundredandfourteen
fivehundredandfifteen
fivehundredandsixteen
fivehundredandseventeen
fivehundredandeighteen
fivehundredandnineteen
fivehundredandtwenty
fivehundredandtwentyone
fivehundredandtwentytwo
fivehundredandtwentythree
fivehundredandtwentyfour
fivehundredandtwentyfive
fivehundredandtwentysix
fivehundredandtwentyseven
fivehundredandtwentyeight
fivehundredandtwentynine
fivehundredandthirty
fivehundredandthirtyone
fivehundredandthirtytwo
fivehundredandthirtythree
fivehundredandthirtyfour
fivehundredandthirtyfive
fivehundredandthirtysix
fivehundredandthirtyseven
fivehundredandthirtyeight
fivehundredandthirtynine
fivehundredandfourty
fivehundredandfourtyone
fivehundredandfourtytwo
fivehundredandfourtythree
fivehundredandfourtyfour
fivehundredandfourtyfive
fivehundredandfourtysix
fivehundredandfourtyseven
fivehundredandfourtyeight
fivehundredandfourtynine
fivehundredandfifty
fivehundredandfiftyone
fivehundredandfiftytwo
fivehundredandfiftythree
fivehundredandfiftyfour
fivehundredandfiftyfive
fivehundredandfiftysix
fivehundredandfiftyseven
fivehundredandfiftyeight
fivehundredandfiftynine
fivehundredandsixty
fivehundredandsixtyone
fivehundredandsixtytwo
fivehundredandsixtythree
fivehundredandsixtyfour
fivehundredandsixtyfive
fivehundredandsixtysix
fivehundredandsixtyseven
fivehundredandsixtyeight
fivehundredandsixtynine
fivehundredandseventy
fivehundredandseventyone
fivehundredandseventytwo
fivehundredandseventythree
fivehundredandseventyfour
fivehundredandseventyfive
fivehundredandseventysix
fivehundredandseventyseven
fivehundredandseventyeight
fivehundredandseventynine
fivehundredandeighty
fivehundredandeightyone
fivehundredandeightytwo
fivehundredandeightythree
fivehundredandeightyfour
fivehundredandeightyfive
fivehundredandeightysix
fivehundredandeightyseven
fivehundredandeightyeight
fivehundredandeightynine
fivehundredandninety
fivehundredandninetyone
fivehundredandninetytwo
fivehundredandninetythree
fivehundredandninetyfour
fivehundredandninetyfive
fivehundredandninetysix
fivehundredandninetyseven
fivehundredandninetyeight
fivehundredandninetynine
sixhundred
.....
ninehundredandninetyseven
ninehundredandninetyeight
ninehundredandninetynine
onethousand
It looks right to me...
If I test it with their examples using for x in range(1, 6) comes out with 19
If I do len(convert(115)) it says 20 characters, but if I do len(convert(342)) it comes out with 24 and not 23... so what did I do wrong?
It looks like you need a dash between your *ty word (twenty, thirty, etc) and the one's digit (one, two, etc). For example, I believe from the description that you should be getting "twenty-one" instead of "twentyone" (which is not a word by the way).
You probably also want to change "fourty" to "forty".
As already noted, the word for 40 is forty, not fourty.
Note that the long series of if statements that check for about 30 separate cases can be replaced by array references. This will make the program half as long.
For instance, replace your teen and placement functions with the following:
def teen(n):
n = int(n)
if n//10 == 1:
return ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"][n-10]
else:
return ""
def placement(n,p):
n = int(n)
if n < 1:
return ""
if p == 1:
return ["one","two","three","four","five","six","seven","eight","nine"][n-1]
if p == 2:
return ["ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"][n-1]
if p == 3:
return placement(n,1) + "hundred"
if p == 4:
return placement(n,1) + "thousand"
Also, replace the first 6 lines of convert with:
def convert(n):
if n < 10:
return placement(n,1)
if n < 20:
return teen(n)
if n < 100:

Categories

Resources