Python-list Digest, Vol 170, Issue 34

ROGER GRAYDON CHRISTMAN dvl at psu.edu
Mon Nov 27 10:27:34 EST 2017


I'll answer your question with a couple questions:

Why do you expect to get the results you seem to expect?

What part of your code should repeat (and why)?

A key factor in recognizing the difference between 'if' and 'while'
is knowing what effect they have on the indented statements 
that follow them.

Roger Christman
Pennsylvania State University

On Mon, Nov 27, 2017 Cai Gengyang wrote:
>

>Message: 38
>Date: Mon, 27 Nov 2017 04:54:21 -0800 (PST)
>From: Cai Gengyang <gengyangcai at gmail.com>
>Subject: While, If, Count Statements
>Message-ID: <b978e7cb-9acf-4aaa-8431-a5469f2994e8 at googlegroups.com>
>Content-Type: text/plain; charset="UTF-8"
>
>
>Input :
>
>count = 0
>
>if count < 5:
>  print "Hello, I am an if statement and count is", count
>
>while count < 10:
>  print "Hello, I am a while and count is", count
>  count += 1
>
>Output :
>
>Hello, I am an if statement and count is 0
>Hello, I am a while and count is 0
>Hello, I am a while and count is 1
>Hello, I am a while and count is 2
>Hello, I am a while and count is 3
>Hello, I am a while and count is 4
>Hello, I am a while and count is 5
>Hello, I am a while and count is 6
>Hello, I am a while and count is 7
>Hello, I am a while and count is 8
>Hello, I am a while and count is 9
>
>The above input gives the output below. Why isn't the output instead :
>
>Hello, I am an if statement and count is 0
>Hello, I am a while and count is 0
>Hello, I am an if statement and count is 1
>Hello, I am a while and count is 1
>Hello, I am an if statement and count is 2
>Hello, I am a while and count is 2
>Hello, I am an if statement and count is 3
>Hello, I am a while and count is 3
>Hello, I am an if statement and count is 4
>Hello, I am a while and count is 4
>Hello, I am a while and count is 5
>Hello, I am a while and count is 6
>Hello, I am a while and count is 7
>Hello, I am a while and count is 8
>Hello, I am a while and count is 9
>
>




More information about the Python-list mailing list