Detailed explanation of the use of while and if in python3

高洛峰
Release: 2017-03-26 18:37:26
Original
1116 people have browsed it

This article explains in detail the use of while and if in python3. Friends in need can refer to it.

# Auther: Aaron Fan

age_of_oldboy = 56

#定义一个while循环的起始判断值count
count = 0
#当count小于3的情况下一直执行while循环
while count < 3:
   guess_age = int(input("猜一下oldboy的年纪: "))
   if guess_age == age_of_oldboy:
       print("好的,你猜对了")
       #满足条件后退出循环break,break是结束整个循环
       break
   #当前面一条if或者elif不成立的情况下, 才会执行后面的elif
   elif guess_age > age_of_oldboy:
       print("猜大了...")
   #所有if不成立的情况下才会执行else
   else:
       print("猜小了!")
   #没执行玩一次while,就给count的值加1
   count += 1
#当while条件里面的东西执行执行完了,就会执行下面的else,如果中间被break了,就不会再执行下面的else了
else:
   print("你用尽了所有的机会...")
Copy after login

The above is the detailed content of Detailed explanation of the use of while and if in python3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template