python练习题-day1

时间:2022-07-14 20:39:45

1.使用while循环输入 1 2 3 4 5 6     8 9 10

count=0
while count<10:
count+=1
if count==7:
continue
print(count)

2.求1-100所有数的和

sum=0
for i in range(101):
sum+=i
print(sum)

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

li=[]
for i in range(1,101):
if i%2==1:
li.append(i)
print(li)

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

li=[]
for i in range(1,101):
if i%2==0:
li.append(i)
print(li)

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

sum=0
for i in range(100):
if i %2 ==0:
sum-=i
if i%2==1:
sum+=i
print(sum) #方法2
sum=0
j=1
for i in range(1,100):
sum+=i*j
j=-j
print(sum)

6.用户登录,三次机会重试

count=0
while True:
uname="myfu"
password=""
u,p=input("input your name and password:").split()
count+=1
if uname ==u and password ==p:
exit("login success")
else:
if count==3:
exit("three times error input,your count is locked")
else:
print("error username or password ,pls try again,%s time last"%(3-count))