简体   繁体   English

Python,“返回”外部函数错误

[英]Python, 'return' outside function error

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)". 我正在用C ++编写此代码,并且一直给我一个我不想要的答案,所以我认为我应该用python编写它,所以当我这样做时,给了我错误“ * 'return'outside function([Place on硬盘驱动器],行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"

尝试将class更改为def

You are telling Python that you are defining a class, because of the "class" keyword. 您由于“ class”关键字而告诉Python您正在定义一个类。

However, you instead should be defining a function, using "def". 但是,您应该使用“ 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. 例如,gimmeANumber()应该返回一个数字,但是当前它返回一个字符串。 The function should look like: 该函数应如下所示:

def gimmeANumber():
    while True:
        Number = raw_input("Please can you input a number from 0 to 255\n")
        Number = int(Number)
        if Number < 0 or 255 < Number:
            print "Please enter a valid number\n"
        else:
            return Number

Python has a built-in function to convert from decimal to binary ( bin() ). Python具有内置功能,可以将十进制转换为二进制( bin() )。

number = int(raw_input('Give me a decimal integer, please: '))
print '%d in decimal is equivalent to %s in binary' % (number, bin(number)[2:])

And to convert from any base to decimal, you could do something like this: 要将任何基数转换为十进制,您可以执行以下操作:

number = int(raw_input('Give me a binary integer, please: '))
print '%d in binary is equivalent to %d in binary' % (number, int(number, 2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM