Python–循环

  • A+
所属分类:Python--循环
#循环
#   死循环 i ==1

i = 1
while i < 10 :
    print('ok')
    i = i + 1
print('ooookkk')


"""
练习题
1.使用while循环输出1234568910
2.求1-100的所有数的和
3.输出1-100内的所有奇数
4.输出1-100内的所有偶数
5.求1-2+3-4+5...99的所有数的和
6.用户登录(三次机会重试)
"""
1.
i = 1
while i "<>"< 11 :
    if i == 7 :
        pass
    else:
        print(i)
    i = i + 1
print('-----end------')

#2.
方法一
n = 1
s = 0
while n < 101 :
    s = s + n
    n = n + 1
print(s)

方法二
i = 0
a = 0
while i < 101 :
    i = i + 1
    if i != 100 :
        a = a + i
    else:
        a = a + i
        print(a)


#3.
n = 0
while n < 100 :
    n = n + 1
    if n % 2 == 1 :
        print(n)
    else:
        pass

#4.
n = 0
while n < 100 :
    n = n + 1
    if n % 2 == 0 :
        print(n)
    else:
        pass

#5.
n = 1
s = 0
while n < 100 :
    temp = n % 2
    if temp == 0 :
        s = s - n
    else:
        s = s + n
    n = n + 1
print(s)

6.
n = 1
while n < 4 :
    p = input('请输入密码:')
    if p == "abc" :
        print("登录成功")
        n = n + 3
    else:
        print("登录失败")
    n = n + 1
pass


avatar

发表评论

您必须登录才能发表评论!