Python学习--控制语句:if, while, for, break, continue

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Filename ifAndWhile.py
# 猜数字游戏,演示if和while的用法

result = 18
running = True

while running:
	guess = int(raw_input('输入一个数字:'))
	
	if guess == result:
		print '恭喜,你猜对了!'
		running = False
	elif guess < result:
		print '太小了!'
	elif guess > result:
		print '太大了!'
	else:
		print '未知错误!'

print '游戏结束!'

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Filename: for.py
# 计算1到10的和,演示for的用法

result = 0

for i in range(1, 11):
	result = result + i

print result

# 计算1到10中奇数的和

result = 0

for i in range(1, 11, 2):
	result = result +i

print result	

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Filename breakContinue.py
# break和continue的用法

while True:
	s = raw_input('输入一个字符串:')
	if s == 'quit':
		break
	elif len(s) < 4:
		print '输入的字符从太短!'
		continue
	else:
		print '你说:', s

你可能感兴趣的:(游戏,python,input)