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

入门一:python变量、获取值、标准输出、循环判断语句

时间:2016-12-11 12:27:11      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:man   输出   for   格式   输入   没有   它的   sof   log   

  变量是由字母、数字和下划线组成的,数字不能作为开头。

  python中没有常量,一般用常量的时候将变量名大写就可以了,注意后面不要修改它的值。

  变量命令:使用下划线分隔。

  字符编码在代码开头加入一行

1 # -*- coding:utf-8 -*-

获取输入的值input:

  普通输入: 

1 name = input("name:")
2 age = int(input("age:"))
3 job = input("job:")
4 salary = input("salary:")

  密文输如:

1 import getpass
2 
3 password = getpass.getpass("password:")

标准输出的几种方式:

  第一种:

 1 info2 = ‘‘‘
 2 -------------info of {_name}---------
 3 Name:{_name}
 4 Age:{_age}
 5 Job:{_job}
 6 Salary:{_salary}
 7 ‘‘‘.format(_name=name,
 8            _age=age,
 9            _job=job,
10            _salary=salary)

  第二种:

1 info = ‘‘‘
2 --------------------info 0f %s---------------
3 Name:%s
4 Age:%d
5 Job:%s
6 Salary:%s
7 ‘‘‘%(name,name,age,job,salary)

if-else循环

  猜年龄:

1 age_of_oldboy = 56
2 
3 guess_age = int(input("guess age:"))
4 if guess_age>age_of_oldboy:
5     print("your guess is older.")
6 elif guess_age==age_of_oldboy:
7     print("you are right!")
8 else:
9     print("your guess is younger.")

 

 while循环

  在猜年龄的基础上控制只让猜三次:

 1 while count<3:
 2     guess_age = int(input("guess age:"))
 3     if guess_age>age_of_oldboy:
 4         print("your guess is older.")
 5     elif guess_age==age_of_oldboy:
 6         print("you are right!")
 7         break
 8     else:
 9         print("your guess is younger.")
10 
11     count = count + 1
12     print("you have {_count} times.".format(_count=3-count))
13 #if count == 3:
14 else:
15     print("sorry,you try too many times")

 

for循环:

##基础格式
1
for i in range(1,10,2): 2 print("loop ",i)

 

  用for循环写猜密码:

 1 age_of_oldboy = 56
 2 
 3 for i in range(3):
 4     guess_age = int(input("guess age:"))
 5     if guess_age>age_of_oldboy:
 6         print("your guess is older.")
 7     elif guess_age==age_of_oldboy:
 8         print("you are right!")
 9         break
10     else:
11         print("your guess is younger.")
12 
13     print("you have {_count} times.".format(_count=2-i))
14 #if count == 3:
15 else:
16     print("fuck")

入门一:python变量、获取值、标准输出、循环判断语句

标签:man   输出   for   格式   输入   没有   它的   sof   log   

原文地址:http://www.cnblogs.com/dabingya/p/6159001.html

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