码迷,mamicode.com
首页 > 编程语言 > 详细

python基础学习3_if_for

时间:2016-08-08 07:43:22      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:windows   python   letter   guess   

if用于条件判断,如果满足条件就执行否则就继续直到结束,熟悉shell条件判断就觉得so easy

格式:

if condition1:

  execute command

elif condition2:

  execute command

else:

  execute command


for n in condition:

  execute command


if示例1:     #基于windows版 python3.5

#!/usr/bin/env pyhon
# -*- coding: UTF-8 -*-
# authors: jack

age = 28
guess_num = int(input("INPUT YOUR GUESS NUM:"))
if guess_num == age:
   print("Your guess right.GOOD!!!")
elif guess_num > age:
   print("Your guess letter!")
else:
   print("Your guess smaller!")

if示例2:

#!/usr/bin/env pyhon
# -*- coding: UTF-8 -*-
# authors: jack

user = ‘jack‘

passwd = ‘jack123‘

username = input("username:")
password = input("password:")

if user == username and password == passwd:
   print ("username is correct...,login success!!!")
else:
    print("Invaild username or password error...")

for示例1:
#!/usr/bin/env pyhon
# -*- coding: UTF-8 -*-
# authors: jack

age = 28
for n in range(10):
   if n < 3:
       guess_num = int(input("INPUT YOUR GUESS NUM:"))
       if guess_num == age:
           print("Your guess right.GOOD!!!")
           break              #跳出整个loop
       elif guess_num > age:
           print("Your guess letter!")
       else:
           print("Your guess smaller!")
   else:
       print("too many attempts ...bye bye")
       break



本文出自 “chhhsdbohe” 博客,请务必保留此出处http://437972.blog.51cto.com/427972/1835473

python基础学习3_if_for

标签:windows   python   letter   guess   

原文地址:http://437972.blog.51cto.com/427972/1835473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!