目录
11-进阶的三元运算符
12--基本的判断用户名和密码
13-while循环
14-for循环
15-猜数
16-range用法
17-乘法表
18-乘法表的扩展(金子塔)
19-列表的嵌套使用
20-利用random函数实现随机数(3个例子)
脚本运行在CentOS 7环境下的,请知晓!!!学习一门语言,最好的方法就是多敲!!!
#!/usr/local/bin/python3
#输入三个数,再打印最大值的数出来
x = int(input("输入第一个数:"))
y = int(input("输入第二个数:"))
z = int(input("输入第三个数:"))
#三目运算符的第一种写法
print((x if (x>y) else y) if ((x if (x>y) else y)>z) else z)
'''
三目运算符的第二种写法
a=(x if (x>y) else y)
print(a if (a>z) else z)
'''
#!/usr/local/bin/python3
import getpass #导入getpass模块
#getpass模块中,有许多多方法,getpass.getpass是其中的一种
username = input('username: ')
password = getpass.getpass('password: ')
#通过IDLE中来调getpass函数,会显示输入的密码,必须在Python Shell或Windows下的CMD才不会显示密码
#切记:判断时候要用“==”,因为“=”是赋值用的
if username == 'lisi' and password == '123456':
print('\033[32;1m login successful!!! \033[0m')
else:
print('\033[31;1m login incorrect!!!\033[0m')
#注意:getpass方法在pycharm会出现挂起的情况,出现输密码显示出来 ,且卡住的情况
'''
建议在cpython解释里执行操作
>>> import getpass
>>> p=getpass.getpass('password')
password
>>> print(p)
aaa
>>>
'''
#!/usr/local/bin/python3
counter = 0
num = 0
while counter < 101:
num += counter
counter += 1
print('1加到100之和为:',num) #注意:num前不加“,”会报错
#面试题:如何用一行代码实现1到100之和
print(sum(range(1,101))) #利用了sum函数求和
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# while-break
while True:
yn = input('Continue(y/n): ')
if yn in ['n', 'N']:
break
print('running...')
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# whlie-continue求100内的偶数和
sum100 = 0
counter = 0
while counter < 100:
counter += 1
# if counter % 2:
if counter % 2 == 1:
continue
sum100 += counter
print(sum100)
#break是结束循环,break之后、循环体内代码不再执行。
#continue是跳过本次循环剩余部分,回到循环条件处。
#!/usr/local/bin/python3
astr = "hello"
alist = [10, 20, 30]
atuple = ('zhangsan', 'lisi', 'alice')
adict = {'name': 'tomcat', 'age': 18}
for ch in astr:
print(ch)
for i in alist:
print(i)
for name in atuple:
print(name)
for key in adict:
print('%s: %s' % (key, adict[key]))
#for循环主要是用来遍历数据对象的
#结果显示如下:
h
e
l
l
o
10
20
30
zhangsan
lisi
alice
name: tomcat
age: 18
###例2###
sum100 = 0
#range(1, 101)时,是只取1到100的数
for i in range(1, 101):
sum100 += i
print(sum100) #5050
#!/usr/local/bin/python3
import random
#例1:3次猜数的机会
num3 = random.randint(1,10)
counter = 0
while counter < 3:
answer = int(input('three guess the number: '))
if answer > num3:
print('\033[31;1m 猜大了 \033[0m')
elif answer < num3:
print('\033[31;1m 猜小了 \033[0m')
else:
print('\033[32;1m 猜对了 \033[0m')
break
counter += 1
else: #python的循环语句可以有 else 子句
print('the number is:', num3)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#例2:猜数猜到对为止
num = random.randint(1,100)
running = True
while running:
answer = int(input('guess the number: '))
if answer > num:
print('\033[31;1m 猜大了 \033[0m')
elif answer < num:
print('\033[31;1m 猜小了 \033[0m')
else:
print('\033[32;1m 猜对了 \033[0m')
running = False
#!/usr/local/bin/python3/
#注意range(0, 10)只遍历0到9
for a in range(0, 10):
print(a)
print('++++')
for b in range(6, 11):
print(b)
print('++++') #F求出0到100之和
sum100 = 0
for c in range(1, 101):
sum100 += c
print(sum100)
#!/usr/local/bin/python3
#方法一
for i in range(1, 10):
for j in range(1, i+1):
print(str(i) + 'x' + str(j) + '=' + str(i*j),end = ' ') #end = ' '表示不换行
print() #实现换行
print()
#方法二
for i in range(1, 10):
for j in range(1,i+1):
print('%sx%s=%s' % (i,j,i*j), end = ' ')
print()
print()
#方法三:用户指定x的乘法表,如5的乘法表
x = int(input('x的乘法表(1到9): '))
for y in range(1, x + 1):
for z in range(1, y + 1):
print('%sx%s=%s' % (y,z,y*z), end = ' ')
print()
根据99乘法表的循环思想,实现金子塔的图形,形状如下:
变成思想:金子塔右边的空格为4、3、2、1、0,而右边的图形为1、3、5、7、9!
#一门编程语言最重要的是编程思想,要多想,多敲(个人根据99乘法表的循环思想想出来的)
b = 5
d = -1
e = 2
for a in range(1,6):
b -= 1
d += e
for c in range(1, b+1):
print(' ', end = '')
print('*' * d)
#!/usr/local/bin/python3
import random
win_list = [['石头','剪刀'], ['剪刀','布'],['布', '石头']]
computer = random.choice(['石头','剪刀','布'])
player = input('请出拳(石头/剪刀/布):')
print('Your choice: %s, Computer is choice: %s'% (player, computer))
if player == computer:
print('\033[34;1m 平局 \033[0m')
elif [player, computer] in win_list:
print('\033[32;1m 你赢了 \033[0m')
else:
print('\033[31;1m 你输了 \033[0m')
#注意比较以下列表的表达式。注意区分
print(win_list)
print(win_list[0])
print(win_list[0][0])
#!/usr/local/bin/python3
#coding: utf8
import random
#例子1
print('\033[32;40m+++例1:random.randint代表随机生成一个数字类型的值+++\033[0m')
num = random.randint(1, 10) # 随机生成1-10之间的数字
guess = int(input('guess a number: ')) # 将用户输入的字符转成整数
print("Your choice: %s, Computer's choice: %s" % (guess, num))
#字符串的%运算符就是用来格式化字符串的。在字符串内部,%s表示用字符串替换
if num == guess:
print("\033[0;32;1m\t you are right! \033[0m")
elif num > guess:
print("\033[0;31;1m\t Small! \033[0m")
else:
print("\033[0;31;1m\t Big! \033[0m")
#例子2
print()
print('\033[32;40m+++例2:random.choice代表从序列中随机选取一个元素+++\033[0m')
all_choices = ['石头', '剪刀', '布']
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
prompt = """(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): """
computer = random.choice(all_choices)
ind = int(input(prompt)) #这一步可以不用转换到int类型,通过字典实现
player = all_choices[ind]
print("Your choice: %s, Computer's choice: %s" % (player, computer))
if player == computer:
print('\033[34;1m平局\033[0m')
elif [player, computer] in win_list:
print('\033[32;1mYou WIN!!!\033[0m')
else:
print('\033[31;1mYou LOSE!!!\033[0m')
#例子3
print()
print('\033[32;40m+++例3:random的其他函数+++\033[0m')
print(random.random()) #随机选取0到1的浮点数
print(random.randrange(0, 100, 2)) #随机选取0到100的偶数,random.randrange中的2表示步长