What is the expected behavior of the following code?
What is the expected behavior of the following code?
A
If im not wrong, is B) string=str(1/3) ---- this is equal a 0.3333333333 dummy='' >>> for character in string: dummy=character+dummy >>> dummy '3333333333333333.0' print(dummy[-1])----- this is 0
If strong is a typo and is actually string, dummy = 333333333333333.0 So the answer is B: 0
A. it raises an exception strong variable does not exist
If there is: for character in strong: Answer is A If there is: for character in string: Answer is B
dummy = character + dummy: Adds the current character to the BEGINNING of the dummy string, effectively reversing the order of characters. print(dummy[-1]): Prints the last character of the reversed string, which is the first character of the original string.
string = str(1/3) dummy='' for character in strong: dummy=character + dummy print(dummy[-1]) Output -> NameError: name 'strong' is not defined Ans A
A is right if the variable name us strong but if corrected to string (no sure if this a typo or intentional), then the result is 0.
It outputs 0. string=str(1/3) print("string=",string) dummy='' for character in string: print("character=",character) dummy=character+dummy print("dummy=",dummy) print(dummy[-1])
Answer is B: string = str(1/3) dummy = '' for character in string: dummy = dummy + character print(dummy[-1]) # 3 # In python all types are classses print(type(dummy)) # <class 'str'>
I tried running your code and still got the exception "IndentationError: expected an indented block"
it is: dummy = character + dummy. Then the output will be 0.