python中怎么用while语句判断一串字符的结束? python如何用if 语句判断字符时如何不区分大小写

python\u4e2dwhile\u5faa\u73af\u7684\u95ee\u9898

Python \u7f16\u7a0b\u4e2d while \u8bed\u53e5\u7528\u4e8e\u5faa\u73af\u6267\u884c\u7a0b\u5e8f\uff0c\u5373\u5728\u67d0\u6761\u4ef6\u4e0b\uff0c\u5faa\u73af\u6267\u884c\u67d0\u6bb5\u7a0b\u5e8f\uff0c\u4ee5\u5904\u7406\u9700\u8981\u91cd\u590d\u5904\u7406\u7684\u76f8\u540c\u4efb\u52a1\u3002\u5176\u57fa\u672c\u5f62\u5f0f\u4e3a\uff1a
while \u5224\u65ad\u6761\u4ef6\uff1a\u6267\u884c\u8bed\u53e5\u2026\u2026
\u6267\u884c\u8bed\u53e5\u53ef\u4ee5\u662f\u5355\u4e2a\u8bed\u53e5\u6216\u8bed\u53e5\u5757\u3002\u5224\u65ad\u6761\u4ef6\u53ef\u4ee5\u662f\u4efb\u4f55\u8868\u8fbe\u5f0f\uff0c\u4efb\u4f55\u975e\u96f6\u3001\u6216\u975e\u7a7a\uff08null\uff09\u7684\u503c\u5747\u4e3atrue\u3002
\u5f53\u5224\u65ad\u6761\u4ef6\u5047false\u65f6\uff0c\u5faa\u73af\u7ed3\u675f\u3002
\u6267\u884c\u6d41\u7a0b\u56fe\u5982\u4e0b\uff1a

Gif \u6f14\u793a Python while \u8bed\u53e5\u6267\u884c\u8fc7\u7a0b

\u5b9e\u4f8b
#!/usr/bin/python count = 0while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"
\u8fd0\u884c\u5b9e\u4f8b »
\u4ee5\u4e0a\u4ee3\u7801\u6267\u884c\u8f93\u51fa\u7ed3\u679c:
The count is: 0The count is: 1The count is: 2The count is: 3The count is: 4The count is: 5The count is: 6The count is: 7The count is: 8Good bye!
while \u8bed\u53e5\u65f6\u8fd8\u6709\u53e6\u5916\u4e24\u4e2a\u91cd\u8981\u7684\u547d\u4ee4 continue\uff0cbreak \u6765\u8df3\u8fc7\u5faa\u73af\uff0ccontinue \u7528\u4e8e\u8df3\u8fc7\u8be5\u6b21\u5faa\u73af\uff0cbreak \u5219\u662f\u7528\u4e8e\u9000\u51fa\u5faa\u73af\uff0c\u6b64\u5916"\u5224\u65ad\u6761\u4ef6"\u8fd8\u53ef\u4ee5\u662f\u4e2a\u5e38\u503c\uff0c\u8868\u793a\u5faa\u73af\u5fc5\u5b9a\u6210\u7acb\uff0c\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\uff1a
# continue \u548c break \u7528\u6cd5 i = 1while i 0: # \u975e\u53cc\u6570\u65f6\u8df3\u8fc7\u8f93\u51fa continue print i # \u8f93\u51fa\u53cc\u65702\u30014\u30016\u30018\u300110 i = 1while 1: # \u5faa\u73af\u6761\u4ef6\u4e3a1\u5fc5\u5b9a\u6210\u7acb print i # \u8f93\u51fa1~10 i += 1 if i > 10: # \u5f53i\u5927\u4e8e10\u65f6\u8df3\u51fa\u5faa\u73af break
\u65e0\u9650\u5faa\u73af
\u5982\u679c\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u6c38\u8fdc\u4e3a true\uff0c\u5faa\u73af\u5c06\u4f1a\u65e0\u9650\u7684\u6267\u884c\u4e0b\u53bb\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a
\u5b9e\u4f8b
#!/usr/bin/python# -*- coding: UTF-8 -*- var = 1while var == 1 : # \u8be5\u6761\u4ef6\u6c38\u8fdc\u4e3atrue\uff0c\u5faa\u73af\u5c06\u65e0\u9650\u6267\u884c\u4e0b\u53bb num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!"
\u4ee5\u4e0a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\uff1a
Enter a number :20You entered: 20Enter a number :29You entered: 29Enter a number :3You entered: 3Enter a number between :Traceback (most recent call last):File "test.py", line 5, in num = raw_input("Enter a number :")KeyboardInterrupt
\u6ce8\u610f\uff1a\u4ee5\u4e0a\u7684\u65e0\u9650\u5faa\u73af\u4f60\u53ef\u4ee5\u4f7f\u7528 CTRL+C \u6765\u4e2d\u65ad\u5faa\u73af\u3002
\u5faa\u73af\u4f7f\u7528 else \u8bed\u53e5
\u5728 python \u4e2d\uff0cwhile \u2026 else \u5728\u5faa\u73af\u6761\u4ef6\u4e3a false \u65f6\u6267\u884c else \u8bed\u53e5\u5757\uff1a
\u5b9e\u4f8b
#!/usr/bin/python count = 0while count < 5: print count, " is less than 5" count = count + 1else: print count, " is not less than 5"
\u4ee5\u4e0a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a
0 is less than 51 is less than 52 is less than 53 is less than 54 is less than 55 is not less than 5
\u7b80\u5355\u8bed\u53e5\u7ec4
\u7c7b\u4f3c if \u8bed\u53e5\u7684\u8bed\u6cd5\uff0c\u5982\u679c\u4f60\u7684 while \u5faa\u73af\u4f53\u4e2d\u53ea\u6709\u4e00\u6761\u8bed\u53e5\uff0c\u4f60\u53ef\u4ee5\u5c06\u8be5\u8bed\u53e5\u4e0ewhile\u5199\u5728\u540c\u4e00\u884c\u4e2d\uff0c \u5982\u4e0b\u6240\u793a\uff1a
\u5b9e\u4f8b
#!/usr/bin/python flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!"

python
while\u5faa\u73af\u8bed\u53e5
python
\u7f16\u7a0b\u4e2d
while
\u8bed\u53e5\u7528\u4e8e\u5faa\u73af\u6267\u884c\u7a0b\u5e8f\uff0c\u5373\u5728\u67d0\u6761\u4ef6\u4e0b\uff0c\u5faa\u73af\u6267\u884c\u67d0\u6bb5\u7a0b\u5e8f\uff0c\u4ee5\u5904\u7406\u9700\u8981\u91cd\u590d\u5904\u7406\u7684\u76f8\u540c\u4efb\u52a1\u3002\u5176\u57fa\u672c\u5f62\u5f0f\u4e3a\uff1a
while
\u5224\u65ad\u6761\u4ef6\uff1a
\u6267\u884c\u8bed\u53e5\u2026\u2026
\u6267\u884c\u8bed\u53e5\u53ef\u4ee5\u662f\u5355\u4e2a\u8bed\u53e5\u6216\u8bed\u53e5\u5757\u3002\u5224\u65ad\u6761\u4ef6\u53ef\u4ee5\u662f\u4efb\u4f55\u8868\u8fbe\u5f0f\uff0c\u4efb\u4f55\u975e\u96f6\u3001\u6216\u975e\u7a7a\uff08null\uff09\u7684\u503c\u5747\u4e3atrue\u3002
\u5f53\u5224\u65ad\u6761\u4ef6\u5047false\u65f6\uff0c\u5faa\u73af\u7ed3\u675f\u3002
\u5b9e\u4f8b\uff1a
#!/usr/bin/python
count
=
0
while
(count
<
9):
print
'the
count
is:',
count
count
=
count
+
1
print
"good
bye!"
\u4ee5\u4e0a\u4ee3\u7801\u6267\u884c\u8f93\u51fa\u7ed3\u679c:
the
count
is:
0
the
count
is:
1
the
count
is:
2
the
count
is:
3
the
count
is:
4
the
count
is:
5
the
count
is:
6
the
count
is:
7
the
count
is:
8
good
bye!
while
\u8bed\u53e5\u65f6\u8fd8\u6709\u53e6\u5916\u4e24\u4e2a\u91cd\u8981\u7684\u547d\u4ee4
continue\uff0cbreak
\u6765\u8df3\u8fc7\u5faa\u73af\uff0ccontinue
\u7528\u4e8e\u8df3\u8fc7\u8be5\u6b21\u5faa\u73af\uff0cbreak
\u5219\u662f\u7528\u4e8e\u9000\u51fa\u5faa\u73af\uff0c\u6b64\u5916"\u5224\u65ad\u6761\u4ef6"\u8fd8\u53ef\u4ee5\u662f\u4e2a\u5e38\u503c\uff0c\u8868\u793a\u5faa\u73af\u5fc5\u5b9a\u6210\u7acb\uff0c\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\uff1a
#
continue
\u548c
break
\u7528\u6cd5
i
=
1
while
i
<
10:
i
+=
1
if
i%2
>
0:
#
\u975e\u53cc\u6570\u65f6\u8df3\u8fc7\u8f93\u51fa
continue
print
i
#
\u8f93\u51fa\u53cc\u65702\u30014\u30016\u30018\u300110
i
=
1
while
1:
#
\u5faa\u73af\u6761\u4ef6\u4e3a1\u5fc5\u5b9a\u6210\u7acb
print
i
#
\u8f93\u51fa1~10
i
+=
1
if
i
>
10:
#
\u5f53i\u5927\u4e8e10\u65f6\u8df3\u51fa\u5faa\u73af
break
\u65e0\u9650\u5faa\u73af
\u5982\u679c\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u6c38\u8fdc\u4e3a
true\uff0c\u5faa\u73af\u5c06\u4f1a\u65e0\u9650\u7684\u6267\u884c\u4e0b\u53bb\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a
#coding=utf-8
#!/usr/bin/python
var
=
1
while
var
==
1
:
#
\u8be5\u6761\u4ef6\u6c38\u8fdc\u4e3atrue\uff0c\u5faa\u73af\u5c06\u65e0\u9650\u6267\u884c\u4e0b\u53bb
num
=
raw_input("enter
a
number
:")
print
"you
entered:
",
num
print
"good
bye!"
\u4ee5\u4e0a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\uff1a
enter
a
number
:20
you
entered:
20
enter
a
number
:29
you
entered:
29
enter
a
number
:3
you
entered:
3
enter
a
number
between
:traceback
(most
recent
call
last):
file
"test.py",
line
5,
in
num
=
raw_input("enter
a
number
:")
keyboardinterrupt
\u6ce8\u610f\uff1a\u4ee5\u4e0a\u7684\u65e0\u9650\u5faa\u73af\u4f60\u53ef\u4ee5\u4f7f\u7528
ctrl+c
\u6765\u4e2d\u65ad\u5faa\u73af\u3002
\u5faa\u73af\u4f7f\u7528
else
\u8bed\u53e5
\u5728
python
\u4e2d\uff0cfor
\u2026
else
\u8868\u793a\u8fd9\u6837\u7684\u610f\u601d\uff0cfor
\u4e2d\u7684\u8bed\u53e5\u548c\u666e\u901a\u7684\u6ca1\u6709\u533a\u522b\uff0celse
\u4e2d\u7684\u8bed\u53e5\u4f1a\u5728\u5faa\u73af\u6b63\u5e38\u6267\u884c\u5b8c\uff08\u5373
for
\u4e0d\u662f\u901a\u8fc7
break
\u8df3\u51fa\u800c\u4e2d\u65ad\u7684\uff09\u7684\u60c5\u51b5\u4e0b\u6267\u884c\uff0cwhile
\u2026
else
\u4e5f\u662f\u4e00\u6837\u3002
#!/usr/bin/python
count
=
0
while
count
<
5:
print
count,
"
is
less
than
5"
count
=
count
+
1
else:
print
count,
"
is
not
less
than
5"
\u4ee5\u4e0a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a
0
is
less
than
5
1
is
less
than
5
2
is
less
than
5
3
is
less
than
5
4
is
less
than
5
5
is
not
less
than
5
\u7b80\u5355\u8bed\u53e5\u7ec4
\u7c7b\u4f3cif\u8bed\u53e5\u7684\u8bed\u6cd5\uff0c\u5982\u679c\u4f60\u7684while\u5faa\u73af\u4f53\u4e2d\u53ea\u6709\u4e00\u6761\u8bed\u53e5\uff0c\u4f60\u53ef\u4ee5\u5c06\u8be5\u8bed\u53e5\u4e0ewhile\u5199\u5728\u540c\u4e00\u884c\u4e2d\uff0c
\u5982\u4e0b\u6240\u793a\uff1a
#!/usr/bin/python
flag
=
1
while
(flag):
print
'given
flag
is
really
true!'
print
"good
bye!"
\u6ce8\u610f\uff1a\u4ee5\u4e0a\u7684\u65e0\u9650\u5faa\u73af\u4f60\u53ef\u4ee5\u4f7f\u7528
ctrl+c
\u6765\u4e2d\u65ad\u5faa\u73af\u3002

a=input('请输入字符:')

n=0

while n<len(a):

print(a[n])

n+=1



while就是根据index来遍历字符,index 等于字符串的长度,就是字符遍历结束了

  • pythonwhile寰幆鐢ㄦ硶
    绛旓細pythonwhile寰幆鐢ㄦ硶濡備笅锛歸hile寰幆鐪鐨寰堟湁鐢紝鎴戣繖閲屽啓浜嗕竴涓柟娉曞彨while True锛屽畠鏄竴涓寰幆锛屽畠姘歌繙鏄纭殑浼氫竴鐩磋窇涓嬪幓锛岀洿鍒颁綘鎶婂畠鎵撴柇銆傛垜浠潵涓琛岃鏉ヨ涓涓嬶紝while寰幆鐨勫紑濮嬪氨鏄痺hile鏉′欢锛寃hile True锛 True灏辨槸鏉′欢浜嗐傜劧鍚庡啋鍙凤紝杩欎釜璺焛f璺焒or涓鏍凤紝鏄鐢ㄥ啋鍙锋潵绗竴琛屽仛缁撳熬鐨勩傛帴...
  • python while寰幆璇彞鏄粈涔?
    绛旓細python while寰幆璇彞鏄紝閫氳繃while 寰幆姹1~100鎵鏈夋暣鏁扮疮鍔鐨鍜屻俽esult = 0 i = 0 while i <= 100:result += i i += 1 print('\t绗%d娆¤绠楃粨鏋滄槸锛%d' % (i, result))print('1~100鎵鏈夋暣鏁扮疮鍔犵殑鍜屼负锛%d' % result)绠浠 do...while 寰幆鏄 while 寰幆鐨勫彉绉嶃傝寰幆绋嬪簭鍦...
  • Python鐢╳hile鎬庝箞鎵撳嵃鍑虹┖蹇冩鏂瑰舰
    绛旓細1銆浣跨敤turtle瀹炵幇姝f柟褰㈢殑缁樺埗锛屽畾涔夊舰鍙俷涓烘鏂瑰舰鐨勮竟闀裤傚叿浣撲唬鐮佸涓嬶紝鏈涢噰绾炽2銆pythonwhile寰幆鐢ㄦ硶锛氫笌if璇彞鐩镐技锛寃hile寰幆鐨勬潯浠惰〃杈惧紡涔熸棤椤绘嫭鍙凤紝涓旇〃杈惧紡鏈熬蹇呴』娣诲姞鍐掑彿銆傛墽琛岃鍙ュ彲浠ユ槸鍗曚釜璇彞鎴栬鍙ュ潡銆傚垽鏂潯浠跺彲浠ユ槸浠讳綍琛ㄨ揪寮忥紝浠讳綍闈為浂銆佹垨闈炵┖锛坣ull锛夌殑鍊煎潎涓簍rue銆3銆佸嚭鐜版寰幆鐨...
  • python while寰幆璇彞鏄粈涔?
    绛旓細python while寰幆璇彞鏄細閫氳繃while 寰幆姹1~100鎵鏈夋暣鏁扮疮鍔鐨鍜屻俽esult=0銆俰=0銆倃hile i <=100銆俽esult+=i銆俰+=1銆俻rint('\t绗%d娆¤绠楃粨鏋滄槸锛%d' % (i, result))銆俻rint('1~100鎵鏈夋暣鏁扮疮鍔犵殑鍜屼负锛%d' % result)銆傚疄渚嬶細/usr/bin/python銆俢ount=0銆倃hile (count < 9)銆俻rint '...
  • python+鏈変竴搴忓垪1+3+5+8+13+21绛夌瓑+鐢╳hile寰幆姹傚嚭杩欎釜鏁板垪绗15涓暟...
    绛旓細杩欎釜鏁板垪鐨瑙勫緥鏄瘡涓椤归兘鏄墠涓ら」鐨勫拰锛岄櫎浜嗙鍥涢」鏄墠涓ら」涔嬪拰鍑忓幓绗竴椤癸紝鍗筹細绗竴椤癸細1 绗簩椤癸細3 绗笁椤癸細5 绗洓椤癸細8 (3 + 5 )绗簲椤癸細13 (5 + 8)绗叚椤癸細21 (8 + 13)...鍥犳锛屽彲浠浣跨敤while寰幆鏉ユ眰鍑鸿繖涓暟鍒楃殑绗15椤广傚叿浣撳疄鐜板涓嬶細a, b, c = 1, 3, 5 # ...
  • python涓甯歌鐨勫惊鐜粨鏋勬湁涓ょ瀹炵幇鏂瑰紡鍒嗗埆鏄?
    绛旓細鍦≒ython涓寰幆璇彞鐨勭被鍨嬩富瑕佹湁浠ヤ笅鍑犵锛While寰幆锛氬湪缁欏畾鐨勫垽鏂潯浠朵负true鏃舵墽琛屽惊鐜綋锛屽惁鍒欓鍑哄惊鐜綋銆侳or寰幆锛氶噸澶嶆墽琛岃鍙ャ傚祵濂楀惊鐜細鍙互鍐while寰幆浣撻噷宓屽for寰幆銆傜粨鏋勭畝浠 寰幆缁撴瀯鍙互鐪嬫垚鏄竴涓潯浠跺垽鏂鍙ュ拰涓涓悜鍥炶浆鍚戣鍙ョ殑缁勫悎銆傚彟澶栵紝寰幆缁撴瀯鐨勪笁涓绱狅細寰幆鍙橀噺銆佸惊鐜綋鍜屽惊鐜...
  • python缂栧啓涓涓▼搴,浣跨敤while寰幆?
    绛旓細鎸夌収浣鐨瑕佹眰缂栧啓鐨勭▼搴忓涓(瑙佸浘,娉ㄦ剰婧愪唬鐮佺殑缂╄繘)
  • whiletrue鍦╬ython涓庝箞鐢╳hiletrue
    绛旓細鍏充簬while true鍦╬ython涓庝箞鐢锛寃hile true杩欎釜寰堝浜鸿繕涓嶇煡閬擄紝浠婂ぉ鏉ヤ负澶у瑙g瓟浠ヤ笂鐨勯棶棰橈紝鐜板湪璁╂垜浠竴璧锋潵鐪嬬湅鍚э紒1銆亀hile 灏辨槸杩涜寰幆锛宼rue 涓哄垽鏂惊鐜殑鏉′欢锛寃hile锛坱rue锛夊氨鏄竴鐩村惊鐜繘琛寋}鍐呯殑浠g爜銆2銆佺洿鍒版湁浠g爜鎵цbreak 璇彞锛岃烦鍑哄惊鐜
  • 缂栫▼鐢╳hile寰幆 10涓囧厓瀛樺叆閾惰,骞村埄鐜囦负6%,10骞村悗鍙栧嚭,涓鍏卞灏戦挶...
    绛旓細鍦≒ython涓瀹炵幇姝ら棶棰樼殑浠g爜锛歚``money = 100000 # 瀛樺叆鐨勬湰閲 rate = 0.06 # 骞村埄鐜 years = 10 # 瀛橀挶鐨勫勾闄 i = 0 while i < years:money += money * rate # 姣忓勾鍒╂伅 i += 1 print("瀛樺叆10骞村悗鎬诲叡鏈 %.2f 鍏" % money)```杩愯杩欐浠g爜锛屽嵆鍙緭鍑猴細瀛樺叆10骞...
  • python鑾峰彇閿洏淇℃伅,while寰幆
    绛旓細while 鍩烘湰鏍煎紡 while 鍒ゆ柇璇彞 锛    # python璇彞 鐩村埌鍒ゆ柇鍙ョ粨鏋滀负false锛屽惊鐜粨鏉 letter1 = 3 while letter1 < 10 :    letter1 += 4     print(letter1)print('---')浣跨敤while璇彞锛岀敤瀛楀吀鍚嶆垨鑰呭垪琛ㄥ悕浣滀负鍒ゆ柇鏉′欢鏃讹紝鍙湁鍒楄〃锛堝瓧鍏革級鍐呭厓绱犱负...
  • 扩展阅读:学python后到底能干什么 ... 学python最佳年龄 ... python培训班学费一般多少 ... python编程入门自学 ... c++和python先学哪个 ... 学python有前途吗 ... python手机版下载官方 ... python网站 ... python符号含义大全 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网