愚かな法学pythonの練習問題33

7561 ワード

愚かな法学pythonの練習問題33


練習を付け加える


1.このwhileサイクルを関数に変更し,試験条件(i<6)の6を変数に変換した.
i = 0
numbers = []
while i < 6:
        print "At the top i is %d" % i
        numbers.append(i)
        i = i + 1
        print "Numbers now: ", numbers
        print "At the bottom i is %d" % i

print "The numbers: "
for num in numbers:
        print num
   


Python

def whiletest(limit):
    i = 0
    numbers = []
    
    
    while i < limit:
        print "At the top i is %d" % i
        # i numbers 
        numbers.append(i)
    
        i = i + 1
        print "Numbers now: ", numbers
        print "At the bottom i is %d" % i
    return numbers      
        
    
    
# limit 
limit = int(raw_input('>' ))

# numbers
numbers = whiletest(limit)

print "The numbers: "
# numbers 
for num in numbers:
    print num
  • は、8行目の+1を定義するために使用できる関数に別のパラメータを追加し、
  • を任意に増加させることができる.
    def whiletest(limit, j):  
        i = 0
        numbers = []
        
        
        while i < limit:
            print "At the top i is %d" % i
            numbers.append(i)
            
            #j i 
            i = i + j 
            print "Numbers now: ", numbers
            print "At the bottom i is %d" % i
        return numbers      
            
        
        
    
    limit = int(raw_input('>' ))
    j = int(raw_input('>  '))
    numbers = whiletest(limit, j)
    
    print "The numbers: "
    for num in numbers:
        print num

    5.forループとrangeでスクリプトを書き直す
    numbers = []
    
    for i in range(6):
        print"At the top i is %d" % i
        numbers.append(i)
        
        
        print "Numbers now:", numbers
        
    
    print "The numbers:"    
    for i in numbers:
        print i