【python基础】之条件判断和循环

一、if判断语句

#仅有if,此时满足条件则执行,否则什么也不做

score = int(input(‘>>:‘))

if score>90:
    print(‘优秀‘)

#可以给if添加一个else语句,意思是,如果if判断是False,这时不执行if的内容,去执行else下的内容

score = int(input(‘>>:‘))

if score>=90:
    print(‘优秀‘)
else:
    print(‘及格‘)

#若需要判断多个条件时,可以用elif(else if的缩写)

score = int(input(‘>>:‘))

if score>=90:
    print(‘A‘)
elif score>=80:
    print(‘B‘)
elif score>=60:
    print(‘C‘)
else:
    print(‘不及格‘)

原文地址:https://www.cnblogs.com/moshang-huakai/p/9325799.html

时间: 2024-08-08 11:27:21

【python基础】之条件判断和循环的相关文章

python基础知识--条件判断和循环

一.输入输出 python怎么来接收用户输入呢,使用input函数,python2中使用raw_input,接收的是一个字符串,输出呢,第一个程序已经写的使用print,代码入下: 1 name=input('Please enter your name:') #把接收到的值赋给name变量 2 print(name)#输出接收到的输入 input在接收输入的时候,是可以看到你输入的值的,如果是输入密码这样的呢,不想让别人看到你的密码,怎么办呢,就需要用到一个标准库,getpass,什么是标准库

python基础之条件判断和循环

1.条件判断 age = 3 if age >= 18: print('adult') elif age >= 6: print('teenager') else: print('kid') elif是else if的缩写,完全可以有多个elif. 2.循环 python循环有两种, 一种是for...in循环,依次把list或者tuple中的每个元素迭代出来. names = ["geg","gege","geege"]; for

Python 基础之三条件判断与循环

If……else 基本结构: If condition: do something else: do something 或者 If condition: do something elif condition: do something else: do something Python 通过缩进来确定一段代码的作用域,同一级别的代码缩进要保持一致,官方建议是4个空格. 提示:IndentationError,都为代码中存在缩进错误. 小练习: # 猜luck number,n=6 # 猜的数

Python入门基础之条件判断和循环

Python之if语句 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,可以用if语句实现: age = 20 if age >= 18: print 'your age is', age print 'adult' print 'END' 注意: Python代码的缩进规则.具有相同缩进的代码被视为代码块,上面的3,4行 print 语句就构成一个代码块(但不包括第5行的print).如果 if 语句判断为 True,就会执行这个代码块. 缩进请严格按照Python的习惯写法

第二章 Python基础之条件判断,循环

1.条件判断if   else 如: if age >=18 and scroe <= 100: #py中不使用&标识并集 等于使用== 不等于使用!= print('成年') else:                       #中间可以使用elif 加如其他条件     print ('dd') 2.循环 遍历 迭代 (1)while循环 需要先定义一个计数器,需要有结束的条件,否则会死循环 count =0 while count<20:     print('tian

python基础之条件控制与循环

Python3 条件控制 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= 18: print('your age is', age) print('adult') 根据Python的缩进规则,如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么

Python 基础之条件判断

条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= 18: print('your age is', age) print('adult') 根据Python的缩进规则,如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做. 也可以给if添加一个else语句,意思是,如果if判断是False,不要执行if的内容,去把else执行

Python - 基础知识 - 条件判断

1. 简单的if/else条件判断 judge_flow.py name = input("Please input name: ") if name == 'master': print('Hello Master') password = input('Please input password: ') if password == 'abc123': print('Access granted.') else: print('Wrong password!') else: pri

python基础(3)--条件判断循环语句与列表解析,生成器

判断循环语句语法说明: 1,if if boolean_expression: ... elif boolean_expression: ... else ... 2,while while boolean_expression: while_suite if boolean_expression2: continue if boolean_expression3: break else: else_suite 3,for for expression in object: for_suite

python基础笔记:判断与循环

判断: #根据身高为1.75,体重为65的小明的bmi输出小明的身材 h=1.75 w=65 bmi=w/(h*h) if bmi<18.5: print('过轻') elif bmi<=25: print('正常') elif bmi<=28: print('过重') elif bmi<=32: print('肥胖') else: print('严重肥胖') #注意冒号的使用 for 循环 #输出 1+2+3+...+1000 的值 sum=0 for x in range(10