码迷,mamicode.com
首页 > 编程语言 > 详细

Python练习题(基础知识练习题(一))

时间:2017-10-27 13:17:41      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:password   open   logs   gif   error   重试   输出   display   for   

1、使用while循环输出 1 2 3 4 5 6     8 9 10

技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

n = 1
while n <= 10:
   if n == 7:
      pass #换成continue不行,因为会跳过本次循环,n不能+1,n永远==7,永远跳过本次循环。
   else:
      print(n)
   n += 1
   # print(‘wss‘,n)
View Code
技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

n = 0
while n <= 9:
    n += 1
    if n == 7:
        pass # 换成continue可以,因为虽然跳出了本次循环,但是下次循环的时候n可以+1
    else:
        print(n)
View Code

2、求1-100的所有数的和

技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

n = 0;s = 0 #s是之前所有数的总和
while n <= 99:
    n += 1
    s += n
    # print(s) #输出每次循环的总和
print(s) #输出最后的总和
View Code
技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

s = 0
for n in range(1,101):
    # print(n)
    s += n
print(s)
View Code

3、输出 1-100 内的所有奇数

技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

n = 0
while n <= 99:
    n += 1
    if n % 2 == 1:
        print(n)
View Code
技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

for n in range(1,101,2):
        print(n)
View Code

4、输出 1-100 内的所有偶数

技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

n = 0
while n <= 99:
    n += 1
    if n % 2 == 0:
        print(n)
View Code
技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

for n in range(2,101,2):
        print(n)
View Code

5、求1-2+3-4+5 ... 99的所有数的和

技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

n = 0;s = 0
while n <= 98:
    n += 1
    if n % 2 == 0:
        s -= n
    else:
        s += n
    # print(s) #输出每次循环运算的结果
print(s) #输出最后的运算结果
View Code
技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

s = 0
for n in range(1,100):
        # print(n)
        if n % 2 == 0:
            s -= n
        else:
            s += n
print(s)
View Code

6、用户登陆(三次机会重试)

技术分享
#!/usr/bin/env python
# -*- encoding: utf8 -*-

import getpass

count = 0
while count < 3:
    user = input(请输入用户名:)
    pwd = getpass.getpass(请输入密码:)
    if user == wss and pwd == 123:
        print(欢迎登陆)
        print(..........)
        break
    else:
        print(用户名或者密码错误)
    count += 1
View Code
技术分享
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

import getpass
username = wss
password = 123
flag = 0
def login():
    user = input(username:)
    passwd = getpass.getpass(password:)
    return user,password
while flag<3:
    user,passwd = login()
    if username == user and password == passwd:
        print(hello,jason)
        break
    else:
        flag += 1
        print (error,input again)
View Code

Python练习题(基础知识练习题(一))

标签:password   open   logs   gif   error   重试   输出   display   for   

原文地址:http://www.cnblogs.com/wushuaishuai/p/7742503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!