python基础循环

时间:2021-07-10 08:45:50
1、使用while循环输入1234568910
 n = 1
while n < 11:
if n == 7:
pass
else:
print(n)
n = n + 1

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

n = 1
s = 0
while n < 101:
s = s + n
n = n + 1 print(s)

3、输出1 - 100内的所有奇数
n = 1
while n < 101:
temp = n % 2
if temp == 0:
pass
else:
print(n)
n = n + 1 print('----end----')

4、输出1 - 100内的所有偶数
n = 1
while n < 101:
temp = n % 2
if temp == 0:
print(n)
else:
pass
n = n + 1 print('----end----')

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

n = 1
s = 0 # s是之前所有数的总和
while n < 100:
temp = n % 2
if temp == 0:
s = s - n
else:
s = s + n
n = n + 1 print(s)
6、设计登录小程序,有三次机会
import datetime
username = "keys"
password = ""
no = 2
while no >= 0:
input_name = input("请输入你的账号:")
input_pswd = input("请输入你的密码:")
if username == input_name and password == input_pswd:
print("恭喜您"+username+"登录成功,今天是{}".format(datetime.datetime.today()))
break
elif input_name == "" or input_pswd == "":
print("用户名或密码不能为空,请重新输入")
print("您还有"+str(no)+"次机会!!!")
else:
if username != input_pswd or password != input_pswd:
print("\n账号或密码错误,请重新输入")
print("温馨提示,您还有"+str(no)+"次机会")
else:
print("您的次数已用完。")
no = no -1