python入门学习笔记(第五章)之if语句(超详细)-------------两个if-else小游戏:(猜数小游戏)与(分数分等级A、B、C、D小游戏)

文章目录

    • 一个简单的示例
    • 条件测试
    • in: 检测特定值是否包含在列表中
    • not in: 检测特定值是否不包含在列表中
  • if语句
      • if-else
      • if-elif-else结构
      • 使用多个eilf代码块
      • 省略else模块
    • 测试多个条件:多个if
    • 用if语句处理列表
      • 确定列表不是空的
      • 使用多个列表
  • 猜数小游戏
    • 版本一
    • 版本二
    • 版本三
      • while循环
    • 版本四
      • random模块
        • randint()函数:生成随机数
  • 分数分等级A、B、C、D小游戏
    • 版本一:多个if排列
    • 版本二:if-else层层嵌套(套娃)
    • 版本三:if-elif
    • 版本四:while循环有限次数
    • 版本五:while循环无限次数






一个简单的示例

age = 19
if age >= 18:
    print('You are old enough to vote!')
You are old enough to vote!

条件测试

>>> car = 'bnw'
>>> car == 'bnw'
True

>>> car = 'Audi'
>>> car == 'bnw'
False

>>> car = 'Audi'
>>> car.lower() == 'adui'   #暂时的,不覆盖
True
>>> car
>'Adui'

in: 检测特定值是否包含在列表中

>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>>> 'mushrooms' in requested_toppings
True
>>> 'pepperoni' in requested_toppings
False

not in: 检测特定值是否不包含在列表中

banned_users = ['andrew', 'carlina', 'david']
user = 'marie'

if user not in banned_users:
    print(user.title() + ', you can post a response if  you wish.')
Marie, you can post a response if  you wish.




if语句

if-else

age = 17
if age >= 18:
    print('You are old enough to vote!')
    print('Have you registered to vote yet?')
else:        #输出这个
    print('Sorry,you are too young to vote.')
    print('Please register to vote as soon as you turn 18!')
Sorry,you are too young to vote.
Please register to vote as soon as you turn 18!

if-elif-else结构

age = 12
if age < 4:
    print('Your admission cost in $0.')
elif age < 18:                #输出这个
    print('Your admission cost in $5.')
else:
    print('Your admission cost in $10.')
Your admission cost in $5.

使用多个eilf代码块

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
else:
    price = 5

print('Your admission cost is $' + str(price) + '.')
Your admission cost is $5.

省略else模块

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
elif age >= 65:
    price = 5

print('Your admission cost is $' + str(price) + '.')
Your admission cost is $5.

测试多个条件:多个if

requested_toppings = ['mushrooms', 'extra cheese']

if 'mushrooms' in requested_toppings:    #添加
    print('Adding mushrooms.')
if 'pepperoni' in requested_toppings:    #没有这个,没加进去
    print('Adding pepperoni.')
if 'extra cheese' in requested_toppings: #添加
    print('Adding extra cheese.')

print('\nFinished making your pizza!')
Adding mushrooms.
Adding extra cheese.

Finished making your pizza!



用if语句处理列表

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']

for requested_topping in requested_toppings:     #遍历列表
    print('Adding ' + requested_topping + '.')

print('\nFinished making your pizza!')
Adding mushrooms.
Adding green peppers.
Adding extra cheese.

Finished making your pizza!

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']

for requested_topping in requested_toppings:     #遍历
    if requested_topping == 'green peppers':
        print('Sorry, we are out of green peppers right now')
    else:
        print('Adding ' + requested_topping + '.')

print('\nFinished making your pizza!')
Adding mushrooms.
Sorry, we are out of green peppers right now
Adding extra cheese.

Finished making your pizza!

确定列表不是空的

requested_toppings = []

if requested_toppings:  #默认非空执行if里面的
    for requested_topping in requested_toppings:
        print('Adding' + requested_topping + '.')
    print('\nFinished making your pizza!')
else:                  #为空时,执行else
    print('Are you sure you want a plain pizza?')
Are you sure you want a plain pizza?

使用多个列表

available_toppings = ['mushrooms', 'olives', 'green peppers',
                      'pepperoni', 'pineapple', 'extra cheese']

requested_toppings = ['mushrooms', 'fresh fries', 'extra cheese']   #中间的没有

for requested_topping in requested_toppings:       #遍历需要的列表
    if requested_topping in available_toppings:    #如果需要的在现有的里面
        print('Adding ' + requested_topping + '.')
    else:                                          #如果需要的不在现有的里面
        print("Sorry, we don't have " + requested_topping + '.')

print('\nFinished making your pizza!')
Adding mushrooms.
Sorry, we don't have fresh fries.
Adding extra cheese.

Finished making your pizza!






猜数小游戏

版本一

只能输入数字,判断对错

temp=input('猜数字,请输入数字:')   #输入的为字符型
guess=int(temp)       #字符型转换整型
if guess==8:          #英文冒号
    print('Yes')      #缩进 
else:
    print('No')



版本二

输入数字,判断对错,错的会告诉大了还是小了

temp=input('猜数字,请输入数字:')
guess=int(temp)
if guess==8:
    print('Yes')
else:                #else里面再嵌套一个if-else
    if guess<8:
        print('小了')
    else:            #大于8
        print('大了')
print('结束了,不玩了')



版本三

while循环

能多次输入,判断对错,错的会告诉大了还是小了,直到对为止结束

temp=input('猜数字,请输入数字:')
guess=int(temp)
while guess !=8:   #while循环,直到对为止
    temp=input('猜错了,重新输入')       #重新输入
    guess = int(temp)                  #重新转化为整型
    if guess==8:
       print('Yes')
    else:
        if guess<8:
           print('小了')
        else:
           print('大了')
print('结束了,不玩了')



版本四

random模块

randint()函数:生成随机数

示例:

import random            #import用于调用
a = random.randint(0,2)  #产生0到2的随机数,三种可能(0,1,2)
print(a)

import random                      #导入random模块,模块后缀为.py
secret = random.randint(1, 10)      #引用randint函数
print(secret)
随机生成110的数




要猜的数字随机生成

import random     #import调用random模块
secret = random.randint(1,10)     #调用import模块中内置函数randint()生成随机数,随机数为1到10
print('开始游戏')
temp=input('猜数字,请输入数字:')
guess=int(temp)

while guess !=secret:
    temp=input('猜错了,重新输入')
    guess = int(temp)
    if guess==secret:
       print('Yes')
    else:
        if guess<secret:
           print('小了')
        else:
           print('大了')
print('结束了,不玩了')









分数分等级A、B、C、D小游戏

版本一:多个if排列

score = int(input('请输入一个分数:'))
if 100 >= score >= 90:
    print('A')
if 90 > score >= 80:
    print('B')
if 80 > score >=60:
    print('C')
if 60 > score >= 0:
    print('0')
if score <0 or score > 100:
    print('输入错误!')



版本二:if-else层层嵌套(套娃)

score = int(input('请输入一个分数:'))
if 100 >= score >= 90:
    print('A')
else:       #else里面细分其他情况
    if 90 > score >= 80:
       print('B')
    else:
         if 80 > score >=60:
            print('C')
         else:
            if 60 > score >= 0:
               print('0')
            else:
                 print('输入错误!')



版本三:if-elif

score = int(input('请输入一个分数:'))
if 100 >= score >= 90:
    print('A')
elif 90 > score >= 80:
    print('B')
elif 80 > score >=60:
    print('C')
elif 60 > score >= 0:
    print('0')
else:
    print('输入错误!')



版本四:while循环有限次数

n = 0
while n < 5:
    score = int(input('请输入一个分数:'))
    if 100 >= score > 90:
       print('A')
    elif 90 >= score > 80:
       print('B')
    elif 80 >= score > 60:
       print('C')
    elif score<=60:
       print('D')
    else:
       print('输入错误')
    n = n+1     #记录循环次数,好跳出循环



版本五:while循环无限次数

E = input('请输入分数')
score = int(E)
while score <= 100:
  E = input('请输入分数')   #再一次输入
  score = int(E)            #转换为整型

  if 100 >= score >= 90:
    print('A')
  elif 90 > score >= 80:
    print('B')
  elif 80 > score >= 60:
    print('C')
  elif score < 60:
    print('D') 
if score > 100:     #与while对齐
  print('输入错误')
print('结束')

你可能感兴趣的:(python)