Reputation: 57
I'm trying to answer a question from a challenge.
Question: Some numbers have personality, while other numbers are just dull. Let's write a program that asks for a list of numbers and describes the interesting ones using the personality traits below, while counting up all the dull ones without traits.
Your program should ask for a list of space-separated numbers. For any number with at least one trait, it should print a full description of its personality. Then it should print how many dull numbers were counted.
Here's my solution for it:
arrogant_numbers = [3, 6, 7, 23, 25, 35, 39, 66, 68, 112, 119, 254, 259, 732, 737, 4565, 4663, 13330, 13730, 29880, 29998, 79670, 80015, 230054, 239068, 1534301, 1607352, 2060587, 21700891, 99167753, 99873125]
def find_personality(number):
traits = ''
# Find the traits of the number
if number % 2 != 0:
traits = traits + 'odd '
# Check for other traits after this
if number >10000:
traits = traits + 'excessive '
if '3' in str(number):
traits = traits + 'irksome '
if number in arrogant_numbers:
traits = traits + 'arrogant '
return traits
# Write the rest of your program after this
dullcounter = 0
numbers = input('Enter numbers: ')
number_list = numbers.split()
for word in number_list:
trts = find_personality(int(word))
print(f"{word} is an {trts}number.")
if '' in trts:
dullcounter = dullcounter + 1
print(f'Dull numbers: {dullcounter}')
I'm having problems with counting the dull numbers (the ones that have no traits) it counts all the numbers given instead of the ones that have no traits.
Here's how the final solution should look like:
Here's how my output looks like:
Upvotes: 0
Views: 103
Reputation: 143
Your if condition is always True. Use an alternative condition like this
if len(trts) > 0:
dullcounter = dullcounter + 1
Upvotes: 1
Reputation: 33361
Your output string trts
will always contain ''
.
Instead of this you should validate if trts
is an empty string.
So, instead of
if '' in trts:
dullcounter = dullcounter + 1
you should use this or similar:
if not trts:
dullcounter = dullcounter + 1
Upvotes: 3
Reputation: 83
The problem is that your function always returns a string, albeit an emtpy string if the number is dull. Try introducing logic that breaks out of the function if a number is dull (instead of returning an empty string).
Upvotes: 1