[Tutor] Tutor Digest, Vol 196, Issue 28

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jun 15 07:16:04 EDT 2020


On 15/06/2020 02:34, Krish P via Tutor wrote:
>  Hi, thanks for your response.
> I not able to generate the Syracuse sequence for each of those numbers in the data.txt file.
> given is the code that I came up with:
> def syr(x):    if x % 2 ==0:        return x/2    else:        return 3*x + 1  def main():    x = 50    print(x*"-") # Line Break (LB)    print("Program use Syracuse sequence to reach 1 from")    print("natural numbers by utilizing a set of data.")    print(x*"-") # LB
>     f = open("data.txt", "r")    print(f.read())
>     main()

You need to post in plain text or the mail server screws up the
indentation as above.

However, even with that I have some questions:

main() isn't really a main function it just prints a banner message,
so calling it print_banner() or somesuch would make more sense.

Where is the code that reads each number (or even a single number!)
from the data file? the f.read() call will read the whole file into
a string but after you print it it disappears because you don't
store it anywhere.

I would expect your code structure to look like:

print banner
open and read data file
for each entry in data
   call syr() function on each data entry
   store and print result

If you don't know how to do any of that let us know.

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list