Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

[Help] Python code wont display output.

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.
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?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by mikehackMay 9, 2022 11:12 AM 2 151 May 9, 2022 11:12 AM
Last Post: mikehack
[C] by FhackMar 19, 2022 04:11 PM 27 972 Mar 19, 2022 04:11 PM
Last Post: focuswow35