三、Python入门-选择结构
选择结构
标准输入:input() 标准输出:print()
if-else语句
if 条件语句:
执行语句1
elif 条件语句:
执行语句2
else:
执行语句3
#猜数游戏
import random cout = 0 difficulty = input("\033[1;31;40m选择难度(0-5)\033[0m") a = random.randint(0,100) while(True): b = input('猜一个数字(0-100):') #input格式 if(a == int(b)): break else: if(int(b)>a): print("\033[1;34;40m比该数要小,剩下%d次机会\033[0m"%(int(difficulty)+4-cout)) #print格式打印1 else: print("\033[1;32;40m比该数要大,剩下{0}次机会\033[0m".format(int(difficulty)+4-cout)) #print格式打印2 if(cout >= int(difficulty)+4): break cout += 1 if cout==int(difficulty)+4: print('\033[1;33;40myou are failed!\033[0m') else: print('\033[1;33;40myou are win!\033[0m')