Python Forum

Full Version: Fixing a problem with file.io
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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')
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.
(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...