学习目标:python复习1~9课
1:数据类型
2:常用操作符
3:分支和循环
学习内容:
import random#调用随机数模块
secret = random.randint(1,10)#randint产生的随机数是左右皆为闭区间,而randrange是左闭右开
times = 3
print('---------萝卜工作室----------')
print("猜猜萝卜现在心里想的数字: ",end='')
guess = 0
while (guess != secret)and(times>0):
temp = input()
while not temp.isdigit():#判断所有字符是否都为数字,为真返回TRUE 否则返回FALSE
temp = input("输入有误,请重新输入吧: ")
guess = int(temp)
times-=1
if guess == secret:
print("卧槽,你是萝卜心里的蛔虫吗?")
print("哼,猜中了也没有奖励")
else:
if guess > secret:
print("大了大了,这都猜不中")
else:
print("嘿,小了小了,你个傻子")
if times > 0:
print('再试一次吧:',end='')
else:
print('机会用光了哦')
print("游戏结束,不玩了")
s为一个字符串:
s.isalnum() 所有字符都是数字或者字母,为真返回True,否则返回False
s.isalpha()所有字符都是字母,为真返回True,否则返回False
s.isdigit()所有字符都是数字,为真返回True,否则返回False
s.islower()所有字符都是小写,为真返回True,否则返回False
s.isupper()所有字符都是大写,为真返回True,否则返回False
s.istitle()所有字符都是首字母大写,为真返回True,否则返回False
s.isspace()所有字符都是空白字符,为真返回True,否则返回False
isinstance(s,int)判断类型,为真返回True,否则返回False,其中s为实例对象,int也可替作float、str等或者元组(str,int,list)是其中一个则返回True
tyoe('520')
<class 'str'>
type(5.20>
<class 'float'>
type('True>
<class 'bool'>
not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9 ==
(not 1 )or (0 and 1 )or (3 and 4 )or (5 and 6) or (7 and 8 and 9) ==0 or 0 or 4 or 6 or 9 == 4
3 and 4 == 4
3 or 4 == 3
>>>3//2
1
>>>3.0//2
1.0
运算符的优先级:幂运算>正负号>算术运算符>比较运算符>逻辑运算符
>>> for i in range(5):
print(i)
0
1
2
3
4
>>> for i in range(2,9):
print(i,end='')
2345678
水仙花数:一个三位数等于其各位数字的立方和
for i in range(0,1000):
sum = 0
temp = i
while temp!=0:#或者while temp:
sum = sum +(temp%10)**3
temp//=10
if sum == i:
print(i)
0
1
153
370
371
407
三色球:有红、黄、绿三种颜色的求,其中红球 3 个,黄球 3 个,绿球 6 个。先将这 12 个球混合放在一个盒子中,从中任意摸出 8 个球,编程计算摸出球的各种颜色搭配。
print('red\tyellow\tgreen')
for red in range(0,4):
for yellow in range(0,4):
for green in range(2,7):
if red+yellow+green == 8:
print(red,'\t',yellow,'\t',green,'\t')#这里'\t'是tab健不是字符串,不用+号,如print(temp+'输入一段话')
red yellow green
0 2 6
0 3 5
1 1 6
1 2 5
1 3 4
2 0 6
2 1 5
2 2 4
2 3 3
3 0 5
3 1 4
3 2 3
3 3 2
break:跳出循环体
continue:终止本轮循环,如果下一轮循环为真,则开始下一轮循环