python_day2——格式化输出、运算符

 利用while输出1-10

count=0
while count<=9:
    count=count+1
    print(count)

在上一个代码的基础上增加一个条件:当出现7的时候,不打印7而是打印空格

count=0
while count<=9:
    count=count+1
    if count==7:#此处两个等号表示判断
        print('')
    else:
        print(count)

在上面代码的基础上增加一个条件:当出现7的时候不显示,直接显示8

法一

count=0
while count<=9:
    count=count+1
    if count==7:#此处两个等号表示判断
        count=count+1
        print(count)
    else:
        print(count)

 法二,利用continue的功能

count=0
while count<=9:
    count=count+1
    if count==7:#此处两个等号表示判断
        continue#结束本次循环开始下次循环
    else:
        print(count)
在法二的基础上优化代码,因为continue后,将重新返回,不会继续向下执行,所以可以删除else
count=0
while count<=9:
    count=count+1
    if count==7:#此处两个等号表示判断
        continue#结束本次循环开始下次循环
    print(count)

pass功能:pass表示什么都不执行,直接跳过,与continue的意义完全相反,此时就需要加上else

 
count=0
while count<=9:
    count=count+1
    if count==7:#此处两个等号表示判断
        pass
    else:
        print(count)

输出100以内所有的奇数

法一

count=-1
while count<99:
    count=count+2
    print(count)

法二

count=1
while count<100:
    print(count)
    count=count+2

法三直接求余数

count=1
while count<100:
       if count % 2==1:#注意这里%表示求余数
            print(count)
       count += 1

 求1+2+3+4+……99和

count=1
sum=0
while count<=99:
    sum=sum+count
    count += 1
print(sum)

 求1-2+3-4+……+99和,

这个基本上是逢偶数减,逢奇数加

count=1
sum=1
while count<99:
    count += 1
    if count%2==1:
        sum=sum+count
    else:
        sum=sum-count
print(sum)

 求1+2+3+4+……+87+89+……99和,绕过88

count=0
sum=0
while count<99:
    count=count+1
    sum=sum+count
    if count==88:
        continue
    print(count)
print(sum)

 

输错三次密码吞卡(目前代码还有问题,需要进一步完善)

code=int(input('请输入您的密码:'))
coding=12
count=0
while count<3:
    if count>0:
        code =int(input('请重新输入您的密码:'))
    else:
        pass
    if code==coding:
        count = count + 1
        print('恭喜您答对了')
        break
    if count>=3:
        break
    else:
        pass
    else:
        count = count + 1
        code =int(input('请重新输入您的密码:'))
        if code==coding:
            print('恭喜您答对了')
            break
        else:
            pass
if count>3:
    print('已吞卡')
else:
    print('恭喜您答对了')

输错三次密码吞卡,自己上面想复杂了,下面是正确代码

coding=12
count=0
while count<3:
    code = int(input('请输入您的密码:'))
    if code==12:
        print('登录成功')
    else:
        print('请重新登录')
    count=count+1

 输错三次密码吞卡,进一步优化代码,增加了剩余次数的显示

coding=12
count=0
while count<3:
    count = count + 1
    code=int(input('请输入密码:'))
    if code==coding:
        print('输入成功')
        break
    else:
        sum = 3 - count
        if sum!=0:
            msg = '剩余%d次机会,请重新输入' % (sum)
        else:
            msg = '对不起,密码错误,已吞卡'
        print(msg)

 

格式化输出

主要起占位的作用,一般格式是:%s,s 即为str表示占据文字的位置;%d,d即为digital表示占据数字的位置,范例如下

#格式化输出
Country=input('请输入你的国籍:')
Age=int(input('请输入你的年龄:'))
msg='我是%s人,今年%d岁'%(Country,Age)
print(msg)

 格式化输出小技巧

 注意当在格式化输出时需要输入百分号时候,如果只是输入一个百分号,系统会和占位用的百分号混淆,进而报错

#格式化输出
Country=input('请输入你的国籍:')
Age=int(input('请输入你的年龄:'))
msg='我是%s人,今年%d岁,学习进度3%'%(Country,Age)
print(msg)



请输入你的国籍:12
请输入你的年龄:12
Traceback (most recent call last):
  File "F:/python/python学习/人工智能/第一阶段day2/练习.py", line 4, in <module>
    msg='我是%s人,今年%d岁,学习进度3%'%(Country,Age)
ValueError: incomplete format

改正,只需要再加一个百分号即可,注意第一个百分号是转译,第二个百分号才是百分号的意思

#格式化输出
Country=input('请输入你的国籍:')
Age=int(input('请输入你的年龄:'))
msg='我是%s人,今年%d岁,学习进度3%%'%(Country,Age)
print(msg)

这是另一种格式化输出的方式

print('%(key)s'%{'key':'value'})#答案是value

 运算符

  计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算,今天我们暂只学习算数运算、比较运算、逻辑运算、赋值运算、成员运算

算数运算

以下假设变量:a=10,b=20

比较运算

赋值运算

逻辑运算

注意逻辑运算存在优先级,一般优先级的排布是()>not>and>or,多个运算符同时存在时候,按照上面从左到右的顺序依次计算

print(1 or 2) 输出结果为1

x or y 若x为非零,则返回x.若x为0,则返回y.
布尔值和数字之间的转换,
数字转换为布尔值:bool(),如果()中为非零,则结果为真;
布尔值转化为数字:int(),int(ture)=1,int(false)=0,

但是and是相反的

比如print(1 and 2) 输出结果为2;

 结论

x or y , x为真,值就是x,x为假,值是y;

x and y, x为真,值是y,x为假,值是x。

 

 

posted @ 2018-12-24 22:06  舒畅123  阅读(102)  评论(0编辑  收藏  举报