1 day

1:
name = "你好,世界"
print(name )

了解到python中输出语句使用。


2:

age_of_oldboy = 56
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 bigger!")
else:
print("you have tried too many times..fuck off")
这是一个猜年龄的小游戏,但是只能猜3次。


3:
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 bigger!")
count +=1
else:
print("you have tried too many times..fuck off")
同上面一样也是通过循环实现3次猜谜游戏。但是不同点在于一个用for 一个用while 个人感觉while会好用一些。

4:

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 bigger!")
count +=1
if count == 3:
countine_confirm = input("do you want to keep guessing..?")
if countine_confirm != 'n':
count =0
在以上的基础上加以衍生,优化出了可操控是否继续游戏的功能。
5:

name = input("name:")
#raw_input 2.x input 3.x
#input 2.x =
age = int(input("age:") ) #integer
print(type(age) , type( str(age) ))
job = input("job:")
salary = input("salary:")

info = '''
-------- info of %s -----
Name:%s
Age:%d
Job:%s
Salary:%s
''' % (name,name,age,job,salary)

info2 = '''
-------- info of {_name} -----
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
'''.format(_name=name,
_age=age,
_job=job,
_salary=salary)

info3 = '''
-------- info of {0} -----
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
'''.format(name,age,job,salary)
print(info3)
3中格式,或者说3种方式。


6:
# Author:Alex Li
import getpass

_username = 'alex'
_password = 'abc123'
username = input("username:")
#password = getpass.getpass("password:")
password = input("password:")
if _username == username and _password == password:
print("Welcome user {name} login...".format(name=username))
else:
print("Invalid username or password!")
用户登录程序,大体思路为现将用户信息存入,然后再将输入的用户信息与之对比,通过if语句进行判断,通过弹出相应信息,
弹出错误提升。
优化方案,在前面加上循环实现3次错误退出程序。

7:

# Author:Alex Li
'''
count = 0
while True:
print("count:",count)
count = count +1 #count +=1
if count == 1000:
break
'''
'''
for i in range(0,10):
if i <3:
print("loop ",i)
else :
continue
print("hehe...")
'''

for i in range(10):
print('----------',i)
for j in range(10):
print(j)
if j >5:
break
此程序用于训练for循环嵌套。











猜你喜欢

转载自www.cnblogs.com/qtvs/p/9393330.html
今日推荐