简体   繁体   中英

loops, list, python 3.x

trying to create a two column table that uses a loop to read and process. trying to create a list that shows a integer and it square root. I have played with the program several ways but cannot figure out why i am getting an invalid syntax.

"""with open('numbers.txt', 'w') as file:
for i in range(5): #The loop
    number = 0
    while (number < 10) or (number > 50):
        number = int(input("Enter integer {0}:  ".format(i+1)))
    number_str = "{0}\n".format(number)
    file.write(number_str)
file.close()"""

with open('numbers.txt', 'r') as file:

import math 

print ("\nInteger\t\tSquare Root")

while True:
    line = file.readline() 

    if len(line) == 0:
        break

    number = int(line) 
    square_root = math.sqrt(number) 

    print (number, "\t\t\t%.4f" % round(square_root, 4)

file.close()

You are missing a closing bracket on the penultimate line. It should be:

print (number, "\t\t\t%.4f" % round(square_root, 4))

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