I was writing this code in C++ and it kept giving me an answer that I didn’t want so I thought I should write it in python, so when I did it gave me the error “* ‘return’ outside function ([Place on hard drive], line 7)”. I searched it and found that it was to do with indentions, so I re-wrote that line of code but I still get this error. Also the program it’s self is most likely wrong and I’m not asking for you to fix that but in theory it should work, also it is to convert decimal to binary as a number. Thanks in advance :).
Here’s the code:
class gimmeANumber():
while True:
Number = raw_input("Please can you input a number from 0 to 255\n")
if Number < 0 or 255 < number:
print "Please enter a valid number\n"
else:
return Number
class decToBinary():
Binary_Converter = 128
i = 0
Binary = 0
while True:
Number = gimmeANumber()
ONumber = Number
while (true):
if (Number - Binary_Converter) >= 0:
Number =- Binary_Converter
Binary_Converter /= 2
Binary += 10**7-i
else:
Binary_Converter /= 2
i += 1
if i == 8:
break
print "\nThe origanal number was " + str(ONumber) + " Now it is " + str(Binary) + " in Binary.\n"
You are telling Python that you are defining a class, because of the “class” keyword.
However, you instead should be defining a function, using “def”.
There are a few other things you will need to fix to make this work. For example, gimmeANumber() should return a number, but it currently returns a string. The function should look like: