简体   繁体   中英

Iteration in Python, i'm confused

                         #
                         #

print("n", '\t', "2**n")     #table column headings
print("---", '\t', "-----")

for x in range(13):          #generate values for columns
    print(x, '\t', 2 ** x)

I was given this code by my professor and told to modify it with the following instructions.

  1. On line 1, define a function that has one parameter n.
  2. On line 2, write its body: It returns 2 ** n.
  3. Edit line 8 to call your function with x (instead of using the expression
    2** x).
  4. The same table will be produced as before.

I did this, but then the table didn't produce the same result as before.

def squareIt(n):
    return 2 ** n

print("n", '\t', "2**n")     #table column headings
print("---", '\t', "-----")

for x in range(13):          #generate values for columns
    print(x, '\t', x **2)

Please help, as i'm stuck in this chapter.

You didn't call your function that you wrote.

def squareIt(n):
    return 2 ** n

print("n", '\t', "2**n")     #table column headings
print("---", '\t', "-----")

for x in range(13):          #generate values for columns
    print(x, '\t', squareIt(x))

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