写在前面
曾经学过C,java,基本已经还给学校了。对爬虫与数据感兴趣,想把Python学学,当个记录与督促,emmmmm,如果大神看到就。。。。。轻喷,就是记录一下自己的思路,搞!
1、猜拳游戏
练习输入(input)与if...elif...else,要求输入自己的猜拳,电脑出拳,算胜负
解题想法:
穷举所有可能性,包括:所有赢的条件、平局条件与输掉的条件,
如石头=1,剪刀=2,布=3,人类出拳是youhand ,电脑出拳是Cophand
1)第一版想法,通过人类出拳头输入的情况,看电脑情况,分类判断,实现后过程太冗余
2)简化,对所有条件穷举,同时,对所有赢的与平局条件与超出边界情况处理后,其它输的条件用else处理
# -*- coding = utf-8 -*-
# @Time : 01/08/2021 08:55
# @Author : XSM
# @File : ceidingke.py
# @Software : PyCharm
import random
print("开始猜拳游戏\n1、石头\t2、剪刀\t3、布\n")
youhand = int(input("请输入您的拳法\n"))
Cophand = random.randint(1,3)
print("youhand",youhand,"Cophand",Cophand)
''' 第一种
if youhand == 1:
????????if youhand - Cophand == 0:
????????????????print("平局")
????????elif youhand - Cophand == -1:
????????????????print("赢了")
????????else:
????????????????print("输了")
elif youhand == 2:
????????if youhand - Cophand == 0:
????????????????print("平局")
????????elif youhand - Cophand == 1:
????????????????print("输了")
????????else:
????????????????print("赢了")
elif youhand == 3:
????????if youhand - Cophand == 0:
????????????????print("平局")
????????elif youhand - Cophand == 2:
????????????????print("赢了")
????????else:
????????????????print("输了")
else: print("请输入正确值")
'''
'''第二种'''
if (youhand <= 0 or youhand > 3):
print("您输入的拳法不正确")
else:
if youhand == Cophand:
print("Draw Game!")
elif (youhand == 1 and Cophand == 2) or (youhand == 2 and Cophand == 3) or (youhand == 3 and Cophand == 1):
print("Win!")
else:
print("Lose!")
2、九九乘法表
基础嵌套循环了。用for...in...rage(x)比起C简洁多了
# -*- coding = utf-8 -*-
# @Time : 01/08/2021 23:08
# @Author : XSM
# @File : 9x9.py
# @Software : PyCharm
for i in range(1,10):
for x in range(1,i+1):
print(i,"*",x,"=",i*x,end="\t")
print("\n")
'''while 实现方式'''
tempX = 1
while tempX <= 9:
y = 1
while tempX-y >= 0:
print(tempX, "*", y, "=", tempX*y,end="\t")
y += 1
print("\n")
tempX += 1
|