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

                           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 ) )

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

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值