Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
I got all the problems fixed but now I have a question. I'm on chapter 5 http://learnpythonthehardway.org/book/ex5.html and I don't get the extra credit. What does %r do? I tried using it instead of using %s but it just does the same thing as %s.

%r is calling: http://docs.python.org/library/functions.html#repr

The distinction between repr (%r) and str (%s) are probably outside your grasp right now. However, the description on the page seems a little lazy.

This explanation may be a bit more helpful, be sure to read the entire 7.1 section:

http://docs.python.org/tutorial/inputoutput.html
Many values, such as numbers or structures like lists and dictionaries, have the same representation using either function. Strings and floating point numbers, in particular, have two distinct representations.
 
I'm getting so frustrated. I'm encountering multiple problems now. First and main problem is that TextWrangler automatically opened a window called "Unix Script Output" and every time I open one of my python programs and run it it builds up. I'll post a video of it ASAP.

Just a heads up - if you do any amount of programming you are going to discover that you spend probably 80% of the time (if you're lucky :p) fighting frustrating problems.

Half of the battle with programming is learning to fight through annoying problems which either make no sense or don't seem to be possible to solve... :)
 
I do understand what I'm doing. I'm using this website http://learnpythonthehardway.org/book/ex5.html for my latest one for my computer specs and stuff. I still have troubles with the last line for the computer specs. Here's the picture
http://imgur.com/qSqMK
And the whole last line's code is
Code:
print "I can add the numbers up for fun too, I can add %s, %s, %s, %s, and %s and my answer is." % (ram, battery_life, screen_size, the_cost, storage_capacity, ram + battery_life +screen_size + the_cost + storage_capacity)
The last line code seems to be fixed because when I try to launch it, it now builds up in the Unix Script Output instead in the error box.

I just saw this one. Well no, you don't understand what you are doing which is why you are learning.

My point was you need to understand what you are typing; you do that by reading the lesson, typing the code that is there and then changing it based on your understanding of the lesson.

The problem you are having isn't a problem at all. You are using a feature of TextWrangler, so I just you read Chapter 12 of their manual:
http://pine.barebones.com/manual/TextWrangler_User_Manual.pdf

You should also consider not running the script inside TextWrangler and through the command line instead.
 
I've never actually used BBEdit/TextWrangler, but if you really are serious about programming, I'd probably get more familiar with using emacs/vi instead as those are readily available on any *nix machine and available on mac. I'd also get more familiar with using the terminal (from your earlier posts it seems like you're trying to avoid using it). Using the terminal is very useful and you can get quite productive if you know how to use it.

If you do decide to pursue programming in high school or college, being familiar with these tools will only help you in the long run as you're more likely to be using a machine with a *nix operating system in the computer labs for lab work, projects and programming assignments, etc.
 
I have a problem I have been dealing for a day or two and I have done every thing in my ability to try and fix it. I have modified the code a little because I noticed at the end of it, the + signs add the variables instead of numbers. My code right now is
Code:
computer_brand = 'Apple'
processor = '1.7 gigahertz i5' 
ram = '4'
operating_system = 'Mac OS 10 Lion'
battery_life = '7'
screen_size = '13.3'
the_cost = '1300'
storage_capacity = '128'

print "This is about my %s computer." % (computer_brand)
print "Its a %s computer so it runs %s." % (computer_brand, operating_system)
print "It has a %s processor with %s of ram." % (processor, ram)
print "The battery life is very long with %s hours long battery life, WOW!" % (battery_life)
print "It can store %s gigabyte of files and has a %s inch high resolution screen." % (storage_capacity, screen_size)
print "And it only costed $%s, thats very cheap for a good and high quality %s computer." % (the_cost, computer_brand)
print "If I add %d, %d, and %d I get %d." % (ram, battery_life, screen_size, ram + battery_life + screen_size)

Error: File 9 More Variables.py; line 16
TypeError: %d format: a number is required, not str

I have even copied and pasted line 16 from the tutorial and just changed the variables but it didn't work! Programming is making me extremely frustrated :mad:.

----------

I've never actually used BBEdit/TextWrangler, but if you really are serious about programming, I'd probably get more familiar with using emacs/vi instead as those are readily available on any *nix machine and available on mac. I'd also get more familiar with using the terminal (from your earlier posts it seems like you're trying to avoid using it). Using the terminal is very useful and you can get quite productive if you know how to use it.

If you do decide to pursue programming in high school or college, being familiar with these tools will only help you in the long run as you're more likely to be using a machine with a *nix operating system in the computer labs for lab work, projects and programming assignments, etc.

How is Terminal useful? You can only type your codes there and you can't save them. I understand Terminal is useful in other parts of the computer but I don't think this goes for coding. What do you mean by "get more familiar with using emacs/vi instead as those are readily available on any nix machine and available on the mac?"
 
I have a problem I have been dealing for a day or two and I have done every thing in my ability to try and fix it. I have modified the code a little because I noticed at the end of it, the + signs add the variables instead of numbers. My code right now is
Code:
computer_brand = 'Apple'
processor = '1.7 gigahertz i5' 
ram = '4'
operating_system = 'Mac OS 10 Lion'
battery_life = '7'
screen_size = '13.3'
the_cost = '1300'
storage_capacity = '128'

print "This is about my %s computer." % (computer_brand)
print "Its a %s computer so it runs %s." % (computer_brand, operating_system)
print "It has a %s processor with %s of ram." % (processor, ram)
print "The battery life is very long with %s hours long battery life, WOW!" % (battery_life)
print "It can store %s gigabyte of files and has a %s inch high resolution screen." % (storage_capacity, screen_size)
print "And it only costed $%s, thats very cheap for a good and high quality %s computer." % (the_cost, computer_brand)
print "If I add %d, %d, and %d I get %d." % (ram, battery_life, screen_size, ram + battery_life + screen_size)

Error: File 9 More Variables.py; line 16
TypeError: %d format: a number is required, not str

I have even copied and pasted line 16 from the tutorial and just changed the variables but it didn't work! Programming is making me extremely frustrated :mad:.

----------



How is Terminal useful? You can only type your codes there and you can't save them. I understand Terminal is useful in other parts of the computer but I don't think this goes for coding. What do you mean by "get more familiar with using emacs/vi instead as those are readily available on any nix machine and available on the mac?"

In your python example your ram,battery_life,screen_size variables are initialized as strings. However @line16 you are trying to use them as integers in your print statement. Therefore you'll either have to a) declare those variables as integers at the beginning instead of strings. ie ram = 4 instead of ram = '4'. or b) convert those variables to integers at the print statement. ie. int(ram) instead of just ram.

As for the terminal comment, you're fine with writing your sample python codes in a file like you have been doing. But my suggestion was in terms of overall usage of the terminal. Like to run the python scripts and/or getting used to the file system structure and more of a navigating and using the terminal tools standpoint. Though since you're still learning the basics, going through the terminal interactive python coding mode will probably be a good learning experience as well, since it's easier to try out various commands without having to constantly save and rerun the scripts. But again, it was more in terms of getting the overall feel of the terminal.

As for the emacs/vi comment, it's more a general suggestion with your choice of editor. You've chosen to use TextWrangler, which I'm sure is fine. But if you are going to start getting used to and learning a new text editor, emacs or vi are the ones I would highly suggest getting good with.
 
In your python example your ram,battery_life,screen_size variables are initialized as strings. However @line16 you are trying to use them as integers in your print statement. Therefore you'll either have to a) declare those variables as integers at the beginning instead of strings. ie ram = 4 instead of ram = '4'. or b) convert those variables to integers at the print statement. ie. int(ram) instead of just ram.

Remember, your talking to an 11 year old here. I didn't quite understand that but I think you mean that don't put single quotes around the variable that are only numbers. It used to be ram = 4GB of DDR3 but if I did the last line would be like this:
If I add 4GB of DDR3, 7, and 13.3 I get 4GB of DDR3713.3

They would just add the variables together instead of the numbers. I don't get how this guy does it http://learnpythonthehardway.org/book/ex5.html.
 
Ok, I got the adding problem fixed, the code 100% works now but I have a tiny question, whats the difference between %r %s and %d? To me they all do the same except for %d. I'll upload a video in a few minutes.
 
D is decimal (a number), s is string (so it considers the words between ' or "), and the name of r escapes me, but it will print exactly what you wrote including the ' or ".
 
D is decimal (a number), s is string (so it considers the words between ' or "), and the name of r escapes me, but it will print exactly what you wrote including the ' or ".

Ok, I think I get it, %d is for numbers but it won't print decimals. So does that mean %r is multi-use, decimal/number or a variable string?
 
Ok, I think I get it, %d is for numbers but it won't print decimals. So does that mean %r is multi-use, decimal/number or a variable string?

Seems like you're having a hard time dealing with the concept of types. More practice and experimenting should help you deal with understanding the need and use for different variables containing different types. Also you can look through some various google pages if you are still confused.

As for the different string formatting operations, this page may come in handy.

http://docs.python.org/release/2.4.4/lib/typesseq-strings.html

If you want to deal with just floating point numbers in your print statement, you can opt to use %f, which is for decimal numbers.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.