Question about lesson #19 Learn python the hard way

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2673 Views - Last Post: 25 February 2015 - 01:11 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Question about lesson #19 Learn python the hard way

Posted 31 January 2015 - 09:25 PM

Finished lesson #19 Zed Shaw's learn python the hard way. I scroll down to the 'study drills' Zed asks for me to find 10 possible ways to runs a function and decide I want to challenge myself and try something hard first. One of the possibilities is to make the user supply the numbers for the program. I can make the program work and here is the program I wrote. Its not exactly like how he has his, but I figure the user is supplying the values and I can't use every aspect of his program. I do have a few quesitons:
1. When I do the addition (For example, if I choose 5 cheese and 5 crackers) it comes out as 55 instead of 10.
2. When I do the multiplication (Same example as above for values)I get an error which says its on line 11. This is the message I receive:
#line 11, in cheese_and_crackers
print cheese * crackers
type error: can't multiply sequence by non-int of type 'str'
3. When I remove the multiplication part of my program and try making the values placed by the user (cheese+100 and crackers +1000) I get this error message.
#print (cheese + 100, crackers + 1000)
TypeError: cannot concatenate 'str' and 'int' objects.

I've been racking my brains on this program for three days and I think I'd have better luck hitting my head with a hammer. Any and all advice would be greatly appreciated.

This is the program below:

def cheese_and_crackers():
   print "How many cheeses do you have? " 
   cheese=raw_input() 
   print "How many boxes of crackers do you have? "
   crackers=raw_input()
   print "Man, that's enough for a party!"
   print "Get a blanket.\n"
   print "We can also add the amount %s cheese and %s crackers: "% (cheese, crackers)  
   print (cheese + crackers) 
   print "We can also multiply the amount %s and %s crackers:" % (cheese, crackers)
   print (cheese * crackers)
   print "We can combine two variables and math:"
   print (cheese + 100, crackers + 1000) 


cheese_and_crackers()



Is This A Good Question/Topic? 0
  • +

Replies To: Question about lesson #19 Learn python the hard way

#2 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: Question about lesson #19 Learn python the hard way

Posted 31 January 2015 - 09:31 PM

I don't actually see any code - is it just not loading for me or is it not present?
Was This Post Helpful? 0
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Re: Question about lesson #19 Learn python the hard way

Posted 31 January 2015 - 09:44 PM

Sorry I thought I put my program in my question.

View Postxclite, on 31 January 2015 - 09:31 PM, said:

I don't actually see any code - is it just not loading for me or is it not present?


def cheese_and_crackers():
   print "How many cheeses do you have? " 
   cheese=raw_input() 
   print "How many boxes of crackers do you have? "
   crackers=raw_input()
   print "Man, that's enough for a party!"
   print "Get a blanket.\n"
   print "We can also add the amount %s cheese and %s crackers: "% (cheese, crackers)  
   print (cheese + crackers) 
   print "We can also multiply the amount %s and %s crackers:" % (cheese, crackers)
   print (cheese * crackers)
   print "We can combine two variables and math:"
   print (cheese + 100, crackers + 1000) 


cheese_and_crackers()


Was This Post Helpful? 0
  • +
  • -

#4 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: Question about lesson #19 Learn python the hard way

Posted 31 January 2015 - 09:49 PM

Oh, you did, you just did some weird things with the code tags that distracted me. Fixing, then on to the question.

Ok, so the errors should be telling you exactly what is wrong.

raw_input returns a string. In the first case, you're adding two strings together, which just concatenates them. For example:
"a" + "b" -> "ab"



In the second case, you can't multiple a string by a string.

In the third case, python won't try to guess what you mean when you add 100 to a string.

Try using int(raw_input()).

As an unrelated point, you can pass the text prompt into raw_input instead of printing it, like:
cheese = int(raw_input("How many cheeses do you have? "))


Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Question about lesson #19 Learn python the hard way

Posted 01 February 2015 - 04:31 PM

I appreciate that you're trying to rewrite the function, but don't forget to do the thing he's actually asking you to: he wants you to take the function that he shows you in the chapter and find ten different ways to call it - meaning, ten different ways to trigger that function. So one way would be the obvious:

cheese_and_crackers(1,2)


or you can use arithmetic in the function call:


cheese_and_crackers(1+3,2+5)


What other ways can you come up with? There are a lot of things that you can do which are more complicated than what I see him showing you here, but there's surely at least ten ways you can fill up those argument slots - I think that's what he's asking you to think about here.
Was This Post Helpful? 2
  • +
  • -

#6 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Re: Question about lesson #19 Learn python the hard way

Posted 02 February 2015 - 09:20 PM

View Postxclite, on 31 January 2015 - 09:49 PM, said:

Oh, you did, you just did some weird things with the code tags that distracted me. Fixing, then on to the question.

Ok, so the errors should be telling you exactly what is wrong.

raw_input returns a string. In the first case, you're adding two strings together, which just concatenates them. For example:
"a" + "b" -> "ab"



In the second case, you can't multiple a string by a string.

In the third case, python won't try to guess what you mean when you add 100 to a string.

Try using int(raw_input()).

As an unrelated point, you can pass the text prompt into raw_input instead of printing it, like:
cheese = int(raw_input("How many cheeses do you have? "))



I Thought about what you said and I must admit I didn't see your suggestion below and after doing some investigation I came to the very same conclusion and made it work. Thank you very much for your help. Ill have to make a better effort to look at the entire message people leave me (I wouldve saved time and a weekend working on how to make it work.

def cheese_and_crackers():
   print "How many cheeses do you have? " 
   cheese=int(raw_input()) 
   print "How many boxes of crackers do you have? "
   crackers=int(raw_input())
   print "Man, that's enough for a party!"
   print "Get a blanket.\n"
   print "We can also add the amount %s cheese and %s crackers: "% (cheese, crackers)  
   print  cheese + crackers
   print "We can also multiply the amount %s and %s crackers:" % (cheese, crackers)
   print  cheese * crackers
   print "We can combine two variables and math:"
   print  cheese + 100, crackers + 1000 


cheese_and_crackers()

This post has been edited by andrewsw: 03 February 2015 - 02:52 AM
Reason for edit:: Fixed botched code tags and removed double quote

Was This Post Helpful? 0
  • +
  • -

#7 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Re: Question about lesson #19 Learn python the hard way

Posted 02 February 2015 - 09:26 PM

View Postjon.kiparsky, on 01 February 2015 - 04:31 PM, said:

I appreciate that you're trying to rewrite the function, but don't forget to do the thing he's actually asking you to: he wants you to take the function that he shows you in the chapter and find ten different ways to call it - meaning, ten different ways to trigger that function.


Wait thats what he wanted the students to do? Aw man!!! I over thought the entire lesson and didn't even come close to what I was suppose to do. Ok, so Ill use your two examples and find the other 8 or maybe I can find more on my own.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Question about lesson #19 Learn python the hard way

Posted 02 February 2015 - 10:02 PM

Well, that's how I read it anyway. But you know, there's nothing at all wrong with spending a little time rewriting those functions that he shows you.
In fact, I often suggest this as a required part of reading any book on programming. The idea is to make sure that you understand everything that's going on, by coming up with a hypothesis about each piece of it. And then you write a little bit of code that tests that hypothesis. For example, what can you do with those variables that you're putting into the format strings? Can they be numeric literals? Can they more complex expressions, instead of just variables? Can they be function calls? What do you think is actually required there?

This question actually relates to the question of "how many different ways can you call this function?". You might like to think about that relationship a little. It'll be important later on. (or not so much later on, depending on how good you are at applying what you learn to the problems at hand)
Was This Post Helpful? 1
  • +
  • -

#9 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Re: Question about lesson #19 Learn python the hard way

Posted 18 February 2015 - 10:54 AM

I'm still stuck on that lesson (Python the hard way lesson #19). I read what you said and figured out more ways to trigger a function. My problem is that every time I try and run it on windows powershell I keep getting the same error message. I checked and re checked my program and I can't figure out why it keeps saying "invalid syntax"

This is the program using what you suggested to me. Please don't give me the correct answer, I'd like to try and learn on my own. But if you could please tell me if I'm on the right track.

1

def cheese_and_crackers(1+3,2+5):
  print "You have %d cheeses!" %1+3
  print "You have %d boxes of crackers!"%2+5
  print "man that's enough for a party!"
  print "Get a blanket. \n"

print "We can just give the function numbers directly:"
cheese_and_crackers(20, 30)

print "OR, we can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, amount_of_crackers)

print "We can even do the math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)

print "And we can combine the two, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)


I also figured I could do mathematics as well as put words within the variable

This post has been edited by jon.kiparsky: 18 February 2015 - 11:11 AM
Reason for edit:: quote extraction

Was This Post Helpful? 0
  • +
  • -

#10 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: Question about lesson #19 Learn python the hard way

Posted 18 February 2015 - 11:06 AM

Please use code tags and properly format code. This is especially important for a language like Python where whitespace is significant.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Question about lesson #19 Learn python the hard way

Posted 18 February 2015 - 11:11 AM

Those are some good examples. The game is really to find things that are integers. What other ways can you think of to get an integer? For example, what if you had a dict like this:

params = {'foo':10, 'bar':20, 'baz':30'} 



What could you do with that?

What if you had the strings '12' and '34' - could you find a way to use that to call this function?
Was This Post Helpful? 0
  • +
  • -

#12 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Re: Question about lesson #19 Learn python the hard way

Posted 18 February 2015 - 11:34 AM

I'm continuing this conversation is public because you're right its a good topic.

Well if I was to use what I learned from that lesson and previous lessons in python, I would say the way I would do it would be like this:

You have
params = {'foo':10, 'bar':20, 'baz':30}

so I would probably do it like this....
def foo{5+5}: or def foo{2*5}:

From what I understand you use an in integer to do the math in a string. So this is how I would do it with your examples?.

def foo{10}:
    print foo
    2*5

def bar{20}:
    print bar
    3+17

def baz{30}:
    print baz
    45-15
 

Am I close?
Was This Post Helpful? 0
  • +
  • -

#13 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: Question about lesson #19 Learn python the hard way

Posted 18 February 2015 - 12:45 PM

Another friendly reminder to use the code tags and format your code, please.
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Question about lesson #19 Learn python the hard way

Posted 18 February 2015 - 01:03 PM

View Postalbert003, on 18 February 2015 - 01:34 PM, said:

Am I close?


Not very. Remember how dictionary lookups work? If you have

d = {'a':1, 'b':3, 'c':43}


then what would you do to look up a value in d?
Was This Post Helpful? 0
  • +
  • -

#15 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Re: Question about lesson #19 Learn python the hard way

Posted 21 February 2015 - 04:06 PM

Ok, I think I get it this time. I wanted to redeem myself for being so far off and not actually troubleshooting or unlearning what I previously learned. So, getting back to your questions, my thought would be to solve your problem this way:

#params = {'foo':10,'bar':20,'baz':30'}
#I can leave it as an integer
foo=10
#I can also convert an integer into a string and reverse the process.
bar=13
baz="This is a number..."
bar=str(bar)
print baz+bar
#This is my favourite hockey players number...13
#We can also reverse the process...
baz ='30'
bar=20
print int(baz)+bar
#The answer will show 50.

This post has been edited by andrewsw: 21 February 2015 - 04:23 PM
Reason for edit:: fixed botched code tags

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2