python入门之if-else语句


一、if语句

if False:
    print('I can do it')
    print('believe me')
print('believe yourself')

"""
the data the input function has received is 'str' type,
so we should transform its type
"""
age = int(input('please input your age:'))
if age >= 18:
    print(f'your age is {age},and you can pass')
else:
    print(f"your age is {age},and you can't pass")
print('system close')

二、elif语句

age = int(input('please input your age :'))

if age < 18:
    print(f'your age is {age}, juveniles')

elif (age >= 18) and (age <= 60):
    print(f'your age is {age}, adult')

elif 60 < age <= 80:  # the 6 row and 9 row are equivalent
    print(f'your age is {age}, retiree')

elif age > 80:
    print(f'your age is {age}, high age')

三、if嵌套语句

money = int(input('coins please :'))

if money != 0:
    print('please get on')
    seat = int(input('how many seats are available :'))
    if seat != 0:
        print('you could get available seats')
    else:
        print('there is not free seat')

else:
    print('sorry, please get off')

四、else语句1

i = 0
while i < 5:
    print('媳妇儿, 我错了')
    i += 1
else:
    print('太好了, 媳妇儿原谅我了')

print('__________________')

i = 0
while i < 5:
    if i == 2:
        print('这一遍不真诚,别道歉了')
        break
    print('媳妇儿, 我错了')
    i += 1
else:
    print('太好了, 媳妇儿原谅我了')

print('__________________')

i = 0
while i < 5:
    if i == 2:
        print('这一遍不真诚,算了,继续吧')
        i += 1
        continue
    print('媳妇儿, 我错了')
    i += 1
else:
    print('太好了, 媳妇儿原谅我了')

五、else语句2

str1 = 'python'
str2 = ''
for i in str2:
    print(i)
else:
    print('循环正常结束')

print('-------------')

for i in str1:
    print(i)
    if i == 'n':
        break
    # print(i)
else:
    print('循环正常结束')

print('-------------')

for i in str1:
    if i == 't':
        continue
    print(i)
else:
    print('循环正常结束')

六、if-else语句举例1

"""
猜拳游戏
"""
import random  # import module

per = int(input('please show (1--石头; 2--剪刀; 3--布): '))
cer = random.randint(0, 2)  # the computer values are random numbers

if (per == 1 and cer == 2) or (per == 2 and cer == 3) or (per == 3 and cer == 1):
    print('player win')
elif per == cer:
    print('tie')
else:
    print('player lose')

七、if-else语句举例2

alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

customer = input('please input: ')
i = 0
j = 0
while i < len(customer):
    while j < 52:
        if customer[i] == alphabet[j]:
            if j <= 25:
                customer = customer.replace(customer[i], alphabet[j+26], 1)
            else:
                customer = customer.replace(customer[i], alphabet[j-26], 1)
            break
        else:
            j += 1
    i += 1
    j = 0
print(f'{customer}')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python中的if语句是一种条件语句,用于根据条件的真假来执行不同的代码块。if语句的基本语法是:if 条件表达式: 代码块。其中,条件表达式是一个返回布尔值的表达式,如果条件表达式为True,则执行代码块中的代码;如果条件表达式为False,则跳过代码块。\[1\]在Python中,if语句可以与else语句结合使用,形成if-else语句if-else语句的语法是:if 条件表达式: 代码块1 else: 代码块2。如果条件表达式为True,则执行代码块1;如果条件表达式为False,则执行代码块2。\[1\]此外,还可以使用if-elif-else语句来处理多个条件。if-elif-else语句的语法是:if 条件表达式1: 代码块1 elif 条件表达式2: 代码块2 else: 代码块3。如果条件表达式1为True,则执行代码块1;如果条件表达式1为False且条件表达式2为True,则执行代码块2;如果条件表达式1和条件表达式2都为False,则执行代码块3。\[2\]以上是Python中if语句的基本用法,可以根据具体的需求灵活运用。如果想深入学习if语句及其使用,可以参考《Python编程--从入门到实践》这本书。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [Python中 if 语句及其使用](https://blog.csdn.net/weixin_42570192/article/details/123611484)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【Python编程】if语句](https://blog.csdn.net/xing09268/article/details/127258846)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十四桥_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值