Python Forum
Fixing a problem with file.io
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fixing a problem with file.io
#1
Ok so, i am doing a school assignment which is just a little simple text game and I am using files (sublime). For some reason it is not printing what it is meant to in the Sublime file even though earlier on today it was.... Here is my code if someone can help me out would be greatly appreciated.
import random

#SETUP
#LIST
races=['human','troll','undead',]
classes=['mage','barbarian','paladin','healer','ranger']
weapons=['staff of gandalf','longbow','battle axe','wand','war hammer']
damage=['1','2','3','4','5','6','7','8','9','10']

#PLAYERS/OPPONENTS
print('Welcome! You have been enslaved, your only way to escape is to fight one of our deadly hand picked fighters!')
print('Type your name in to start')
name = input('')
print('Welcome', name,)
print('')
print('We will start by picking and opponent.')
print('')
print('Here is a list of our deadliest fighters, be careful who you choose!:')
print('')
opponent1 = 'Opponent ONE is the '+random.choice(races)+" "+random.choice(classes)+' using the legendary '+random.choice(weapons)
opponent2 = 'Opponent TWO is the '+random.choice(races)+" "+random.choice(classes)+' using the legendary '+random.choice(weapons)
opponent3 = 'Opponent THREE is the '+random.choice(races)+" "+random.choice(classes)+' using the legendary '+random.choice(weapons)
opponent4 = 'Opponent FOUR is the '+random.choice(races)+" "+random.choice(classes)+' using the legendary '+random.choice(weapons)

opponent1print = opponent1
opponent2print = opponent2
opponent3print = opponent3
opponent4print = opponent4

print(opponent1)
print(opponent2)
print(opponent3)
print(opponent4)

print('')

print('To chose and opponent, type in either 1, 2, 3 or 4')
print('')

choose = input('')
    
if choose == ('1'):
    print('You chose opponent ONE')

elif choose == ('2'):
    print('You chose opponent TWO')

elif choose == ('3'):
    print('You chose opponent THREE')

elif choose == ('4'):
    print('You chose opponent FOUR')

# 


#FILE IO SETUP - LIST OF EACH OPPONENTS DAMAGE/HEALTH/ABILITIES - Have different files for different opponents.

contents = []

#go get file contents

with open('assignment.txt','r') as myfile:
    line = myfile.read()
    while line !='': 
        thing = line.rstrip('\r\n') 
        contents.append(thing)
        line = myfile.readline()

#see the contents in the file

print('')

if choose == ('1'):
    with open('assignment.txt','w') as target:
        for thing in contents:
            target.write(opponent1print)
if choose == ('2'):
    with open('assignment.txt','w') as target:
        for thing in contents:
            target.write(opponent2print)
if choose == ('3'):
    with open('assignment.txt','w') as target:
        for thing in contents:
            target.write(opponent3print)        
if choose == ('4'):
    with open('assignment.txt','w') as target:
        for thing in contents:
            target.write(opponent4print)
print('file written')
Reply
#2
Can you please elaborate on exactly what the problem is? Example input and output, along with expected output, is best for helping us to help you.
Reply
#3
(Mar-13-2019, 09:05 AM)ThickTac Wrote: micseydel

So, I am not really sure, but I do know that earlier on it was working fine. Opponent1print was adding text like it was supposed to into the sublime file. Shortly after it just randomly stopped being added to the sublime.txt file. I have re downloaded and re installed sublime and python but hasnt fixed issue, code isnt giving me any error messages aswell...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,540 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  Invalid syntax error - need help fixing calgk01 3 3,228 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Fixing a code error Twoshawns 5 2,697 May-14-2020, 11:56 AM
Last Post: Twoshawns
  Help Fixing Code kianwalters05 5 3,887 May-12-2020, 12:13 PM
Last Post: kianwalters05
  Fixing "PermissionError: [Errno 13] Permission denied" puredata 17 71,957 Mar-09-2020, 03:20 PM
Last Post: syssy
  Fixing the code khanhcao 2 79,768 Feb-06-2020, 07:24 AM
Last Post: buran
  Fixing indentation issues. MuntyScruntfundle 9 4,109 Feb-02-2019, 05:55 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020