学习python Day 2

猜数字 程序

secret_number = 9
i = 0
while i < 3:
guess_number = int(input(“Guess what is the secret number?”))
i += 1
if guess_number == secret_number:
print(“You win”)
i = 3## 这里可以用 break
elif i < 3:
print(“Try again.”)
else:##这里的else可以是while语句的
print(“You are failed”)

小车跑起来

while True:
i = input("> ").lower()
if i == “start”:
print(“Car started…Ready to go!”)
elif i == “stop”:
print(“Car stopped”)
elif i == “quit”:
print(“End the game”)
break
else:
print(“Wrong code!!!”)

小车跑起来(改进)

j = “”
while True:
i = input("> “).lower()
if i == “start” and j != “start”:
print(“Car started…Ready to go!”)
j = i
elif i == “start” and j == “start”:
print(“Car already started!!!”)
j = i
elif i == “stop” and j == “start”:
print(“Car stopped!”)
j = i
elif i == “stop” and j == “stop”:
print(“Car already stopped!!!”)
j = i
elif i == “help”:
print(”""
start - to start the car
stop - to stop the car
quit - to quit
“”")
elif i == “quit”:
print(“End the game”)
break
else:
print(“Wrong code!!!”)

小车跑起来(改进第二种)

started = False
while True:
command = input("> ").lower()
if command == “start”:
if started:
print("Car is already started! ")
else:
started = True
print(“Car started… “)
elif command == “stop”:
if not started:
print(“Car is already stopped! “)
else:
started = False
print(“Car stopped”)
elif command == “help”:
print(”””
start - to start the car
stop - to stop the car
quit - to quit
“””)
elif command == “quit”:
print(“End the game”)
break
else:
print(“Wrong code!!!”)

for语句

for x in “python”:
print(x)

迭代遍历

for x in [“Cauchy”,“Chen”,“Help”,1,2,3,4]:
print(x)
for x in range(10):#rang(10)从0到9;rang(2,10,3)最后个3是每次遍历走三步
print(x)

prices = [10, 20, 30]
total = 0
for x in prices:
total += x
print(f"Total is {total}")

F星星打印

numbers = [5, 2, 5, 2, 2]
for x in numbers:
print("*" * x)

numbers = [5, 2, 5, 2, 2]#另一种写法
for x in numbers:
output = “”
for y in range(x):
output += “x”
print(output)

name = [“John”, “Smith” ,“Sarah” ,“Line” ,“Mosh”]
print(name)
print(name[1])
print(name[1:4])
print(name[-2])
结果:

[‘John’, ‘Smith’, ‘Sarah’, ‘Line’, ‘Mosh’]
Smith
[‘Smith’, ‘Sarah’, ‘Line’]
Line

max函数

numbers = [1, 2, 3, 10, 5, 12, 15, 1]
y = numbers[0]
for x in numbers:
if x > y:
y = x
print(y)

矩阵

j = [1, 2, 3]
k = [4, 5, 6]
l = [7, 8, 9]
matrix = [j, k, l]
print(matrix[1])
print(matrix[0][0])

对组进行操作

.pop()#去掉末尾那个
insert(0, 10)#在第一个位置,插入10
append(5)#在最后加入5
.index()#寻找某个数在这个组中第一次出现,5 in 这个组——作为判断
.count#()计算某个数在其中出现的次数
.sort()#排序
.reverse()#反着排序
.copy()

重复去除

numbers = [2, 2, 4, 6, 3, 4, 6, 1]
nums = []
for i in numbers:
if i not in nums:
nums.append(i)
print(nums)

第二种:

numbers = [2, 2, 4, 6, 3, 4, 6, 1]
nums = numbers.copy()
for i in numbers:
while i in nums:
nums.remove(i)
else:
nums.append(i)
print(nums)

tuple组件

numbers = (1, 2, 3)
不支持项目分配,只取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值