Python_基础_2

1条件语句

'''
语法 if条件:
        条件成立执行的代码1
        ...
有缩进得到属于if语句条件块
'''

age = int(input('请输入您的年龄:'))
#例子
if age >= 18:
    print(f'您的年龄是{age},已经成年可以上网')
else:
    print(f'您的年龄是{age},您未成年')


if 条件1:

elif 条件2:

else:
#例子
age = int(input('请输入您的年龄:'))

if age < 18 :
    print(f'年龄是{age}小于18为童工,不合法')
elif (age >= 18) and (age <= 60): #elif 18 <= age <= 60:
    print(f'年龄是{age},为合法工作年龄')
else:
    print(f'年龄是{age},为退休年龄')

'''
if嵌套
'''
#例子:公交车
money_bus = 1
seat_bus = int(input('请输入公交车还剩余多少座位:'))

money_own = int(input('请输入您有多少钱:'))

if money_own >= money_bus :
    print('您可以乘坐公交车')
    if seat_bus >= 1 :
        print('您可以坐下')
    else:
        print('您不可以坐下')
else:
    print('您没有足够的钱乘坐公交车')

#随机数  可以用来实现剪刀石头布游戏
import random
random.randint(0,2)

# 三目运算符
```python
a, b = 1, 2
c = a if a > b else b
print(c) # 2

2.1while循环语句

# 1--100 的和
i = 1
result = 0
while i <= 100 :
    result += i
    i += 1
print(result)

# 1-100的偶数和  法1
i = 1
result = 0
while i <= 100 :
    if i % 2 == 0 :
        result += i
    i += 1
print(result)

# 1-100的偶数和  法2

i = 2
result = 0
while i <= 100 :
    result += i
    i += 2
print(result)

#遇到continue就开启下一次,遇到break就跳出循环
i = 1
while i <= 5 :
    if i == 4 :
        print('吃饱了不吃了')
         break
    if i == 3 :
        print(f'这个苹果是坏的,呀还没吃呢')
        i += 1
        continue
    print(f'吃了第{i}个苹果')
    i += 1


#嵌套练习

i = 1
while i <= 5 :
    j = 1
    while j <= 5 :
        print('*',end=' ')
        j += 1
    print()
    i += 1

i = 1
while i <= 5 :
    j = 1
    while j <= i :
        print('*', end=' ')
        j += 1
    print()
    i += 1

i = 1
while i <= 9 :
    j = 1
    while j <= i :
        print(f'{j}*{i}={i * j}',end=' ')
        j += 1
    print()
    i += 1

2.2for循环语句

str1 = 'i love you '
for j in str1 :
    if j == 'o':
        print(f'遇到{j}不打印')
        break
    print(j)

str1 = 'i love you '
for j in str1 :
    if j == 'i':
        print(f'遇到{j}不打印')
        continue
    print(j)

#while...else...
else 循环正常结束之后需要执行的放这里


i = 1
while i <= 5 :
    print('我错了')
    i += 1
else:
    print('原谅我了')

i = 1
while i <= 5 :
    if i == 3 :
        print('哎呀怎么这么凉啊')
        break
    print('我错了')
    i += 1
else:
    print('原谅你了')

i = 1  #continue也正常结束
while i <= 5:
    if i == 3:
        print('哎呀怎么这么凉啊')
        i += 1
        continue
    print('我错了')
    i += 1
else:
    print('原谅你了')
str1 = 'i love you'
for j in str1:
    print(j)
else:
    print('正常结束')

str1 = 'i love you'
for j in str1:
    if j == 'l':
        print('遇到l不打印')
        break
    print(j)
else:
    print('正常结束')

str1 = 'i love you'
for j in str1:
    if j == 'l':
        print('遇到l不打印')
        continue
    print(j)
else:
    print('正常结束')
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值