练习:
1,计算100以内奇数的和
- #计算100以内所有奇数的和
- sum = 0
- # n = 1
- # while n < 100:
- # # sum = sum + n
- # sum += n
- # # n = n + 2
- # n += 2
- # print(sum)
- n = 99 #求偶数时n = 100
- while n > 0:
- sum += n
- # n = n - 2
- n -= 2
- print(sum)
-
2,打印直角三角形
- *
- **
- ***
- ****
- *****
- ******
- layer = int(input("请输入要打印的层数:")) #layer=3
- index = 1
- while index <= layer: #layer=3
- #打印*
- j = 1
- while j <= index: #index = 1 2 3
- print("*",end="")
- j += 1
- print() #换行
- index += 1 # index = 2 3 4
3,打印九九乘法表
- i = 1
- while i <= 9:
- j = 1
- while j <= i:
- print("%s * %s = %s "%(j,i,(i*j)),end="")
- j += 1
- print() #换行
- i += 1
优化:
- i = 1
- while i <= 9:
- j = 1
- while j <= i:
- res = i * j
- if res < 10:
- print("%s * %s = %s "%(j,i,(i*j)),end="")
- else:
- print("%s * %s = %s "%(j,i,(i*j)),end="")
- j += 1
- print() #换行
- i += 1
4,判断一个数是否为质数(质数)
质数又称为素数,一个大于1的自然数,除了1和它本身之外,不能被其他自然数整数的数叫做质 数,最小的质数是2,它是唯一一个偶数质数,例如:2,3,5,7,11,13,17等
- #判断一个数是否为质数(素数)
- num = int(input("请输入一个数:"))
- if num <=1:
- print("这个数不是质数")
- elif num == 2:
- print("这个数是质数")
- else:
- i = 2
- while i < num:
- if num % i == 0:
- print("这个数不是质数")
- break
- i += 1
- else:
- print("这个数是质数")
5,实现猜单词的小游戏
- 小提示:
- import random
- word = ("easy","python","difficult","hello")
- random.choice(word)
- 将单词的字母顺序打乱
- random.randrange()
- len()
import random
dir(random) ----- 查看某个模块(random)的所有的方法
help() ----- 调取帮助文档
- import random
- WORDS = ("import","pyhon","hello","easy","difficult")
- print("欢迎来到猜单词游戏!请将乱序的单词组合成正确的单词")
- iscontinue = "Y"
- while iscontinue == "Y" or iscontinue == "y" or iscontinue == "YES":
- words = random.choice(WORDS)
- right = words
- # print(words)
- #打乱顺序
- newword = ""
- while words:
- position = random.randrange(len(words))
- # print(position)
- # newword = newword + words[position]
- newword += words[position]
- # print(newword)
- words = words[:position]+words[(position+1):]
- print("乱序后的单词是:",newword)
- guess = input("请你猜单词:")
- """
- if guess == right :
- print("恭喜你,猜对了!")
- else:
- print("抱歉,猜错了!")
- """
- while guess != right and guess != "":
- print("抱歉,猜错了!")
- guess = input("请你继续猜:")
- if guess == right :
- print("恭喜你,猜对了!")
- iscontinue = input("你是否继续游戏Y/N:")
6,实现两个数的交换(用户输入两个数,存储到内存中的)
- num1 = int(input("请输入第一个数:"))
- num2 = int(input("请输入第二个数:"))
- #方法一:取中间变量
- # temp = num1
- # num1 = num2
- # num2 = temp
- #方法二:求和法
- # num1 = num1 + num2 # 30 = 10 + 20
- # num2 = num1 - num2 # 30 - 20 = 10
- # num1 = num1 - num2 # 30 - 10 = 20
- # 方法三:异或交换法(python独有 引入了寄存器)
- num1,num2 = num2,num1
- print("交换后的num1 = %s ,num2 = %s"%(num1,num2))
7,一个自然数与3的和是5的倍数,以3的差是6的倍数,这个自然数最小是多少?(while)
- # index = 0
- # while True:
- for i in range(1000):
- if (i + 3) % 5 == 0 and (i - 3 ) % 6 == 0:
- print(i)
- break
- i += 1
8,在400~500之间求一个数,它被2取余1,被5取余是3,被8取余是1,这个数是多少?
- for i in range(400,501):
- if i % 2 == 1 and i % 5 == 3 and i % 8 == 1:
- print(i)
- break
9,打印等腰三角形
- layer = int(input("请输入要打印的层数:"))
- #每一行
- for i in range(1,layer+1):
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- print("*",end="")
- print("")
10,打印实心菱形
- layer = int(input("请输入要打印的层数:"))
- while layer % 2 == 0:
- layer = int(input("请重新输入要打印的层数(注意要输入奇数行):"))
- #上半部分
- for i in range(1,layer // 2 + 2): #上半部分多打印一行 layer // 2 + 1 前闭后开的区间
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- print("*",end="")
- print("")
- #下半部分
- for i in range(layer // 2 ,0,-1):
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- print("*",end="")
- print("")
11,打印空心菱形
- layer = int(input("请输入要打印的层数:"))
- while layer % 2 == 0:
- layer = int(input("请重新输入要打印的层数(注意要输入奇数行):"))
- #上半部分
- for i in range(1,layer // 2 + 2): #上半部分多打印一行 layer // 2 + 1 前闭后开的区间
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- #判断一下是否是第一个和最后一个
- if j == 0 or j == star_num - 1:
- print("*",end="")
- else:
- print(" ",end="")
- print("")
- #下半部分
- for i in range(layer // 2 ,0,-1):
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- #判断一下是否是第一个和最后一个
- if j == 0 or j == star_num - 1:
- print("*",end="")
- else:
- print(" ",end="")
- print("")
12,打印两个组合在一起的三角形
- layer = int(input("请输入要打印的层数:"))
- while layer % 2 == 0:
- layer = int(input("请重新输入要打印的层数(注意要输入奇数行):"))
- #上半部分
- for i in range(1,layer // 2 + 2): #上半部分多打印一行 layer // 2 + 1 前闭后开的区间
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- #判断一下是否是第一个和最后一个
- if j == 0 or j == star_num - 1 or i == layer // 2 + 1:#i == layer // 2 +
- 1判断是不是最后一行
- print("*",end="")
- else:
- print(" ",end="")
- print("")
- #下半部分
- for i in range(layer // 2 ,0,-1):
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- #判断一下是否是第一个和最后一个
- if j == 0 or j == star_num - 1:
- print("*",end="")
- else:
- print(" ",end="")
- print("")
13,打印圣诞树
- layer = int(input("请输入要打印的层数:"))
- while layer % 2 == 0:
- layer = int(input("请重新输入要打印的层数(注意要输入奇数行):"))
- #上半部分
- for i in range(1,layer // 2 + 2): #上半部分多打印一行 layer // 2 + 1 前闭后开的区间
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- #判断一下是否是第一个和最后一个
- if j == 0 or j == star_num - 1 or i == layer // 2 + 1 or j == star_num
- // 2:#i == layer // 2 + 1判断是不是最后一行 j == star_num // 2判断是不是中间那一列
- print("*",end="")
- else:
- print(" ",end="")
- print("")
- #下半部分
- for i in range(layer // 2 ,0,-1):
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- #判断一下是否是第一个和最后一个
- if j == star_num // 2:
- print("*",end="")
- else:
- print(" ",end="")
- print("")
14,打印以下图形
- layer = int(input("请输入要打印的层数:"))
- while layer % 2 == 0:
- layer = int(input("请重新输入要打印的层数(注意要输入奇数行):"))
- #上半部分
- for i in range(1,layer // 2 + 2): #上半部分多打印一行 layer // 2 + 1 前闭后开的区间
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- #判断一下是否是第一个和最后一个
- if j == 0 or j == star_num - 1 or i == layer // 2 + 1 or j == star_num
- // 2:#i == layer // 2 + 1判断是不是最后一行 j == star_num // 2判断是不是中间那一列
- print("*",end="")
- else:
- print(" ",end="")
- print("")
- #下半部分
- for i in range(layer // 2 ,0,-1):
- #计算空格的个数
- sapce_num = layer - i
- for j in range(0,sapce_num):
- print(" ",end="")
- #计算*的个数
- star_num = 2 * i - 1
- for j in range(0,star_num):
- #判断一下是否是第一个和最后一个
- if j == 0 or j == star_num - 1 or j == star_num // 2:
- print("*",end="")
- else:
- print(" ",end="")
- print("")