Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Output of Python code
#1
what is the output of the following code in Python 3.x environment?

x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")

Options:
1. abcdef
2. Error
3. a b c d e f
4. No Output
Quote
#2
(Sep-13-2019, 10:44 AM)hemal07yc Wrote: what is the output of the following code in Python 3.x environment?

x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")

Options:
1. abcdef
2. Error
3. a b c d e f
4. No Output

Sorry, but that seems to be a very easy exercise, that you don't seem to have even tried to work it out.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Quote
#3
I Didn't get any output
According to me, answer should be "No Output"

Kindly confirm that it is correct or not.

Thanking you in advance..
Quote
#4
(Sep-13-2019, 10:57 AM)hemal07yc Wrote: I Didn't get any output

So you have an answer.

However, if you are wondering why there is no output: there is no letter 'i' in x ('abcdef') so while loop is never executed as condition is false.

If you are adventurous you can write i = 'a' and observe what will happen (warning: you will enter endless loop, to break it press ctrl + c)
newbieAuggie2019 likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#5
(Sep-13-2019, 10:57 AM)hemal07yc Wrote: I Didn't get any output
According to me, answer should be "No Output"

Kindly confirm that it is correct or not.

Thanking you in advance..

Okay, that's better...

Nevertheless, there must be an indentation (four spaces) for print, otherwise it produces an
Error:
expected an indented block error
The correct code should be:
x = "abcdef"
i = "i"
while i in x:
    print(i, end=" ")
And if you run it, nothing happens because:
1. If there were an 'i' in the string "x" ('abcdef'), then "i" would be printed. As it is not the case, nothing is printed. That explains the whole exercise, as nothing is printed; therefore the solution is number 4. No Output.

All the best,

(Sep-13-2019, 11:09 AM)perfringo Wrote:
(Sep-13-2019, 10:57 AM)hemal07yc Wrote: I Didn't get any output

So you have an answer.

However, if you are wondering why there is no output: there is no letter 'i' in x ('abcdef') so while loop is never executed as condition is false.

If you are adventurous you can write i = 'a' and observe what will happen (warning: you will enter endless loop, to break it press ctrl + c)

Oops! It seems that I posted just after you! Sorry! Blush
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Quote
#6
(Sep-13-2019, 11:10 AM)newbieAuggie2019 Wrote: Oops! It seems that I posted just after you! Sorry! Blush

At some point we are all have been ninja'd Big Grin
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  What will the following code output? dukoolsharma 5 461 Dec-15-2018, 06:05 AM
Last Post: HarshaliPatel
  Python on Windows 2012RC2 (with VS Code 1.22.2). Scripts not generating output. JGFMK 2 782 Apr-24-2018, 08:17 PM
Last Post: JGFMK
  csv output code migs2173 2 1,177 Jul-08-2017, 10:06 AM
Last Post: migs2173

Forum Jump:


Users browsing this thread: 1 Guest(s)