+ 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

27th Feb 2018, 12:46 PM
Monish Singh
Monish Singh - avatar
3 Antworten
+ 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)
27th Feb 2018, 1:03 PM
Nox
Nox - avatar
+ 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)
27th Feb 2018, 1:06 PM
Ran Avital
Ran Avital - avatar
+ 3
@Monish Singh, please here is your solution: https://code.sololearn.com/cxoL5JVxzrF2/?ref=app
27th Feb 2018, 2:42 PM
📈SmileGoodHope📈
📈SmileGoodHope📈 - avatar