条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= 18: print('your age is', age) print('adult') 根据Python的缩进规则,如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做. 也可以给if添加一个else语句,意思是,如果if判断是False,不要执行if的内容,去把else执行…
pyhton if 语句 if 语句后接表达式,然后用: 表示代码块. age = 20 if age >= 18: print 'your age is', age print 'adult' your age is 20 adult python if-else if age >= 18: print 'adult' else: print 'teenager' 利用 if ... else ... 语句,我们可以根据条件表达式的值为 True 或者 False ,分别执行 if 代…
一. for循环实例 1.循环字符串 Python Shell: for i in "hello": print(i) h e l l o 2.循环数组Python Shell: n = ['] for i in n: print(i) 1 2 3 3.一定次数的循环range() Python Shell: for i in range(2,11,2): print(i) 2 4 6 8 10 range(start,end,step),start为起始数(包含),end为截至(不包…
#/usr/bin/python #coding=utf-8 #@Time :2017/10/18 15:31 #@Auther :liuzhenchuan #@File :while 循环.py 示例1: n = 0 while True: print 'hello world' if n == 10: break n +=1 打印如下: hello world hello world hello world hello world hello world hello world hello…
1.在循环中使用continue输出1-10之间的奇数 num=0 while num <10: num += 1 if num %2 == 0: #--%--运算符,相除返回余数 continue print(num) 2.使用 active = True\False 设置循环标志 #调查用户梦想的度假胜地 responses={} name=input("What's your name?") resort=input("What's your dream reso…
# Author:"Mamba" import random setNum = random.randint(1,10) #print(setNum) count = 0 while count == 0: print('let`us play guess number games') print('your have three times ,now begin') guess = int(input("please input your number:")) i…