判断随机数的函数:
1,流程图:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
try: import random question=input("would you want to play a number game?:") if question=="yes": value=random.randint(0,100) ret1=0 ret2=100 while True: count=1 question_two=(int(input("Try to guess the number: "))) if question_two>value: print("请输入%s到%s之间的数"%(ret1,question_two)) ret2=question_two continue elif question_two<value: print("请输入%s到%s之间的数"%(question_two,ret2)) ret1=question_two continue else: print("That\'s it") question_three=input("would you like to play again?:") if question_three=='yes': ret1=0 ret2=100 value=random.randint(0,100) continue else: break else: question=="no" except Exception as f: print('错误是%s'%f)
2.产生一个菱形:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def func(f): count=1 def inner(*args): while True: nonlocal count if count<num: print((2*count-1)*'*') count+=1 else: break f(*args) return inner @func def func1(num2): while True: print('*'*(2*num2-1)) num2-=1 if num2==0: return num1=int(input('请输入一个奇数')) num=num1//2+1 func1(num)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
num=int(input('请输入一个数字')) num1=num//2+1 count=1 while True: if count<num1: print((2*count-1)*'*') count+=1 elif count==num1: flag=num1 count=num print((2*flag-1)*'*') else: flag-=1 print((2*flag-1)*'*') if flag==0: break