0

so I'm currently trying to print a list of cards in a text based card battler I'm making for a school project, and I'm wondering if I can get some help. I'm trying to print something different if a line in a file is 0 or 1, but I can't figure it out. thanks if you can help

def mainfunc():
 while i<cardlist:
  #if it's zero, do this
   print("the card this line represents")
  #if it's one, do this
   print("locked")
  #else, becasue if it's else then you're at the end of the file
   print("your deck:")
   #print your current deck
   print("which card do you want to add?")
   print(filelinecount("RIPScards"))
  • Function definitionis wrong: ```def mainfunc```, it should be ```def mainfunc()```. And why don't you use ```if...else``` –  Aug 04 '21 at 02:40
  • @sujay yeah I can fix the func, but the if... else is what I'm using, and it's not working. the main problem is checking all the lines in a row. also I'm new to posting here so I'm not sure on the etiquette – Sam Larkin Aug 04 '21 at 02:43

1 Answers1

0

This is what I would do (UPDATED):

# For preventing unwanted behavior if this file ever gets imported in another:
if __name__ == "__main__":
    with open(**insert file path here**, 'r') as f:
        for line in f:
           if line.strip() == "0":
              print("the card this line represents")
           elif line.strip() == "1":
              print("locked")
           else:
             print("your deck")
             print("which card do you want to add?")
             print(filelinecount("RIPScards"))

You can read a file line-by-line with it open like so:

with open(**file path**, 'r') as f:
    for line in f:
       # Do stuff with the line here

Or you can read all the lines one time and close the file, then do stuff with the lines, like so:

f = open(**file name here**, 'r')
$lines = f.readlines()
f.close() # Make sure to close it, 'with open' method automatically does that for you, but manually opening it (like in this example) will not do it for you!

# Do stuff with it:
for line in lines:
    # Do something with the line

Hopefully that helps!

TwoFace
  • 101
  • 4
  • this answer is a lot nicer than what i was trying to use tbh, but I'm still confused about one thing, though it might seem stupid. what does "if __name === "__main__": mean? – Sam Larkin Aug 04 '21 at 02:53
  • 1
    [What does if __name__ == “__main__”: do?](https://stackoverflow.com/q/419163/15497888) – Henry Ecker Aug 04 '21 at 02:55
  • Essentially python stores a variable called '\_\_name__' which holds module name for that file. If you directly invoked the script, then '\_\_name__' will be set to "\_\_main__". Now this seems like a trivial thing to keep track of, but this can be critical if you ever plan to use any functions or classes in another python file. If you aren't planning to expand your code into multiple files then technically you don't need that if check, but it is common practice to do so. I don't have enough chars to explain more: see more [here](https://www.freecodecamp.org/news/if-name-main-python-example/) – TwoFace Aug 04 '21 at 03:00
  • @TwoFace hey, coming back after a while i've just realised that this answer doesn't actually solve the problem. the main issue is in checking ALL lines. this would work for one line, but has no way to work on more lines – Sam Larkin Aug 04 '21 at 04:19
  • @SamLarkin I apologize: I just updated my response to reflect editing an entire file line-by-line. – TwoFace Aug 04 '21 at 15:23
  • thanks bro, this was really helpful, thanks for updating it too. – Sam Larkin Aug 06 '21 at 01:44