Sep 1, 2016 04:10 AM
(This post was last modified: Sep 1, 2016 04:40 AM by H4cker193.)
Hello all,
So I have been learning python and have been able to figure out the errors on my own up to this point which is cumbersome. I am following an example in my python book and have followed the code to a T. It is an older python book but should be to 3.5 standards. It is suppose to take numbers in a range and say whether or not the number is perfect, abundant or deficient. Any help would be highly appreciated.
import math
# Classify a range of numbers with respect to perfect, abundant or deficient
# Unless otherwise stated, variables are assumed to be of type int. Rule 4
top_num_str = input('What is the upper number for the range:')
top_num = int(top_num_str)
number = 2
while number < top_num:
divisor = 1
sum_of_divisors = 0
while divisor < number:
if number % divisor == 0:
sum_of_divisors = sum_of_divisors = divisor
divisor = divisor + 1
number +=1
# Classify the number based on its divisor sum
if number == sum_of_divisors:
print(number,'is perfect')
if number< sum_of_divisors:
print(number,'is abundant')
if number > sum_of_divisors:
print(number,'is deficient')
number += 1
I added the "Import Math" at the top thinking that was the issue but it did not help.
After I enter a number it just hangs and does not classify the numbers in the range.
Update: Visual Studio finally told me what was wrong. It says on the line that contains the if number == sum_of_divisors:
That the colon is wrong. But in the book the colon is there. Also if I take it out, it underlines everything in red. Still does not make sense.
So I have been learning python and have been able to figure out the errors on my own up to this point which is cumbersome. I am following an example in my python book and have followed the code to a T. It is an older python book but should be to 3.5 standards. It is suppose to take numbers in a range and say whether or not the number is perfect, abundant or deficient. Any help would be highly appreciated.
import math
# Classify a range of numbers with respect to perfect, abundant or deficient
# Unless otherwise stated, variables are assumed to be of type int. Rule 4
top_num_str = input('What is the upper number for the range:')
top_num = int(top_num_str)
number = 2
while number < top_num:
divisor = 1
sum_of_divisors = 0
while divisor < number:
if number % divisor == 0:
sum_of_divisors = sum_of_divisors = divisor
divisor = divisor + 1
number +=1
# Classify the number based on its divisor sum
if number == sum_of_divisors:
print(number,'is perfect')
if number< sum_of_divisors:
print(number,'is abundant')
if number > sum_of_divisors:
print(number,'is deficient')
number += 1
I added the "Import Math" at the top thinking that was the issue but it did not help.
After I enter a number it just hangs and does not classify the numbers in the range.
Update: Visual Studio finally told me what was wrong. It says on the line that contains the if number == sum_of_divisors:
That the colon is wrong. But in the book the colon is there. Also if I take it out, it underlines everything in red. Still does not make sense.

