博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第三天,小作业,表达式,while循环
阅读量:6717 次
发布时间:2019-06-25

本文共 3642 字,大约阅读时间需要 12 分钟。

num += 1 等价于 num = num + 1

num -= 1 等价于 num = num - 1
num *= 2 等价于 num = num * 2
num /= 2 等价于 num = num / 2
num //= 2 等价于 num = num // 2
num %= 2 等价于 num = num % 2
num **= 2 等价于 num = num ** 2

 

##输入3个数,比较数值大小并打印

#第一种

num1 = int(input("Num1:"))

num2 = int(input("Num2:"))
num3 = int(input("Num3:"))
max_num =0
if num1>num2:
max_num= num1
if max_num > num3:
print("Max NUM is",max_num)
else:
print("Max NUM is",num3)
else:
max_num = num2
if max_num > num3:
print("Max NUM is",max_num)
else:
print("Max NUM is",num3)

 

##第二种

num1=int(input("Num1:"))

num2=int(input("Num2:"))
num3=int(input("Num3:"))

a=num1-num2

b=num1-num3
c=num2-num3

if a>=0 and b>=0:

print("The MAX number is","num1:",num1)
elif a<=0 and c>=0:
print("The MAX number is","num2:",num2)
else :
print("The MAX number is","num3:",num3)
if a<=0 and b<=0:
print("The MIN number is","num1:",num1)
elif a>=0 and c<=0:
print("The MIN number is","num2:",num2)
else :
print("The MIN number is","num3:",num3)

 

 

 

##while循环 语句

格式:

while 条件:

命令

##while语句当条件为真时执行

'''age=50

flag=true                         ####--错误---,真值赋值首字母必须大写   flag=True

while flag=true:                  ####--错误---,while循环判断条件是判断真假,不用等式,应该为while flag:

  user_age=int(input("Guess my age:"))

  if user_age=age:                  ###---错误---判断中==表示等于

    print ("YES")

    flag=false                ####--错误---,真值赋值首字母必须大写 ,flag=False

  elif user_age<age:

    print("Pls try bigger.")

  else:

    print("Pls try smaller.")

print("END")'''

####while语句 执行命令需要首行缩进,不能在写字本上写代码!!!!缩进符格式不一样,报错"invalid character indentifier"

num=1

while num<=10:
  num+=1
  if num==11:                            ##while中加入if判断,if条件成立,continue   跳过后面的while循环语句.continue表示返回到while循环开始的地方.
    continue
  print(num)

##输出结果2 3 4 5 6 7 8 9 10  没有11

 

###while......:

  ........

  else:

  .......

####表示如果while循环正常结束,则执行else后面的语句,continue不会破坏while循环.非正常结束为break或者报错

 

print("Hello world.",end='')

print("Hello world.",end='')

print("Hello world.",end='')

#####输出3行变为1行  end=''

 

##九九乘法表

a=1
b=1
while a<=9:
  c=a*b
  print(a,'x',b,'=',c,end=' ')
  b+=1
  if b==10:
    print('')
    a+=1
    b=a
    continue

1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9

2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18
3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27
4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36
5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45
6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54
7 x 7 = 49 7 x 8 = 56 7 x 9 = 63
8 x 8 = 64 8 x 9 = 72
9 x 9 = 81

###错误示范

a=1

b=1
while a<=9:
  c=a*b
  print(a,'x',b,'=',c,end=' ')
  b+=1
  if b==9:
    print('')
    a+=1
    b=a
    continue                                             ########此程序在执行时,a=9以后,b不停累加,b>9,不会执行if嵌套,不执行a累加,则循环无限重复.

1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8

2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16
3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24
4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32
5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40
6 x 6 = 36 6 x 7 = 42 6 x 8 = 48
7 x 7 = 49 7 x 8 = 56
8 x 8 = 64
9 x 9 = 81 9 x 10 = 90 9 x 11 = 99 9 x 12 = 108 9 x 13 = 117 9 x 14 = 126 9 x 15 = 135 9 x 16 = 144 9 x 17 = 153 9 x 18 = 162 9 x 19 = 171 9 x 20 = 180 9 x 21 = 189 9 x 22 = 198 9 x 23 = 207 9 x 24 = 216 9 x 25 = 225 9 x 26 = 234 9 x 27 = 243 9 x 28 = 252 9 x 29 = 261 9 x 30 = 270 9 x 31 = 279 9 x 32Traceback (most recent call last):
File "D:/WPy64-3720/ZZ/0524-3.py", line 6, in <module>
print(a,'x',b,'=',c,end=' ')
KeyboardInterrupt

 

###金字塔形

a=1

b=1
while b<=9:
  c=a*b
  print(a,'*',b,'=',c,end='\t')
  a+=1
  while a>b:
    print()
    b+=1
    a=1

 

转载于:https://www.cnblogs.com/zpzhou/p/10917845.html

你可能感兴趣的文章
Java Scanner 类
查看>>
zoj 1655 单源最短路 改为比例+最长路
查看>>
impulse
查看>>
Deep Learning 教程翻译
查看>>
贪心算法
查看>>
SDL示例一:实现七段数码管的显示
查看>>
Hive权限之审计
查看>>
Redis的安装与使用
查看>>
谈谈站桩
查看>>
容器、应用服务器和web服务器的区别
查看>>
分析统计<第三篇>
查看>>
javascript--- HTML DOM
查看>>
Exactly-once Spark Streaming from Apache Kafka
查看>>
哎,系统分析师下午没过
查看>>
c++opencv项目移植到Android(Mat—》IplImage*)
查看>>
嵌入式linux------SDL移植(am335x下显示yuv420)
查看>>
当vcenter是linux版本的时候Sysprep存放路径
查看>>
代码管理(五)git 删除分支
查看>>
[学习笔记]Spring依赖注入
查看>>
网络虚拟化(SDN,NFV..)和企业骨干网的演化
查看>>