8. Keeping Track of the Produce question


#1
  1. Keeping Track of the Produce

I am wondering if there is a difference between these two methods

  • ps:I know how to fix the activity just wondering if my method is the wrong way of doing things.

I learned to create custom text by doing something along the line of:

name=input("Enter your name")
print ("Hello: ",name)

so as a result I did this:

prices = {
  'banana':4,
  'apple':2,
  'orange':1.5,
  'pear':3
}
stock = {
  'banana':6,
  'apple':0,
  'orange':32,
  'pear':15
}

for z in prices:
  print z
  print 'price: ',prices[z]
  print 'stock: ',stock[z]
  print

just wondering why python ask u to do it like (below)
is this method better, or just another way of doing the same thing?

once  = {'a': 1, 'b': 2}
twice = {'a': 2, 'b': 4}
for key in once:
  print "Once: %s" % once[key]
  print "Twice: %s" % twice[key]

picture to easier visualize-


#2

Hi @paulaswam,

Codecademy does not want the user to include a print statement that inserts a blank line in the output.


#3

@appylpye

please read my entire question


#4

The lesson expects a string printing value. so you need to use string formatting syntax ( %s ) here.

that’s the difference.


#5

Codecademy wants you to use their example as a model, so that you can practice string formatting. That is not meant to imply that it is always the best way to do it.


#6

@appylpye thanks for your insight

@rcodeman
To be honest I don’t understand what u mean by ‘string formatting syntax’. because from what I see they produce the same output. can you explain?


#7

sure :slight_smile:

look at here

print 'hello',1
print 'hello %s'%1
print '...................'

if you run the above code the output will be the same.but both are not same type.

t='hello',1
s='hello %s'%1
print t==s
print '....................'
print t
print s
print '....................'
print type(t)
print type(s)

The first one is tuple and second one is string.

producing same output is always not same.

print 5 # type int
print '5' # type str

#8

thank you, I understand what you mean now. :smiley:


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.