3.20 python

@3.20 python
input 命令

a_num = intinput('请输入第一个数字:'))
b_num = int(input('请输入第二个数字:'))
result = a_num + b_num
print('%d + %d 的结果是 %d' %(a_num, b_num, result))

得到以下结果:
在这里插入图片描述
if
如果a>b则进行加法运算,如果 a<b则进行减法运算

a = 10
b = 20
if a > b:
    ret = a + b
else:
    ret = a - b
print('ret=%d' % ret)
#获得用户输入的用户名
user_name = input('请输入您的名字:')
# 正确的用户名
correct_name = '桂圆'
if user_name == correct_name:
    print('欢迎 %s 登录' % user_name)
else:
    print('登录失败')

=是赋值
==是判断
结果如下:
在这里插入图片描述
and or


```python

#导入随机数据库
import random
#用户出拳
user_quan = input('请出拳:石头(0) 剪刀(1) 布(2)')
#电脑随机出拳
computer_quan = random.randint(0,2)
规定赢的条件
if (user_quan == 0 and computer_quan == 1) or \
    (user_quan == 1 and computer_quan == 2) or \
    (user_quan==2 and computer_quan==0):
    print('你赢了')
elif (user_quan == computer_quan) :
    print('平局')
else:
    print('你输了')

```python
i=1
while i<=100:
    print(i)

    i +=1

累加1到100所有数字总和

i = 1
total = 0
while i <= 100:
    total = total + i
    i += 1

print(total)

#第n行打印n个¥

i = 1
while i <=6:
    print('¥'*i)
    i+=1

****continue ****
循环时跳过一个数字

index = 1
my_sum = 0

while index <= 100:

    if index == 50:
    #划重点 跳过index=50之后 index要+1 否则会陷入死循环
        index += 1
        continue

    my_sum += index
    index += 1

print(my_sum)
def my_add(a,b):
    ret = a + b
    return ret

ret = my_add(10,20)

final_result = ret + 100
print('最终结果',final_result)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值