#11-1嵌套循环 ,一次打印3个乘法表
multiplier = 5
for i in range(1,11):
print(i,"x",multiplier,"=",i*multiplier)
for multiplier in range (5,8):
for i in range (1,11):
print(i,"x",multiplier,"=",i*multiplier)
print()
#11.2 可变循环
numStars = int(input("How many stars do you want?\n"))
for i in range (1,numStars+1): #for in range(0,numStars)一样
print("*",end="")
#11-3 可变嵌套循环
numLines = int(input("How many lines of stars do you want?\n"))#多少行
numStars = int(input("How many stars per line?\n"))#每行多少个星星
for line in range(0,numLines):
for star in range(0,numStars):
print("*",end = "")
print()
#11-4 更多可变嵌套循环 更复杂的星星模块
numBlocks = int(input("How many blocks of srars do you want"))
for block in range(1,numBlocks + 1):
for line in range(1,block * 2):
for star in range(1,(block + line) * 2):
print("*",end="")
print()
print()
#11-5 在嵌套循环中打印循环变量
numBlocks = int(input("How many blocks of stars do you want?"))
for block in range(1,numBlocks +1):
for line in range(1,block * 2):
for star in range(1,(block + line) *2):
print("*",end = "")
print("line=",line,"star=",star)
print()
#11-6 热狗组合
print("\tDOG \tBun \tKetchup\tMustard\tOnions")#z注意空格,因为有的单词比较长
count = 1
for dog in [0,1]:
for bun in [0,1]:
for ketchup in [0,1]:
for mustard in [0,1]:
for onion in [0,1]:
print("#",count,"\t",end = "")#不换行符号
print (dog,"\t",bun,"\t",ketchup,"\t",mustard,"\t",onion,"\t")
count = count +1
#11-7 代码清单,计算卡路里的热狗程序
#列出热狗各个部分的卡路里
dog_cal = 140
bun_cal = 120
ket_cal = 80
mus_cal = 20
onion_cal = 40
print("\tDog \tBun \tKetchup\tMustard\tOnions\tCalories")
count = 1
for dog in [0,1]:#热狗程序是外循环
for bun in [0,1]:
for ketchup in [0,1]:
for mustard in [0,1]:
for onion in [0,1]:
total_cal = (bun*bun_cal)+(dog*dog_cal)+ \
(ketchup * ket_cal) + (mustard * mus_cal) +\
(onion * onion_cal)
print("#",count,"\t",end="")
print(dog,"\t",bun,"\t",ketchup,"\t",mustard,"\t",onion,"\t",total_cal)
count= count + 1
multiplier = 5
for i in range(1,11):
print(i,"x",multiplier,"=",i*multiplier)
for multiplier in range (5,8):
for i in range (1,11):
print(i,"x",multiplier,"=",i*multiplier)
print()
#11.2 可变循环
numStars = int(input("How many stars do you want?\n"))
for i in range (1,numStars+1): #for in range(0,numStars)一样
print("*",end="")
#11-3 可变嵌套循环
numLines = int(input("How many lines of stars do you want?\n"))#多少行
numStars = int(input("How many stars per line?\n"))#每行多少个星星
for line in range(0,numLines):
for star in range(0,numStars):
print("*",end = "")
print()
#11-4 更多可变嵌套循环 更复杂的星星模块
numBlocks = int(input("How many blocks of srars do you want"))
for block in range(1,numBlocks + 1):
for line in range(1,block * 2):
for star in range(1,(block + line) * 2):
print("*",end="")
print()
print()
#11-5 在嵌套循环中打印循环变量
numBlocks = int(input("How many blocks of stars do you want?"))
for block in range(1,numBlocks +1):
for line in range(1,block * 2):
for star in range(1,(block + line) *2):
print("*",end = "")
print("line=",line,"star=",star)
print()
#11-6 热狗组合
print("\tDOG \tBun \tKetchup\tMustard\tOnions")#z注意空格,因为有的单词比较长
count = 1
for dog in [0,1]:
for bun in [0,1]:
for ketchup in [0,1]:
for mustard in [0,1]:
for onion in [0,1]:
print("#",count,"\t",end = "")#不换行符号
print (dog,"\t",bun,"\t",ketchup,"\t",mustard,"\t",onion,"\t")
count = count +1
#11-7 代码清单,计算卡路里的热狗程序
#列出热狗各个部分的卡路里
dog_cal = 140
bun_cal = 120
ket_cal = 80
mus_cal = 20
onion_cal = 40
print("\tDog \tBun \tKetchup\tMustard\tOnions\tCalories")
count = 1
for dog in [0,1]:#热狗程序是外循环
for bun in [0,1]:
for ketchup in [0,1]:
for mustard in [0,1]:
for onion in [0,1]:
total_cal = (bun*bun_cal)+(dog*dog_cal)+ \
(ketchup * ket_cal) + (mustard * mus_cal) +\
(onion * onion_cal)
print("#",count,"\t",end="")
print(dog,"\t",bun,"\t",ketchup,"\t",mustard,"\t",onion,"\t",total_cal)
count= count + 1
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220205/viewspace-2074842/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/220205/viewspace-2074842/