+ 6
What is the output of the code (in python2) ?
import random SIDES=["EAST","WEST","NORTH","SOUTH"]; N=random.randint(1,3) OUT=" " for I in range(N,1,-1): OUT=OUT+SIDES[I] print OUT
3 Respostas
+ 5
Hi Monish,
in python 2, it will give SOUTHNORTH but in python 3, you will get an error saying "Missing parentheses in call to 'print'". This is because in python 3, you have to use () with print. In this case: print (OUT)
+ 7
randint(m, n) can give a result of numbers from m (including) to n (including).
For example: if N = 3:
OUT = " SOUTHNORTH"
Explanation for the loop:
N = 3
l = 3
SIDES[l] = "SOUTH"
OUT += "SOUTH"
Then l = 2
SIDES[2] = "NORTH"
OUT += "NORTH"
Then l = 1 and the loop is over.
Don't forget to add brackets to the print function:
print(OUT)
+ 3
@Monish Singh, please here is your solution:
https://code.sololearn.com/cxoL5JVxzrF2/?ref=app