怎么用Python的for循環和while循環算平均數分別用for和while在1到10
m = 0for i in range(5): s = raw_input() m += float(s)/5.0 print 'the average is: ',m i = 0m = 0while i 追問: m += float(s)/5.0 是什么意思啊 還有這個程序好想沒有關于1到10中挑選五個數字啊 追答: s就是是你輸入的字符串,比如'7',float(s)是將這個字符串轉換成小數,這樣就可以運算了。
m += 1相當于 m = m+1,那5個數你自己輸入嘛。 用戶 2017-09-27 舉報。
python循環語句
Python While循環語句 Python 編程中 while 語句用于循環執行程序,即在某條件下,循環執行某段程序,以處理需要重復處理的相同任務。
其基本形式為:while 判斷條件: 執行語句…… 執行語句可以是單個語句或語句塊。判斷條件可以是任何表達式,任何非零、或非空(null)的值均為true。
當判斷條件假false時,循環結束。實例:#!/usr/bin/python count = 0 while (count print 'The count is:', count count = count + 1 print "Good bye!" 以上代碼執行輸出結果: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 語句時還有另外兩個重要的命令 continue,break 來跳過循環,continue 用于跳過該次循環,break 則是用于退出循環,此外"判斷條件"還可以是個常值,表示循環必定成立,具體用法如下:# continue 和 break 用法 i = 1 while i i += 1 if i%2 > 0: # 非雙數時跳過輸出 continue print i # 輸出雙數2、4、6、8、10 i = 1 while 1: # 循環條件為1必定成立 print i # 輸出1~10 i += 1 if i > 10: # 當i大于10時跳出循環 break 無限循環 如果條件判斷語句永遠為 true,循環將會無限的執行下去,如下實例:#coding=utf-8#!/usr/bin/python var = 1 while var == 1 : # 該條件永遠為true,循環將無限執行下去 num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!" 以上實例輸出結果: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 "*", line 5, in num = raw_input("Enter a number :") KeyboardInterrupt 注意:以上的無限循環你可以使用 CTRL+C 來中斷循環。
循環使用 else 語句 在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是通過 break 跳出而中斷的)的情況下執行,while … else 也是一樣。#!/usr/bin/python count = 0 while count print count, " is less than 5" count = count + 1 else: print count, " is not less than 5" 以上實例輸出結果為: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 簡單語句組 類似if語句的語法,如果你的while循環體中只有一條語句,你可以將該語句與while寫在同一行中, 如下所示:#!/usr/bin/python flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!" 注意:以上的無限循環你可以使用 CTRL+C 來中斷循環。
python中for循環怎么用
1. for 循環介紹
復制代碼代碼如下:
>>> li = ['a', 'b', 'e']
>>> for s in li: (1)
。 print s (2)
a
e
>>> print "\n".join(li) (3)
a
e
(1) for 循環的語法同 list 解析相似。li 是一個 list,而 s 將從第一個元素開始依次接收每個元素的值。
(2) 像 if 語句或其它任意縮進塊,for 循環可以包含任意數目的代碼行。
(3) 這就是你以前沒看到過 for 循環的原因:至今我們都不需要它。太令人吃驚了,當你想要的只是一個 join 或是 list 解析時,在其它語言中常常需要使用 for 循環。
要做一個 “通常的” (Visual Basic 標準的) 計數 for 循環也非常簡單。
2. 簡單計數
復制代碼代碼如下:
>>> for i in range(5): (1)
。 print i
>>> li = ['a', 'b', 'c', 'd', 'e']
>>> for i in range(len(li)): (2)
- 104 -Dive Into Python http://**
。 print li[i]
python基礎題 while循環語句
當運行環境為python 2時,期望input()輸入字符串時應使用引號括起來,否則將報錯。所以答案為B
當運行環境為python 3時,input() 默認接收到的是 str 類型,答案為A。之所以不是D,是因為input獲取的是整個字符串,而不是遍歷單個字符
參考資料:網頁鏈接
我的環境為py2,第一次用引號括起來時輸出abcq;第二次直接輸入abcq,報錯
python三個for循環怎么使用
一般來說,for 循環是利用的遍歷來實現的
基礎語法是
for i in sequence:
statements(s)
另外一種執行循環的遍歷方式是通過索引
例如:
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print '當前水果 :', fruits[index]
第三種方式就是可以和else連用,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是通過 break 跳出而中斷的)的情況下執行
例如:
for num in range(10,20): # 迭代 10 到 20 之間的數字
for i in range(2,num): # 根據因子迭代
if num%i == 0: # 確定第一個因子
j=num/i # 計算第二個因子
print '%d 等于 %d * %d' % (num,i,j)
break # 跳出當前循環
else: # 循環的 else 部分
print num, '是一個質數'
關于python 的while的用法
L=[1,2,4,8,16,32,64] #定義一個列表
x=5 #把5賦予x(這樣x是全局的)
found=i=0 #把0賦予found和i
while not found and iif 2**x==L[i]: (L[i]是什么意思)### L[i]代表取L的第i個值,L[0]就是1,L[2]就是4,L[3]得8
found=1 (不懂)### 給變量found賦值1
else:
i=i+1 (這個我遇到多次了,有點不明白)### 意思是 i增1,執行前如果i為1,執行后i就等于2
if found: (found有是什么)### 如果i為真(不等于0就是真),就打印下面的
print 'at index', i #輸出字符串'at index'和i人值
else: ### 如果i為假
print x, 'not found ' #輸出x的值和字符串'not found'
轉載請注明出處華閱文章網 » python循環語句