0

I have a task in which you need to take numbers from another text file for the legendary fizzbuzz. I previously wrote a program that takes numbers from input. How can I now include reading numbers from another file into it?

print("Fizz:")
fizz = int(input())
print("Buzz:")
buzz = int(input())
print("3 number:")
num = int(input())
i = 1
while i <= num:
    if i % fizz == 0 and i % buzz == 0:
        print("FB")
    elif i % fizz == 0:
        print("F")
    elif i % buzz == 0:
        print("B")
    else:
        print(i)
    i+=1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 2
    I recommend just looking up python input/output and files. This is an introductory topic and lots of content online that shows you how to do this. – Sterling Oct 11 '22 at 19:30
  • There are many tutorials on reading and writing files in Python. SO is not a substitute for learning the basics. – Barmar Oct 11 '22 at 19:30
  • Do you want to [read a file into a list](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list?rq=1)? – OneCricketeer Oct 11 '22 at 19:31
  • Possibly more precise duplicate: [How should I read a file line by line in Python?](/questions/11555468) – Karl Knechtel Oct 11 '22 at 19:36
  • I just can’t understand how to do it right. File contains 20 strings with 3 numbers each. How can I assign first number as fizz, second as buzz and 3 as 3 number. And in each string – Oleksandr Matvieiev Oct 11 '22 at 19:46
  • [ConfigParser](https://docs.python.org/3/library/configparser.html) is built into the standard library that comes with Python. It's designed to read and write [ini](https://en.wikipedia.org/wiki/INI_file) style files. It's a convient way to save and retrieve variables. Look at [this answer](https://stackoverflow.com/a/63019760/9705687) or [this answer](https://stackoverflow.com/a/73420515/9705687) for how to get started. – bfris Oct 11 '22 at 20:27
  • in question you should show what you have in file because all depends on if you have numbers in separated lines or in one line (separated by space or comma) – furas Oct 12 '22 at 09:46

0 Answers0