标签:continue while True for count
为了坚持而坚持(这话说了自己不信)字符编码
ASCII 编码
定义变量 name = "Hello World"
print (name)
3.用户输入(input)和格式化输出
例:用户输入(input)
username = input("type your username:")
print(username)
格式化输出
a,例:字符串拼接(‘‘‘ + ‘‘‘)
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info1=‘‘‘
------info1 is ‘‘‘ + username ‘‘‘-----------
username is : ‘‘‘+ username ‘‘‘
age is : ‘‘‘ +age ‘‘‘
job is : ‘‘‘+job ‘‘‘
salary is : ‘‘‘ + salary
print (info1)
b, 字符串拼接(‘‘‘ %s ‘‘‘) ,s= string 字符串
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info2=‘‘‘
------info2 is %s-----------
username is : %s
age is : %s
job is : %s
salary is : %s
‘‘‘%(username,username,age,job,salary)
print (info2)
c, 字符串拼接(‘‘‘ %s ‘‘‘) s= string %d=整数 int=强制为数字?
username = input ("username is :")
age = int(input("age is :"))
job = input("job is:")
salary=input("salary is:")
info3=‘‘‘
------info3 is %s-----------
username is : %s
age is : %d
job is : %s
salary is : %s
‘‘‘%(username,username,age,job,salary)
print (info3)
d, 字符串拼接(使用排序.format)
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info4=‘‘‘
------info4 is {0}-----------
username is : {0}
age is : {1}
job is : {2}
salary is : {3}
‘‘‘.format(username,age,job,salary)
print (info4)
e, 字符串拼接(.format)
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info5=‘‘‘
------info5 is {_username}-----------
username is : {_username}
age is : {_age}
job is : {_job}
salary is : {_salary}
‘‘‘.format(username=_username,
age=_age,
job=_job,
salary=_salary)
print (info5)
以上的输出(print)结果是:
**/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/user input.py"
username is :alex
your job is:IT
your age is:23
salary is :232323
4.密码加密
例:
username = input("username:")
password = getpass.getpass("password:")
print (username,password)
5.if 条件判断语句()
例:
oldboy_age=55
age = input("guess oldboy age is :")
if oldboy_age == age
print("yes,you got it")
else
print("wrong ansser")
加上条件:使用户最多可以猜错三次条件
或是while 加条件语句如(while count < 3 )
定义计数器 count = 0, count += 1 每次循环+1
跳出循环break
例:
age= 55
count = 0
while True:
if count == 3:
break
guess = int(input("guess age :"))
if guess == age:
print("yes,you got it.")
elif guess < age:
print("think older.")
else:
print ("think smaler")
count += 1
执行的结果为:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/whiel True 循环.py"
guess age :23
think older.
guess age :23
think older.
guess age :23
think older.
Process finished with exit code 0
对以上的代码做优化
例:
age = 55
count = 0
while count < 3:
guess = int(input("guess age: "))
if age == guess:
print ("yes ,you got it")
elif age < guess:
print ("think older")
else:
print ("think smaller")
count += 1
** # if count == 3:
else:
print("you have try too many times.")
输出结果是:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/whiel True 循环.py"
guess age: 34
think smaller
guess age: 35
think smaller
guess age: 78
think older
you have try too many times.
Process finished with exit code 0
6.continue #跳出本次循环,执行下一次循环
while True:
for i in range(3):
user = int(input("guess age:"))
if user == age:
print("1")
elif user > age:
print(">")
elif user < age:
print("<")
con = input("do you want con?")
if con == ‘c‘:
continue
else:
break
age_of_oldboy = 55
count = 0
while count < 3:
age = int(input("please input age of oldboy:"))
if age_of_oldboy == age:
print("yes ,you got it!")
break
elif age_of_oldboy < age:
print("think smaller")
else:
print("think older")
count += 1
if count == 3:
continue_confirm = input("do you want to keep guessing..?")
if continue_confirm != ‘n‘:
count = 0
# != 不等于
标签:continue while True for count
原文地址:http://blog.51cto.com/2926352/2092616