Sign in to follow this  

Is this correct? (PYTHON)

This topic is 3935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys ive been learning python with an online book and the first end of chapter exercise is: "Write a program that gets 2 string variables and 2 integer variables from the user, concatenates (joins them together with no spaces) and displays the strings, then multiplies the two numbers on a new line." here is the program i made, I know having a program multiply a persons age and birth year is useless :) but its just for the sake of the exercise. I want to know if this would be the correct way to make the program that the author is talking about. The program runs fine and works but im not sure if this is the way it is supposed to be written.. thanks FirstName=raw_input("First Name? ") LastName=raw_input("Last Name? ") Age=input("Age? ") YearOfBirth=input("Year Of Birth? ") print FirstName+LastName print Age*YearOfBirth

Share this post


Link to post
Share on other sites
Ahh yes your right, I didnt understand the exercise completely. Thanks for letting me know I scripted it correct though. I was wondering If there was ever more than one way to acheive the same exact program. Like would there be another way to write the same exact prgram that I made? If not, Ill easily be able to tell If my programs are correct by simply seeing if they run perfectly.

Share this post


Link to post
Share on other sites
There is, you can get the input directly from the commandline, and you can check the input more (if this was a more serious application), as it is now I can enter "banana-panic!" as my age ;) it will break on the last line of course.

Share this post


Link to post
Share on other sites
There's almost always going to be more than one way any given program can be expressed in any given programming language. In some cases they will actually have different functionality, and in others you may be expressing identical functionality in a different way. If your program solves the stated program without errors (or handling any errors gracefully) you can general consider it to be a 'correct' solution.

One correct solution may however be 'better' than another. In general:
  • Simpler code which produces the same behavior is better.
  • Code which is more readable is better.
  • Code which produces the same behavior more efficiently is better.
  • Code which is more general/reusable may be better.

All these things of course depend on the problem at hand; for a given problem you may have requirements that force you to sacrifice simpler, more readable code for something more efficient, and if the speed is really neccesary then that's fine in that situation.



As a beginner, your main goal should simply be to produce programs that function correctly -- paying attention to the other things is a good idea, but don't worry about them too much -- until you've learnt more of the language your ability to write things in a more elegent way is limited by the fact that there are features you simply aren't yet aware of, but you'll find that as you get practice the quality of your code will improve.

The program you've made works correctly (with the caveat that it will not gracefully handle bad input), so you can consider it an acceptable solution.

Share this post


Link to post
Share on other sites
ok so at first I should concentrate on making my programs run smoothly, then later when I get better I can explore new ways to script the same program more efficiently? I understand. I have a question though about the Python language. I know its a very capable language and can do many things, I started with it because I heard it was a lot easier for a beginner to learn than C++. I want to know exactly how much you can do in Python? is it almost as capable as C++? I was planning on learning C++ after I mastered Python but until then, I want to be able to do advanced programming with Python such as games and other visual apps. I know I wont be able to do these sort of things anytime soon, but I want to be sure that I will be able to with Python, so that I dont have to rush into learning C++. One of my main goals in programming is video games, so I want to be sure Im heading there :)

[Edited by - biggjoee5790 on March 8, 2007 3:32:17 PM]

Share this post


Link to post
Share on other sites
A very nice thing about Python is that it is fairly easy to call C libraries from it, so if you make a game and a part turns out too slow you can rewrite that in C or C++ and call it from Python. The best of both worlds!

Share this post


Link to post
Share on other sites
the only thing I notice wrong is there's no type checking but it's probably much too early for that, but if you want more of a challenge add it(do things like make sure that age and year of birth are numbers)

Share this post


Link to post
Share on other sites
Ok so although Python is slower than C++, it is fully capable of creating 2d and 3d games? I still definetely want to learn C++ because almost all modern games are coded in C++, but I want to be able to get into game programming even before I learn C++.

And about adding a type check, I havent really learned about that yet so I wasnt sure how to

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
I want to know exactly how much you can do in Python?
Well, some commercial games that have used Python to varying degrees include Toontown Online, EVE Online and Civilization 4. You can check out the Quotes about Python and Python Success Stories pages to get an idea of some other companies and products that have used Python.

As a beginner it's very unlikely that you'd be trying to do anything that Python can't handle for quite some time.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kazgoroth
Quote:
Original post by biggjoee5790
I want to know exactly how much you can do in Python?
Well, some commercial games that have used Python to varying degrees include Toontown Online, EVE Online and Civilization 4. You can check out the Quotes about Python and Python Success Stories pages to get an idea of some other companies and products that have used Python.

As a beginner it's very unlikely that you'd be trying to do anything that Python can't handle for quite some time.


Thats exactly what I wanted to know. But I have some questions about progressing through Python. At what point in learning Python do you think I will have the ability to get into creating programs that are more than just text? Im sure that question is hard to answer because everyone is different, but maybe you can give me an estimate. Dont take this as me being in a rush or anything, because im not. Im just curious as to how long it will take before ill actually be able to start closer to my game programming goals (although I am moving closer right now, learning the bare basics of Python) Hope you can give me some ideas

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
Quote:
Original post by Kazgoroth
Quote:
Original post by biggjoee5790
I want to know exactly how much you can do in Python?
Well, some commercial games that have used Python to varying degrees include Toontown Online, EVE Online and Civilization 4. You can check out the Quotes about Python and Python Success Stories pages to get an idea of some other companies and products that have used Python.

As a beginner it's very unlikely that you'd be trying to do anything that Python can't handle for quite some time.


Thats exactly what I wanted to know. But I have some questions about progressing through Python. At what point in learning Python do you think I will have the ability to get into creating programs that are more than just text? Im sure that question is hard to answer because everyone is different, but maybe you can give me an estimate. Dont take this as me being in a rush or anything, because im not. Im just curious as to how long it will take before ill actually be able to start closer to my game programming goals (although I am moving closer right now, learning the bare basics of Python) Hope you can give me some ideas


Truly, only you can tell when you're ready to move on. But because python is so much easier to progress with than, say , C++, you could try out PyGame and if you understand it then obviously you're ready. If you don't understand it, find the parts that don't make sense and learn about them and then come back.

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
Thats exactly what I wanted to know. But I have some questions about progressing through Python. At what point in learning Python do you think I will have the ability to get into creating programs that are more than just text? Im sure that question is hard to answer because everyone is different, but maybe you can give me an estimate. Dont take this as me being in a rush or anything, because im not. Im just curious as to how long it will take before ill actually be able to start closer to my game programming goals (although I am moving closer right now, learning the bare basics of Python) Hope you can give me some ideas
As you've observed yourself, everyone is different so there isn't really a good universal answer to this one. I know some very good programmers who learnt by fiddling with advanced engines they didn't understand until they learned what everything did, and plenty of others who started off by getting a solid understanding of the language they were using and then started using different libraries and engines. I will say however that in generally most people find it easier to learn these things and actually understand what's going on if they learn thier basics properly first.


I would recommend you stick with text-based excercises that allow you to focus on the language for a while yet, but whenever you feel you're ready you can grab PyGame (or perhaps the Panda3D engine if you're feeling a bit more adventurous and want to try 3d), looking at some of the examples and documentation and seeing if you can figure it out. If you don't understand you can always go back to learning a bit more and try again at a later stage.

You've mentioned that you're reading a book -- my suggestion would be to finish off the book (make sure you do most if not all of the excercises, actually doing them is a much better way to learn than just reading about it) and then give it a try.


nobodynews has given you good advice as well; when you do decide to try out some graphics you almost certainly won't understand everything that's going on immediately, but you can figure out which bits you do understand (your should be able to trace through how the program flows with what you learn about if statements, loops and other flow control for example) and then go back to your book(s) or the online documentation to figure out the bits you don't understand. If you ask some focussed questions about specific parts and people can see that you've done some work with it yourself (just like you did with this thread, you'd already had a go at writing the program) people will usually be quite happy to help you with some of the trickier parts.

Hope that helps. [smile]

Share this post


Link to post
Share on other sites
Yes that definetely helps me a lot. Im going to get a really good beginners Python book that takes you from the basics to advanced. When I have a really good grasp on the language and can make almost all types of text based programs with ease, Ill try to move into the graphics realm. Im guessing the first step would be to take my text based console programs and move them into actual visual windows (you know, with buttons and everything like a windows app) Im not sure if thats the best approach but it seems to make sense.

Share this post


Link to post
Share on other sites

This topic is 3935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this