-
def save():
-
try:
-
saves.append(str(level))
-
saves.append(str(stre))
-
saves.append(str(spd))
-
saves.append(str(hp))
-
saves.append(str(maxhp))
-
saves.append(str(exp))
-
saves.append(str(maxexp))
-
saves.append(str(gp))
-
saves.append(str(sword))
-
save = open('gamesave.txt', 'r+')
-
for s in saves:
-
save.write(s)
-
save.write('\n')
-
saves.remove(s)
-
print s
-
print "Save successful."
-
save.close()
-
except ValueError: print "Save unsuccessful."
-
This should append all of those values, all of which are numbers, into the saves list. Then it should open gamesave.txt, and write them into it, and remove them from the list one by one. As it goes through, only a few are actually sent through, and the last 4 are left.
The attachment shows this. The top line is the saves list. The numbers below it are the ones that are written to the text file.
Anyone know why it won't get all the way through the list?
[link removed]
5 2220 bvdet 2,851
Recognized Expert Moderator Specialist -
def save():
-
try:
-
saves.append(str(level))
-
saves.append(str(stre))
-
saves.append(str(spd))
-
saves.append(str(hp))
-
saves.append(str(maxhp))
-
saves.append(str(exp))
-
saves.append(str(maxexp))
-
saves.append(str(gp))
-
saves.append(str(sword))
-
save = open('gamesave.txt', 'r+')
-
for s in saves:
-
save.write(s)
-
save.write('\n')
-
saves.remove(s)
-
print s
-
print "Save successful."
-
save.close()
-
except ValueError: print "Save unsuccessful."
-
This should append all of those values, all of which are numbers, into the saves list. Then it should open gamesave.txt, and write them into it, and remove them from the list one by one. As it goes through, only a few are actually sent through, and the last 4 are left.
The attachment shows this. The top line is the saves list. The numbers below it are the ones that are written to the text file.
Anyone know why it won't get all the way through the list?
During the iteration, items are being removed from the list. Do something like this instead: - >>> myList = [1,2,3,4,5,6,7,8,9,0]
-
>>> for item in myList:
-
... print item
-
... myList.remove(item)
-
...
-
1
-
3
-
5
-
7
-
9
-
>>> myList
-
[2, 4, 6, 8, 0]
-
>>>
-
>>> myList = [1,2,3,4,5,6,7,8,9,0]
-
>>> for item in myList[:]:
-
... print item
-
... myList.remove(item)
-
...
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
-
>>> myList
-
[]
During the iteration, items are being removed from the list. Do something like this instead: - >>> myList = [1,2,3,4,5,6,7,8,9,0]
-
>>> for item in myList:
-
... print item
-
... myList.remove(item)
-
...
-
1
-
3
-
5
-
7
-
9
-
>>> myList
-
[2, 4, 6, 8, 0]
-
>>>
-
>>> myList = [1,2,3,4,5,6,7,8,9,0]
-
>>> for item in myList[:]:
-
... print item
-
... myList.remove(item)
-
...
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
-
>>> myList
-
[]
Thanks that worked very well =]
I have another concern though. I have the game set up in functions so that the switch is easier to read. -
while c != 'q':
-
main()
-
c = raw_input("[A]ttack [S]tore S[t]ats [L]oad Sa[v]e [Q]uit ").lower()
-
clear()
-
if c == 'a':
-
attack()
-
elif c == 's':
-
store()
-
elif c == 't':
-
stats()
-
elif c == 'l':
-
load()
-
elif c == 'v':
-
save()
-
else:
-
if c == 'q':
-
gb = raw_input("Goodbye! Press [Enter]")
-
else:
-
print "Invalid Choice."
-
main has all of the basic variables already defined for later use. When going to attack I get the error: -
Traceback (most recent call last):
-
File "C:\Python25\Tools\custom\game.py", line 210, in <module>
-
attack()
-
File "C:\Python25\Tools\custom\game.py", line 52, in attack
-
if hp > 0:
-
UnboundLocalError: local variable 'hp' referenced before assignment
-
but all the code up to that point in attack was already defined: -
def attack():
-
monName = _choice(monsterName)
-
print "You are attacking a %s" % monName
-
monIndex = monsterName.index(monName)
-
monHP = monsterHP[monIndex]
-
monExp = monsterExp[monIndex]
-
monStr = monsterStr[monIndex]
-
monSpd = monsterSpd[monIndex]
-
monMin = monsterMin[monIndex]
-
monMax = monsterMax[monIndex]
-
-
if sword == 1:
-
stre += 4
-
spd += 3
-
if hp > 0:
-
Why does that error come up?
main has all of the basic variables already defined for later use. When going to attack I get the error: -
Traceback (most recent call last):
-
File "C:\Python25\Tools\custom\game.py", line 210, in <module>
-
attack()
-
File "C:\Python25\Tools\custom\game.py", line 52, in attack
-
if hp > 0:
-
UnboundLocalError: local variable 'hp' referenced before assignment
-
but all the code up to that point in attack was already defined: -
def attack():
-
monName = _choice(monsterName)
-
print "You are attacking a %s" % monName
-
monIndex = monsterName.index(monName)
-
monHP = monsterHP[monIndex]
-
monExp = monsterExp[monIndex]
-
monStr = monsterStr[monIndex]
-
monSpd = monsterSpd[monIndex]
-
monMin = monsterMin[monIndex]
-
monMax = monsterMax[monIndex]
-
-
if sword == 1:
-
stre += 4
-
spd += 3
-
if hp > 0:
-
Why does that error come up?
Hp is NOT defined for your attack function, that is, it is not defined in it OR as a global variable in the file/module. So you get an error. If it IS defined globally, try:
When I try and run that, it says it is invalid syntax.
bartonc 6,596
Recognized Expert Expert
When I try and run that, it says it is invalid syntax.
You'll need to post more of your program in order to get the scope of your variables worked out. Please start a new thread regarding "creating an RPG" or something like that.
Thanks.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Oci-One Kanubi |
last post by:
Everything works fine in Access, but when I double-click on the
resultant Excel files the first one opens correctly, but subsequent
ones, and any other Excel files I try to open, fail to display at all;
I get the Excel window frame with corrupted contents. The only way I
can get decent functionality from Excel is to open Excel from the
Start menu, open each of these new files from the Excel File menu, and
save each one. Thereafter Excel...
|
by: James Fortune |
last post by:
Here is an example of Access creating a single page PDF file. The
text in the textbox is scaled to fit horizontally into a grey box 100
pixels wide that is fontsize pixels high. Clicking the command button
creates the pdf file. Be sure to close Acrobat Reader before creating
another file with the same name. Form code is followed by module
code. No stream compression or pdf linearization is used in this
example. Note that the special...
|
by: musicloverlch |
last post by:
I used to be able to create PDFs, automatically save them to a file,
and mail them out to people. This was on Access 97 with Windows NT.
We've changed to Windows XP and Access 2003. Doesn't work anymore.
Part of the problem is that the users have no access to the registry
and can only save files on the C:\ drive to their My Documents folder.
I found this code on the web, but it doesn't work either. The files
continue to print out of my...
|
by: Patrick.O.Ige |
last post by:
I have a code below and its a PIE & BAR CHART.
The values now are all static but I want to be able to pull the values from
a database.
Can you guys give me some ideas to do this?
Thanks
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Declare your object variables
|
by: asad |
last post by:
Hello friends, how ru all, i have some problem about saving created
thumbnail, following is the code i use for creating thumbnail but thumbnail
was not saved it is on memory which method is used to store created thumbnail
in to hard disk
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1"
%>
<%@ Import Namespace="System.Drawing.Imaging"%>
| |
by: Nathan Sokalski |
last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving, the other in which I close the FileStream afterwards, although both return the same error. Here are the two versions of the code and the errors they each return (NOTE: I rebooted immediately before running each of these versions so that I knew they...
|
by: Kim |
last post by:
Im making a config file to a C# program in XML. I have save and load
functions, but these require a base XML file with its structure to
exsist (Im new to XML, so this was the way I it to work).
Now I want to extend that, so if the base XML file doesnt exsist a
function will make the structure. However this way seem somewhat
troublesome to do it, but it works.
Is there an easier way?
Also is it allowed to use space in the element name ?
|
by: grant |
last post by:
Well, I just downloaded SQL 2005 Express to have a play. Good old Bill
says "up and running in 20 minutes"
Well 5 hrs, 5 reboots and 4 full un-install and re-install later I have
finally got it working. About what I expected really.
Anyway, my question relates to creating user defined functions. I've 10
yrs exp in MS Access and VB, and user ent Manager to create views on
the work server, but havnt done much with functions.
|
by: xZaft |
last post by:
#Game by Todd Lunter
from random import choice as _choice
import random as r
c = 'a'
level = 1
stre = 2
spd = 2
exp = 0
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |