python First day
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python First day相关的知识,希望对你有一定的参考价值。
1.python 输入和格式化输出。
#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: Aron1)input输入name = input("name:")
age = input("age:")
job = input("job:")
salary = input("salary:")
输入的格式位str
格式转换:
age = int(input("age:"))
输入为int
2)格式化输出的三种方式
①
info = ‘‘‘
------ info of {_name} ------
Name:{_name}
Age:{_age}
job:{_job}
salary:{_salary}
-----------------------------
‘‘‘ .format(_name = name,
_age = age,
_job = job,
_salary = salary
)
②
info = ‘‘‘
------ info of {1} ------
Name:{2}
Age:{3}
job:{4}
salary:{5}
-----------------------------
‘‘‘ .format(name,name,age,job,salary)
③
info = ‘‘‘
------ info of %s ------
Name:%s
Age:%s
job:%s
salary:%s
-----------------------------
‘‘‘ %(name,name,age,job,salary)
2.while循环以及for循环
1)while循环
_username = ‘Aron‘
_password = ‘abc123‘
count = 0
while True :
password = input("password:")
if _username == username and _password == password:
print("Welcome user {name} login...".format(name=username))
break
else:
print("Invalid username or password! Please re-enter")
count += 1
if count == 5 :
break #(跳出循环) #continue(跳过本次循环,进入下一次循环)
简写:
age_of_oldboy = 56
count = 0
while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy :
print("yes,you got it")
break
elif guess_age > age_of_oldboy :
print("think smaller...")
else:
print("think birgger!")
count += 1
if count == 3:
countine_confirm = input("do you want to keep guessing?")
if countine_confirm != ‘n‘:
count =0
else:
print("you have tried too many times..fuck off")
2)for 循环
age_of_oldboy = 56
for n in range(0,10,2):
print("loop",n)
for i in range(3):
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy :
print("yes,you got it")
break
elif guess_age > age_of_oldboy :
print("think smaller...")
else:
print("think birgger!")
else:
print("you have tried too many times..fuck off")
以上是关于python First day的主要内容,如果未能解决你的问题,请参考以下文章