大家好,本文将围绕笨办法学python 3电子书下载展开说明,笨办法学python3pdf完整版是一个很多人都想弄明白的事情,想搞清楚笨办法学python3进阶篇pdf需要先了解以下几个事情。

文章目录
习题11:提问
age = input("How old are you?")
height = input("How tall are you?")
weight = input("How much do you weigh?")
print ("So, you're %r old, %r tall and %r heavy." %(age, height, weight))
输出结果为
How old are you?35
How tall are you?180
How much do you weigh?140
So, you're '35' old, '180' tall and '140' heavy.
问题15、16、17:文件的读写操作
问题20:函数和文件
from sys import argv
, input_file = argv
def print_all(f):
	print (f.read())
	
def rewind(f):
	f.seek(0)
	
def print_a_line(line_count, f):
	print (line_count, f.readline())
#打开文件	
current_file = open(input_file,encoding = 'utf-8')
#展示文件全部内容
print ("First let's print the whole file:\n")
print_all(current_file)
#重新定义展示文章行号
print ("Now let's rewind, kind of like a tape.")
rewind(current_file)
#展示文件的前三行内容
print ("Let's print three lines:")
current_line = 1 #设置开始行行号
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
输出结果为
First let's print the whole file:
To all the people out there.
I say I don't like my hair.
I need to shave it off.
Now let's rewind, kind of like a tape.
Let's print three lines:
1 To all the people out there.
2 I say I don't like my hair.
3 I need to shave it off.
习题21:函数可以返回东西
def add(a, b):
    print ("ADDING %d + %d" % (a, b))
    return a + b
def subtract(a, b):
    print ("SUBTRACTING %d - %d" % (a, b))
    return a - b
def multiply(a, b):
    print ("MULTIPLYING %d * %d" % (a, b))
    return a * b
def divide(a, b):
    print ("DIVIDING %d / %d" % (a, b))
    return a / b
print ("Let's do some math with just functions!")
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print ("Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height,weight, iq))
print('\n')#换行符
# A puzzle for the extra credit, type it in anyway.
print ("Here is a puzzle.")
what = add(age, subtract(height, multiply(weight, divide(iq,2
 
                   
                   
                   
                   最低0.47元/天 解锁文章
最低0.47元/天 解锁文章
                           
                       
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   1020
					1020
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            