一些基本练习(notepad++写的,需要调格式)

1.用户登录判断

name = input("请输入姓名")
pwd = input("请输入密码")

if name == "alex" and pwd == "123":
print('yes')
else:
print('no')

进阶:只能输入三次错误

i = 0
while i < 3:
name = input("请输入用户名:")
pwd = input("请输入密码")
if name == "admin" and pwd == "pwd":
print("登陆成功")
break
else:
print("请再次输入")
i += 1
print("登录失败")

2.输出1到10

import time
kaishi = 1

while True:
print(kaishi)
if kaishi == 10:
break
kaishi = kaishi + 1
time.sleep(1)
print('end')

进阶:输出除7外的1到10

start = 1
while True:
if start == 7:
start += 1
continue
print(start)
if start == 10:
break
start += 1

3.计算1到100

start = 1
sum = 0
while True:
print(start)
sum = sum + start
if start == 100:
break
start += 1
print(sum)

进阶:输出1-2+3....

sum = 0
start = 1
while start < 100:

temp = start % 2
if temp == 1:
if start == 1:
s = str(start)
else:
s = s + "+" +str(start)
sum = sum + start
else:
s = s + "-" +str(start)
sum = sum - start

start += 1
print(s)
print(sum)

4.奇数偶数

偶数.

i = 1

while True:

if i == 101:
break
s = i % 2
if s == 0:
print(i)
i += 1

 

奇数.

start = 1
while True:
if start == 101:
break

temp = start % 2
if temp == 0:
pass
else:
print(start)
start += 1

 

posted @ 2017-10-16 15:30  彩色的大卷发  阅读(196)  评论(0编辑  收藏  举报