猜数字游戏:#!/usr/bin/env python
# -*- coding:utf-8 -*-
age_of_xcn = 20
for i in range(3):
guess_age = int(input("guess age:"))
if guess_age == age_of_xcn:
print("yes,you got it")
break
elif guess_age > age_of_xcn:
print("think smaller")
else:
print("think bigger")
else:
print("bay bay")
执行结果:
guess age:1
think bigger
guess age:2
think bigger
guess age:4
think bigger
bay bay
进程已结束,退出代码
#只打印偶数(10以内)#!/usr/bin/env python
# -*- coding:utf-8 -*-
for i in range(0, 10, 2):
print("loop", i)
执行结果:
loop 0
loop 2
loop 4
loop 6
loop 8
猜数字游戏:
当用户输入3后则会提示“do you want to keep ?”输入知道不是n,则count=0,然后重新从0开始循环#!/usr/bin/env python
# -*- coding:utf-8 -*-
age_of_xcn = 20
count = 0
while count
guess_age = int(input("guess age:"))
if guess_age == age_of_xcn:
print("yes,you got it")
break
elif guess_age > age_of_xcn:
print("think smaller")
else:
print("think bigger")
count += 1
if count == 3:
confirm = input("do you want to keep ?")
if confirm != 'n':
count = 0
执行结果:
guess age:1
think bigger
guess age:2
think bigger
guess age:3
think bigger
do you want to keep ?ere
guess age: