@3.20 python
input 命令
a_num = int(input('请输入第一个数字:'))
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)