Jump to content
  • Advertisement
Sign in to follow this  
biggjoee5790

Is this correct? (PYTHON)

This topic is 4214 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
Advertisement
Seems ok to me. Although by your description it sounds like you should have also concatenated the numbers to the string.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!