流程控制

来源:互联网 发布:淘宝产品打折功能收费 编辑:程序博客网 时间:2024/06/09 14:05


1.if语句
number=59
guess=int(input("Enter an integer:"))
print("guess is",guess)#可以直接打印连接
print("guess is"+str(guess))#需要将guess转换为字符串之后才能连接
if guess==number:
    print("you got it beautifull")
elif guess<number:
    print("no,the number is higher than it")
   
else :
    print("no ,the number is lower than it")
print("Done")

2. for in 循环

for i in range(1,10):
    print(i)
   
a_list=[1,3,5,7,9]
for i in a_list:
    print(i)


a_dict={"tom":111,"jerry":222}
for i in a_dict:
    print(i)  #默认遍历的是字典的键
    print(a_dict[i])
for ke,el in a_dict.items(): #字典的itmes()方法可以实现键值遍历
    print(ke,el)

3.while循环
number=59
guess_flag=False
while guess_flag==False:
  guess=int(input("Enter an integer:"))
  print("guess is",guess)#可以直接打印连接
  print("guess is"+str(guess))#需要将guess转换为字符串之后才能连接
  if guess==number:
    print("you got it beautifull")
    guess_flag=True
  elif guess<number:
    print("no,the number is higher than it")
   
  else :
    print("no ,the number is lower than it")







原创粉丝点击