判断输入的名字是正确的:
name = raw_input(‘>>>‘) if name == "alex": print (‘yes‘) else: print (‘no‘)
判断用户名和密码是否正确:
name = raw_input("username") pwd = raw_input("password") if name == "alex" and pwd == "123": print(‘yes‘) else:
print(‘no‘)
if条件语句的格式为:
if 条件:
代码块
else:
代码块
if 条件:
代码块
elif 条件:
代码块
else:
代码块
while语句
kaishi = 1 flag = True while flag: print(kaishi) if kaishi == 10: flag = False kaishi = kaishi + 1 time. sleep(1) print(‘end‘)
Break 关键字 用于跳出这个循环 break下面的代码将不再执行
continue 跳出本次循环 继续下一次循环
如何输出123456 89 10
start = 1 while True: if start == 7 start += 1 continue print(start) if start == 10 break start += 1
如何计算1+2+3+4...+100 = 5050
sum = 0 start = 1 while True: sum = sum + start if start == 100: break start += 1 print(‘sum‘)
如何判断一个数是奇数还是偶数?
XX%2=0就是偶数
XX%2=1就是奇数
s = "alex SB" ret = "SB" in s print(‘ret)
布尔判断SB是否在s中
时间: 2024-11-13 21:50:21