is there any advantage to placing it after main? I mean is there any particular reason why it would have been after?
is there any advantage to placing it after main? I mean is there any particular reason why it would have been after?
Hi
Im pretty new to the whole C++ programming language but after learning python and java i decided to give it a go. I have been using tutorials at www.cplusplus.com and that has been working fine but i came across a bit of code today i couldn't understand. It went like this:
// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
string mystr;
mine.title = "2001 A Space Odyssey";
mine.year = 1968;
cout << "Enter title: ";
getline (cin,yours.title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> yours.year;
cout << "My favorite movie is:\n ";
printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
My problem is where is goes void printmovie (movies_t movie);
and there is nothing after it and then later in the program is does the same thing again but this time there is an actual function defined. My question is, why would the person who made this have put that void printmovie (movies_t movie);
without having any actual code defined until way later in the program.
Thanks. :)
Wow vega, thats brilliant. I never thought of doing things like that. Thanks a lot, i have the rather large task in front of me that is understanding the whole thing. So thanks again, i think thats all i need.
Just a tip. If you want other people to be able to play this game you need to fix a few things. The one that instantly comes to mind is that you spell beggar like "begger" when the actuall spelling is beggar. For this reason some people will find it more difficult to play your game.
and the problem is on line 467. There you say
if prompt == 'look around' or 'look':
#do stuff
The problem is that python will check if prompt is equal to "look around" and then for the next one is does not check if prompt is equal to look. If you wanted this to work then you would have to go
if prompt == "look around" or prompt == "look":
Cause at the moment it checks the first statement and it returns false because prompt does not equal "look around" but then seeing there is nothing being compared to "look" the program returns True becuase "Look" is not an empty string and therefore returns true in the case of an if statement when nothing is being compared.
What functions dont work? I mean there are a lot of functions you need to be a bit more specific then to just say my functions dont work. Do all of them not work, or just one or two?
okay, i know this is not exactly what you asked but in your code i noticed a lot of:
print ""
You dont need to do that as just going
print
Without the "" marks will do the exact same thing. Or you could use
print """\n\nIn this game you will live your life in a completely text based world, the world of Tekstovia."""
and that will stop the need for those other prints.
Also, put this whole thing through some kind of spellchecker because there are so many spelling mistakes.
dull lights can make it hard to see
im afraid of most things with more then 6 legs!
Do you think that ice-cream is the best food ever?
Windows Crashes
But linux is much _______
ingot --> harry Potter
ha ha ha! Just need a license first!
Are you scared of heights?
Hi
I have been mucking around with VPython for a bit now and i can use it for basic 3D Python but i have not yet worked out how to do things that arent the usual, add square, add arrow, add sphere.
So i was wondering if it was even possible to do things in VPython such as importing 3D models that you could use as a world terrain or even just adding textures to things like the square and circle? Either of those would be really nice.
If you could help that would be wonderful.
Thanks
Well no matter i can still get to it. I just tried again and also checked the Down for everyone or just me and got
It's just you. www.diveintopython.org is up.
So yeah sounds quite strange...
I think the best thing you can do for the first question is just ask us becuase i really dont think there is any IDE in python that will do that.
just wanted to say that i found that that www.diveintopython.org was not down so you can use that if you want.
Have you had a look at the wx.ProgressBar? Becuase in the Docs and Demos they show a way to do just that. You might have to decipher the un-needed code though.
another way to do it is to Destroy the image every time so the code would look like this:
if self.image:
self.image.Destroy()
self.image = wx.StaticBitmap(image, etc. etc.)
Well is ProgressBar inside a class in the module cause then you might have to go:
import mod1
mod1.class.ProgressBar()
couldnt you just import mod1 into your mod2? That way you could then access all methods.
Thanks, that worked a charm. The only thing i was missing was the "" around the $(FULL_CURRENT_PATH. But thanks anyway.
Problem Solved!
the asterix means a parameter that will catch all parameters that arent used in the first ones (if there are any) and all the left over parameters that are given to the method are then made into a list. For a reference see here:
http://www.pasteur.fr/recherche/unites/sis/formation/python/ch06s06.html
Hi
i have been spending the last few days deciding on an IDE that would let me program in Java, C++ and python and notepad++ looked pretty good. The only issue is i cant work out how to make the program run once i have made it. I looked in google and i couldn't find any tutorials to set it for python but if anyone could help that would be greatly appreciated.
Thanks!
The problem is that loop1 exists in two places. In the global scope and in the function scope. This means that the loop1 in the function is different to the loop1 in the main body. To get around this you can add global loop1
to your program.
that just makes sure the program knows where to look to find the variables. So if more than one thing was in the formatted string you would use a tuple.
This is a direct quote from
http://en.wikibooks.org/wiki/Python_Programming/Exceptions
Custom Exceptions
Code similar to that seen above can be used to create custom exceptions and pass information along with them. This can be extremely useful when trying to debug complicated projects. Here is how that code would look; first creating the custom exception class:
class CustomException(Exception): def __init__(self, value): self.parameter = value def __str__(self): return repr(self.parameter)
And then using that exception:
try: raise CustomException("My Useful Error Message") except CustomException, (instance): print "Caught: " + instance.parameter
By tk you dont mean Tkinter do you? Because that is packaged with python.
hey, i did this. This works fine for me.
dic = {'like you': 'like me', "You're": 'I am', 'You are': 'I am', 'She was': 'she was',
'you are': 'I am', 'about you': 'about me', 'am': 'are', 'she was': 'she was',
'yourself': 'myself', 'are': 'am', 'You': 'I', 'Was': 'were', 'your': 'my',
'keep you': 'keep me', 'You and I are': 'We are', "you're": 'I am', "I've": 'you have',
'stop you': 'stop me', 'hates you': 'hates me', 'Are we': 'are we', 'Are they': 'are they',
'You and I': 'we', 'You and me': 'we', 'loves you': 'loves me', 'They are': 'they are',
'hate you': 'hate me', 'you': 'I', 'was': 'were', 'see you': 'see me', 'likes you': 'likes me',
'Me and you': 'we', 'we are': 'we are', ' I ': 'you', 'for you': 'for me', 'me and you': 'us',
'mine': 'yours', 'We are': 'we are', 'love you': 'love me', 'thanks you': 'thanks me',
'you and me': 'us', 'Were': 'was', "I'm": 'you are', 'My': 'your', 'me': 'you', 'He was': 'he was',
'myself': 'yourself', 'hit you': 'hit me', 'they are': 'they are', 'Am': 'are', 'yours': 'mine',
'thank you': 'thank me', 'he was': 'he was', 'at you': 'at me', 'Are': 'am', 'were': 'was',
'to you': 'to me', 'with you': 'with me', 'my': 'your', 'Your': 'my', 'you and I': 'we'}
line = raw_input("Enter your line")
def changeLine(line):
line = line.lower()
for f in dic.keys():
f = f.lower()
if f in line:
index = line.index(f)
new = dic[f]
line = line[:index]+new.upper()+line[index+len(dic[f])+1:]
return line
line = changeLine(line)
print "Line:",line
Also note that wxPython has some issues on the mac so be careful.
why are you using a list not a dictionary?
Why use the getattr? I think gribs way of doing it probably is what you want.
Are you using wxPython?
In that case the event you are looking for is: wx.EVT_LEFT_DOWN
What do you want re-arranged. That piece of code looks perfect.
Thanks guys, i have started to learn it, i really like it. It is easy to understand and quite intuative. Thanks for pointing it out Vega.
Is VPython good with 3D?
Hi
I have fiddled around with other GUI's such as pygame and wxPython but i wanted to use something a little more powerful when it came to 3D objects and i saw what PyOpenGl can do. But i spent ages looking for any help but i couldn't find any place that had a beginners tutorial in PyOpenGl. I was wondering if anyone on DaniWeb knew where i could find some.
Thanks
wait im wrong. Sorry didnt read it properly!
But i did have a quick go and this works:
def line_sum_and_count(line):
"""Finds the sum and count of the numbers in the given line of text."""
#-----------------------------------------------------
split = line.split()
count = 0
sum=0
for f in split:
count+=1
sum += float(f)
#-------------------------------------------------------
return sum, count
def test_line_sum_and_count():
"""Tester for the above function """
sum, count = line_sum_and_count('14.0 20.0 17')
if sum == 51 and count == 3:
print "passed"
else:
print "failed"
test_line_sum_and_count()
The reason it will say failed at the end of the code is becuase sum and count only exist inside your method. To stop that you can add global sum
and global count
to the start of the code.
Jargon is programming terms such as variables, definitions, modules. At least thats how i understand it to be.
Yeah i ended up finding it in:
C:\Program Files\wxPython2.8 Docs and Demos\demo\
So thats all worked out. Thanks Guys!
what do you need to add?
Has anyone heard of happy numbers?
Well wikipedia says this about them:
Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers.
For example:
23
2**2 = 4
3**3 = 9
4+9=13
1**1=1
3**3=9
1+9=10
1**1=1
0**0=0
0+1=1
So therefore 23 IS a happy number.
Try and make a program that can do two things:
Oh i also was wondering where do you find the code for such things as that Main module? Is it in the program files?
sys.argv is the arguments you give to the program when you run it from the command line. For example:
C:\Python> python image.py This_is_arg_0 This_is_arg_1
Hope that helps. What i think you have to do is give the program a location of some images to convert. That seems the most likely thing.
So should i just import that little bit of code into my project to load my xrc files?
It is just found in the wxPython demo so it might be a custom thing. I noticed it was to open a file. But when i replaced this:
text = opj('data.xrc')
with this:
text= open('data.xrc')
It gave me some sort of error where is said it wanted a string or unicode object not a file. So is there any way to open an object (file) and make it a string.
What i want to do with this is have wxPython read from xml files and make a GUI from that. It is in the Demo so i just wanted to do it myself. If you have any pointers for that as well that would be good.
Thanks!
Hi
My problem today is i have opened up the wxPython Demo and copied one of the pieces of code across to IDLE so i can have a fiddle with it. My problem is that at the start it goes from Main import opj
. Thats what stuffs it up. The interpreter goes something like this:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
from Main import opj
ImportError: No module named Main
Any help would be greatly appreciated.
It really depends. Are you using wxPython Tkinter or some other kind of GUI becuase they all differ.
I can give you an example in wxPython
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None)
self.menubar = wx.MenuBar()
self.file = wx.Menu()
self.file.Append(wx.ID_ANY,'&Test')
self.menubar.Append(self.file,'File')
self.SetMenuBar(self.menubar)
self.Show()
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
Hope that helps!
Yeah i definitly agree with steve. Cause what is happening at the moment is that your program makes a BoxSizer and then you instantly overwrite it with a FlexSizer so i would reccomend you get rid of one of them really im just echoing steves words but they are very true.