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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

二十四桥_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值