Python if语句 for语句 while循环的练习

练习一:

考试成绩的问题:提示用户输入成绩,判断是属于哪个水平,将结果打印到控制台。60以下不及格,60分以上为及格,70分至80分为合格,80分至90分为良好,90分以上为优秀。

例如:

请输入考试成绩:85

你的成绩是 85 ,成绩良好。

score = int( input( "请输入考试成绩:" ) )

if score < 60:

print( "成绩不及格" )

elif (score >= 60) and (score < 70):

print( "你的成绩是:{:}\n成绩及格".format(score) )

elif (score >= 70) and (score < 80):

print( "你的成绩是:{:}\n成绩合格".format(score) )

elif (score >= 80) and (score < 90):

print( "你的成绩是:{:}\n成绩良好".format(score) )

elif (score >= 90) and (score <= 100):

print( "你的成绩是:{:}\n成绩优秀".format(score) )

else:

print( "请输入0-100以内的数字" )

练习二:

打印20次 "hello python" 并在前面加上序号

打印如:

1 - hello python

2 - hello python

...

for k in range( 1, 21 ):

print( str( k ) + " - " + "hello python" )

练习三:

在火车站安检的例子中,添加功能:

进站以后,如果行李大于100公斤,提示“行李太多,需要托运”

否则提示“可以开心的坐火车了”

has_ticket = True

knife_len = 10

luggage = 101

if has_ticket:

print( "Hello passenger,please go to the checking part." )

if knife_len >= 20:

print( "Dear passenger,your knif is too long({}cm),you are not allowed to check in".format( knife_len ) )

elif luggage > 100:

print( "You luggage is too heavy,please checked your baggage!" )

else:

print( "Check in!Have a nice trip!" )

else:

print( "You are not allowed to check in,please buy your ticket at the ticket lobby. " )

练习四:

提示用户分别输入二个整数,然后在控制台打印最大值和最小值。

num1, num2 = eval( input( "Please input two number(like: a,b)" ) )

if num1 > num2:

print( "最大值:{:},最小值{:}".format( num1, num2 ) )

elif num2 > num1:

print( "最大值:{:},最小值{:}".format( num2, num1 ) )

else:

print( str( num2 ) + "=" + str( num1 ) )

练习五:

import random

list1 = [1, 2, 3]

list1[0] = random.randint( 0, 100 )

list1[1] = random.randint( 0, 100 )

list1[2] = random.randint( 0, 100 )

list1.sort()

print( "最大值:{:},最小值{:}".format( list1[2], list1[0] ) )

练习六:

使用 while 计算 1 + 2 + 3 + 4 + .... + 100 的和,并打印结果

num = 1

sum1 = 0

while num >= 1:

sum1 = sum1 + num

if num == 100:

break

num = num + 1

print( sum1 )

练习七:

import random

num = 0

while True:

print( random.randint( 0, 100 ) )

num += 1

if num == 10:

break

练习八:

使用 while 循环 ,打印 10 个 1 -- 100 的随机数,并在循环结束后,打印最大值

import random

list=[]

for k in range(1,11):

list.append(random.randint(0,100))

list.sort()

print("最大值:{:}最小值:{:}".format(list[9],list[0]))练习九:

使用 while 循环,获得 1 -- 100 内所有能被 7 整除的数,并在控制台打印

num = 1

while num<=100:

if num % 7 == 0:

print( num )

num += 1

练习十:

使用 while 循环,统计 1 -- 100 内所有能被 7 整除的数的个数,打印结果

num = 0

sum1 = 0

while num <= 100:

num += 1

if num % 7 == 0:

sum1 += 1

print( sum1 )

练习十一:

模仿石头剪刀布,实现 老虎、杠子、鸡的游戏。

游戏规则:

老虎吃鸡,鸡吃虫,虫吃杠子,杠子打老虎。

假设:

用 1 表示老虎 2表示鸡  3 表示虫子  4 表示杠子

电脑使用随机数,用户手工输入。

import random

while True:

computer = random.randint( 1, 4 )

try:

player = int( input( "Please input your answer:(老虎/1 鸡/2 虫/3 杠子/4)" ) )

except ValueError:

print( "Please input the right answer,try again!" )

continue

if ((player == 1) and (computer == 2)) or ((player == 2) and (computer == 3)) or (

(player == 3) and (computer == 4)) or ((player == 4) and (computer == 1)):

print( "player:{:},computer{:},YOU WIN!".format( player, computer ) )

elif player == computer:

print( "The same,Once more!" )

elif not (player== 1 or player == 2 or player == 3 or player==4):

print( "please input the right answer,try again!" )

else:

print( "player:{:},computer{:},YOU LOSE!".format( player, computer ) )

练习十二:

使用while 循环,连续玩10把石头、剪刀、布 的游戏,游戏结束后,统计用户赢的次数

num = 0

times = 0

while True:

computer = random.randint( 1, 3 )

times += 1

if times == 11:

print( "In 10 times you have win {:}次".format( num ) )

break

try:

player1 = int( input( "Please input your answer:(石头(1)/剪刀(2)/布(3)" ) )

except ValueError:

print( "Please input the right answer,try again!" )

continue

if player1 == computer:

print( "computer:{:} player:{:} You input the same with computer,Try again!".format( computer, player1 ) )

elif ((player1 == 1) and (computer == 2)) or ((player1 == 2) and (computer == 3)) or (

(player1 == 3) and (computer == 1)):

print( "computer:{:} player:{:} You win! Once more!".format( computer, player1 ) )

num += 1

elif not (player1 == 1 or player1 == 2 or player1 == 3):

print( "please input the right answer,try again!" )

else:

print( "computer:{:},player:{:} You lose!Try again!".format( computer, player1 ) )

以上为一些小练习,不同的人可能使用的方法不同,笔者给出的答案只作为参考,并且还有很大的优化空间,欢迎指正批评。

python循环语句if语句的题目_Python if语句 for语句 while循环的练习相关推荐

  1. python语言的三种基本结构_Python语言基础分支语句、循环语句.PPT

    * * 循环结构回顾 for循环 for 循环作为编程语言中最强力的特征之一 Python可以使用for语句循环遍历整个序列的值 for循环所做的概括为一句话: 于- 其中的每一个元素,做-事情 * ...

  2. python if 语句第一个不程序_python小程序1--if语句

    题目: 定义出一个name并提前赋值,如果name是python就打印welcome boss,否则打印name. 代码如下: flae=False name='lill' if name=='pyt ...

  3. 用python循环结构计算派的值_Python程序设计实验报告四:循环结构程序设计(设计型实验)...

    安徽工程大学 Python程序设计 实验报告 班级  物流191   姓名姚彩琴学号3190505129 成绩 日期     2020.4.8     指导老师修宇 [实验名称]实验四 循环结构程序设 ...

  4. python查看关键字列表的命令是_Python 41 完整查询语句 和 一堆关键字

    一:完整查询语句 1.拷贝表 *** create table copy_table select *from customer ; 拷贝结构 与数据 create table copy_table ...

  5. python中遍历字典判断是否存在_Python基础之(判断,循环,列表,字典)

    一.python介绍 Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程.Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在 ...

  6. pythonif语句的多分支使用_Python多分支if语句的使用

    注意:if语句代码是从上往下执行的,当执行到满足条件的语句时,代码会停止往下执行 注意:if语句后面要加上冒号 score = int (input("score:")) if s ...

  7. python的for语句条件_Python中的条件选择和循环语句

    Python中的条件选择和循环语句 同C语言.Java一样,Python中也存在条件选择和循环语句,其风格和C语言.java的很类似,但是在写法和用法上还是有一些区别.今天就让我们一起来了解一下. 一 ...

  8. python range 步长为负数_Python入门第7课,循环结构学习,for语句range函数的3种用法...

    上课前,大陈带领学生们一起回顾和梳理前面学过的知识. 体验课,Python与人工智能初体验. 第1课,输出语句及赋值语句. 第2课,输入语句学习. 第3课.第4课,学习条件控制if语句. 第5课.第6 ...

  9. python分支语句_Python语言基础分支语句、循环语句.ppt

    * * 循环结构回顾 for循环 for 循环作为编程语言中最强力的特征之一 Python可以使用for语句循环遍历整个序列的值 for循环所做的概括为一句话: 于- 其中的每一个元素,做-事情 * ...

最新文章

  1. python中ht_python – 解析HTSQL时处理语法歧义
  2. python 获取类名
  3. 算法自动化测试的挑战与思考
  4. SVN在另类环境中实现自动提交的方法
  5. rpm -e --nodeps_微课 | rpm的思维导图
  6. 熊猫的python小课账号_学习python中的pandas有没有好的教程推荐?
  7. 5 查询一个小时前_2021国考成绩查询系统登录入口
  8. AJAX JSON之讲解
  9. 输出魔方矩阵(C语言实现)
  10. 「新功能」对接金蝶云星空K3 Cloud插件支持版本升级
  11. 米尔MYD-JX8MPQ yocto 编译流程 (记录)
  12. 战舰少女r魔盒服务器维护,战舰少女r魔盒官方
  13. python+opencv代码给证件照换底色(别再用PS啦)(转载)
  14. 输入上/下标数字以及字母
  15. 从前慢-谷粒商城篇章4
  16. 王川:小米盒子的产品观
  17. 如何在宝贝详情页中制作一张图片多个链接
  18. 四、Solr数据源配置(JNDI、DIH)及定时重做索引
  19. shared_ptr
  20. 利用BCD码计数器和7段码转换器子电路

热门文章

  1. 数学 {n次方根,根号,平方根}
  2. IDEA中使用Docker插件构建镜像并推送至私服Harbor
  3. Vue项目——文章发布和修改
  4. CF1324F Maximum White Subtree
  5. 用户登录 验证数据库
  6. 傅连仲主编的《计算机应用基础》,学生教学论文,关于计算机应用基础课程标准研读的必要性相关参考文献资料-免费论文范文...
  7. 5V2A移动电源管理芯片脚位兼容IP5306
  8. Dynamics CRM Xrm.Utility.openEntityForm passing lookup parameters
  9. HObject,unsigned char的相互转换
  10. 【Prism】MEF版Commanding