上篇案例
#我写的 import random num = random.randint(1, 10) print(num) a = int(input("请输入数字")) if a == num: print("bingo!") elif a > num: b = int(input("大了,还有两次机会,重新输入")) if b > num: c = int(input("大了,还有一次机会,重新输入")) if c > num: print(f"大了,正确答案是{num}") elif c < num: print(f"小了,正确答案是{num}") else: print("bingo!") elif b < num: c = int(input("小了,还有一次机会,重新输入")) if c > num: print(f"大了,正确答案是{num}") elif c < num: print(f"小了,正确答案是{num}") else: print("bingo!") else: print("bingo!") else: b = int(input("小了,还有两次机会,重新输入")) if b > num: c = int(input("大了,还有一次机会,重新输入")) if c > num: print(f"大了,正确答案是{num}") elif c < num: print(f"小了,正确答案是{num}") else: print("bingo!") elif b < num: c = int(input("小了,还有一次机会,重新输入")) if c > num: print(f"大了,正确答案是{num}") elif c < num: print(f"小了,正确答案是{num}") else: print("bingo!") else: print("bingo!")
有点复杂
import random num = random.randint(1, 10) print(num) guess_num = int(input("第一次输入")) if guess_num == num: print("bingo!") else: if guess_num < num: print("小了") else: print("大了") guess_num = int(input("第二次输入")) if guess_num == num: print("bingo!") else: if guess_num < num : print("小了") else : print("大了") guess_num = int(input("第三次输入")) if guess_num == num: print("bingo!") else: if guess_num < num: print(f"小了,正确答案是{num}") else: print(f"大了,正确答案是{num}")
第一阶段
第四章 循环语句
4.1循环语句while
i = 0 while i < 100: print(i) i += 1
import random num = random.randint(1, 100) print(num) count = 1 guess_num = int(input("猜1-100,请输入数字")) while guess_num != num: print("不对,再猜") if guess_num > num: print("大了") else : print("小了") guess_num = int(input("范围1-100,请输入数字")) count += 1 print(f"猜了{count}次就猜对了!")
老师版本
import random num = random.randint(1, 100) print(num) count = 1 flag = True while flag: guess_num = int(input("猜1-100,请输入数字")) count += 1 if guess_num == num: flag = False print("bingo!") else: if guess_num > num: print("大了") else : print("小了") print(f"猜了{count}次就猜对了!")
补充知识点
#print输出不换行 print("hello ",end='')
print("world",end='')
#字符串多行之间自动对齐 print("hello\tworld") print("heima\tbest")
#九九乘法表 i = 1 while i < 10: j = 1 while j <= i: print(f"{j} * {i} = {j * i}\t",end = '') j += 1 i += 1 print()
4.2 for循环语句(range语句)
for循环是一种轮环机制,对待办事项逐个处理(遍历循环)
for 临时变量 in 待处理数据集 : (这里也有冒号)
代码...
name = "itheima is a brand of itcast" count = 0 for x in name : if x == 'a': count += 1 print (count)
从0开始,不包含num
range 不包含num2本身
for i in range(3): print(i) print(i)
编译器报错,但可以运行
符合标准的写法是 提前定义 i= 0
用for循环实现九九乘法表
for x in range(1,10): for y in range(1,x+1): print(f"{y}*{x}={x*y}\t ",end='') print()
循环中断 break continue
continue:终端本次循环 进入下一循环
break:结束循环
案例
import random sum = 10000 for x in range(1,21): jixiao = random.randint(1,10) if jixiao < 5: print(f"员工{x},绩效{jixiao},低于5,不发工资") continue else: if sum == 0: print("公司没钱了,下个月再发") break else: sum -= 1000 print(f"员工{x},绩效{jixiao},发放工资1000元,账户余额{sum}元")
p38-50
标签:guess,python,day3,else,int,num,print,input From: https://www.cnblogs.com/piaoguo326/p/17435890.html