Hack Forums
[Python] [Help] Python code wont display output. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] [Help] Python code wont display output. (/showthread.php?tid=5392875)



[Help] Python code wont display output. - H4cker193 - Sep 1, 2016

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.


* - bytez - Sep 2, 2016

Is that your real syntax? If so, try making it right

Code
it have to be like this
if(number == sum_of_divisors:)
      print(number is perfect)

Im still at very basics of python but that's the first problem i saw if its true :P

Also im not sure if its cause im noob to python but i find it very hard to understand your code :d

edit: Just tried running the code, it keeps going on loop, im not a math genious but are you sure thats the exact code?