Python 学习实例汇总

While 语句学习实例:

1、猜拳的游戏

>>> import random
>>> while True:
	s = int(random.randint(1,3))
	if s==1:
		pc = "石头"
	if s==2:
		pc = "剪刀"
	if s==3:
		pc = "布"
	u = input("请输入 石头、剪刀、布,回车结束:")
	#print (u)
	list1 = ["石头","剪刀","布"]
	if (u not in list1):
		print ("输入有误,请重新输入:")
		u = input("请输入 石头、剪刀、布,回车结束:")
	elif u==pc:
		print ("电脑出了:"+ pc +",     平局!")
	elif (u=='石头'and pc=='剪刀') or (u=='剪刀' and pc=='布') or (u=='布' and pc=='石头'):
		print ("电脑出了:"+ pc + ",    你赢了!")
	elif (pc=='石头'and u=='剪刀') or (pc=='剪刀' and u=='布') or (pc=='布' and u=='石头'):
		print ("电脑出了:"+ pc + ",    你输了!")

持续更新中。。。。。。

2、猜数字游戏

>>> import random
>>> sj = int(random.uniform(1,10))
>>> ui = int(input("请输入一个整数,按回车结束:"))
请输入一个整数,按回车结束:9
>>> while True:
	if sj == ui:
		print ("恭喜你,你猜对了!")
		break
	elif sj > ui:
		print ("小了")
		ui = int(input("在猜猜:"))
	elif sj < ui:
		print ("大了")
		ui = int(input("在猜猜:"))

3、九成九乘法表
>>> i = 1
>>> while i :
       j = 1
       while j:
           print (j ,"*", i ," = " , i * j)
           if i == j :
               break

           j += 1
           if j >= 10:
               break

       print ("\n")
       i += 1
       if i >= 10:
           break

猜你喜欢

转载自blog.csdn.net/qintaiwu/article/details/80722116