9. something of value


#1



I get a pass at code academy with the following code and I believe my code is correct. However, when I write the same code on my mac python shell 3.5.2 I am getting an invalid syntax warning. does anybody have any idea whats going on?


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

for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]
total=0
for key in prices:
    total+=prices[key]*stock[key]
print total


#2

python3 is not backward compatible with python2, one of the biggest changes was the updated of print:

# python 2
print "hello world"
#python 3
print("hello world")

print now requires parentheses since it is a function call and no longer a statement


#3

Thanks and I am aware of that.

So in my shell my final line is print (total), but that still gives me the syntax error.


#4

then i need to see the code you ran in the shell, given it can't be this one. And a typo or another small mistake is easily made


#5

prices={
"banana":4,
"apple":2,
"orange":1.5,
"pear":3
}
stock={
"banana":6,
"apple":0,
"orange":32,
"pear":15
}
total=0
for key in prices:
total+=prices[key]*stock[key]
print (total)


#6

hard to keep the formatting but indentation is as follows:

for key...
(4 spaces) ...Total+=...
(same indent as for key) print (total)


#7

okay, please use markup to keep the style of the code:

Otherwise i can't run your code


#8

prices={
	"banana":4,
	"apple":2,
	"orange":1.5,
	"pear":3
	}
stock={
	"banana":6,
	"apple":0,
	"orange":32,
	"pear":15
	}
total=0
for key in prices:
	total+=prices[key]*stock[key]
print (total)

#9

do you think I am missing print (key) before my total by the way?


#10

this has to do with the fact that you use the IDLE.

by doing this:

>>> for key in prices:
...     total+=prices[key]*stock[key]
... print (total)

the idle thinks print is still part of the loop, so what you should do is:

>>> for key in prices:
...     total+=prices[key]*stock[key]
... 
>>> print (total)

#11

yes you are right! many thx.


#12

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