简体   繁体   中英

Exponential function return problem -python

I am working on this problem -

 modify powerOfTwo() to meet the conditions below
#    - accept 1 integer parameter; an exponent
#    - print to the screen 2 raised to the exponent argument, example:
#    --- "2 to the power of 2 is 4"
#    - also return the result (for the example, you would return 4)

my code is as follows:

def powerOfTwo(exp):
    x = int(input("please enter a number for what power of 2: "))
    e = (2**x)
    print("2 to the power of",x, "is: ",e,)



    return (2**x)

When running the program, my professor's code checks my code for accuracy, and I believe it is checking it incorrectly. The program returns the following, which includes my own input of "5":

please enter a number for what power of 2: 5
2 to the power of 5 is:  32
+3 function call w/ correct # of params is present
+2 return value is correct type: <class 'int'>
-- return value is incorrect
Expected:  64
Returned:  32

The expected value, 64, is obviously incorrect. Is something wrong with my code, or with my professors code that is checking my problem? (included below):

q2s = 0
e = random.randint(3,11)
try:
    print("\nq2-1:  Checking powerOfTwo(" + str(e) + ")")
    p2 = powerOfTwo(e)
    print("+3 function call w/ correct # of params is present")
    score += 3
    q2s += 3
    if isinstance(p2, int):
        print("+2 return value is correct type:",type(p2))
        score += 2
        q2s += 2
        if p2 == (2 ** e):
            print("+2 return value is correct:",p2)
            score += 2
            q2s += 2
        else:
            print("-- return value is incorrect")
            print("Expected: ",2 ** e)
            print("Returned: ",p2)

        e = random.randint(12,22)
        print("\nq2-2:  Checking powerOfTwo(" + str(e) + ")")
        p2 = powerOfTwo(e)
        if p2 == (2**e):
            print("+2 return value is correct:",p2)
            score += 2
            q2s += 2
        else:
            print("-- return value is incorrect")
            print("Expected: ",2**e)
            print("Returned: ",p2)
    else:
        print("-- return value is incorrect type:",type(p2))
except:
    print("**Something is wrong with powerOfTwo()")
    printErr()

Your professor's program generates a random number which is input to your function. So you can't use 'input' to type your own number. You have to use the value 'exp'.

Your professor's code generates a random number and puts it into the function. In order to check that the function is running correctly, you just need to enter the random number that is generated.

Additionally, it seems that you haven't initialized score in what you provided:

q2s = 0
e = random.randint(3,11)
score=0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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