Python Forum
Using a If-else statement as solution - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Using a If-else statement as solution (/thread-5705.html)



Using a If-else statement as solution - sri_harsha_g - Oct-17-2017

Hope you all are having a good learning experience. Here I have a secondary solution proposal for a Quiz, a bit traditional but should work flawlessly.

However I am unable to catch the mistake in my code to arrive at the same results as to the code given by the course.

Here is the python code, please help me out if you can:

#Modify the code below so that the function sense, which
#takes p and Z as inputs, will output the NON-normalized
#probability distribution, q, after multiplying the entries
#in p by pHit or pMiss according to the color in the
#corresponding cell in world.

p=[0.2, 0.2, 0.2, 0.2, 0.2]
world=[‘green’, ‘red’, ‘red’, ‘green’, ‘green’]
Z = 'red’
pHit = 0.6
pMiss = 0.2

def sense(p, Z):
#
#ADD YOUR CODE HERE
#
q=
for i in range(len§):
#hit = (Z==world)
#q.append(p[i]((hitpHit)+((1-hit)*pMiss)))
print i
if(Z==world[i]):
q.append(p[i]*pHit)
print i,"Hit"
print i
else:
q.append(p[i]*pMiss)
print i,"Miss"
return q

print sense(p,Z)

Response : Here I have used some print statements to debug the code and I see that my code does not want to process index 0 and index 3. If one of you are kind enough to help me it would help me a long way. Thanks in advance.

0
0
1
1 Hit
1
2
2 Hit
2
3
3
4
4
4 Miss
[0.12, 0.12, 0.04000000000000001]
[/i][/i][/i][/i]