2 Replies - 2015 Views - Last Post: 21 November 2007 - 11:14 AM Rate Topic: -----

#1 Jacobpsp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 06-September 07

Please can i ahve help on my text based game

Posted 15 November 2007 - 03:52 PM

Hey, ive just set my self a task to make a text based game which can load a save game file and create one.

Im haveing a problem though.

Here is the programs source code.
 
# input and output functions.
def savefile(tries):
	# Create game save file.
	out_file = open("score.txt", "w")
	out_file.write(tries)
	out_file.close()
	
def readfile():
	# Read pre made file.
	in_file = open("score.txt", "r")
	score = in_file.read()
	in_file.close()
	
print "Welcome to guessgame .v2"
print "1. View previous scores"
print "2. New game"
choice = input("What do you want to do: ")
# if user chooses option 1
if choice == 1:
	readfile()

# if user chooses option 2
elif choice == 2:
	import random
	num = random.randint(0, 99)
	guess = 0
	tries = 1
	while guess != num:
		guess = input("Enter your guess: ")
		if guess < num:
			print "That is too low!"
		elif guess > num:
			print "Too high!"
		tries = tries + 1
			  
print "Correct the random number was, ", num ,". You took:  ", tries, "tries."

choice = 0

print "Do you want to: "
print "1. Save your score"
print "2. Quit"

choice = input("What do you want to do: ")
if choice == 1:
	print "1. Create a new game save file"
	print "2. Save in score file"
	choice = 0
	choice = input("What do you want to do: ")
	if choice == 1:
		savefile()
	   
	else:
		print "Error"



i keep getting an error for the 'savefile()'
i dont understand it though and dont have a clue on what i should fd to fix it.
Please can i have help
.
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Please can i ahve help on my text based game

#2 rockstar_  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 189
  • Joined: 16-October 06

Re: Please can i ahve help on my text based game

Posted 18 November 2007 - 10:24 PM

View PostJacobpsp, on 15 Nov, 2007 - 03:52 PM, said:

i keep getting an error for the 'savefile()'
i dont understand it though and dont have a clue on what i should fd to fix it.
Please can i have help

Jacobpsp-

It's usually a good idea to post the code and the error. Python's tracebacks help out so much. After throwing your code through a python interpreter, your error was this:
Traceback (most recent call last):
  File "file.py", line 51, in <module>
	savefile()
TypeError: savefile() takes exactly 1 argument (0 given)


Here's a breakdown: On line 51, your code calls the function savefile() without providing it with the one parameter you designed it to require. Take a look at line 2:
def savefile(tries):


You didn't provide the function with the number of tries! So I changed line 51 to look like this:
		savefile(tries)


This then threw the error:
Traceback (most recent call last):
  File "test.py", line 51, in <module>
	savefile(tries)
  File "test.py", line 5, in savefile
	out_file.write(tries)
TypeError: argument 1 must be string or read-only character buffer, not in


This means that the file method write() wants a string, not an int, so I changed line 5 to look like this:
	out_file.write(str(tries))


Voila! It works. If you learn to read the python Tracebacks, they'll become your best friend.
Was This Post Helpful? 0
  • +
  • -

#3 Jacobpsp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 06-September 07

Re: Please can i ahve help on my text based game

Posted 21 November 2007 - 11:14 AM

View Postrockstar_, on 18 Nov, 2007 - 10:24 PM, said:

View PostJacobpsp, on 15 Nov, 2007 - 03:52 PM, said:

i keep getting an error for the 'savefile()'
i dont understand it though and dont have a clue on what i should fd to fix it.
Please can i have help

Jacobpsp-

It's usually a good idea to post the code and the error. Python's tracebacks help out so much. After throwing your code through a python interpreter, your error was this:
Traceback (most recent call last):
  File "file.py", line 51, in <module>
	savefile()
TypeError: savefile() takes exactly 1 argument (0 given)


Here's a breakdown: On line 51, your code calls the function savefile() without providing it with the one parameter you designed it to require. Take a look at line 2:
def savefile(tries):


You didn't provide the function with the number of tries! So I changed line 51 to look like this:
		savefile(tries)


This then threw the error:
Traceback (most recent call last):
  File "test.py", line 51, in <module>
	savefile(tries)
  File "test.py", line 5, in savefile
	out_file.write(tries)
TypeError: argument 1 must be string or read-only character buffer, not in


This means that the file method write() wants a string, not an int, so I changed line 5 to look like this:
	out_file.write(str(tries))


Voila! It works. If you learn to read the python Tracebacks, they'll become your best friend.


thanks mate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1