输出1到100之间的所有奇数和偶数:
num = 1 while num < = 100 : if num % 2 = = 0 : print (num) num + = 1 cai num = 1 while num < = 100 : if num % 2 = = 1 : print (num) num + = 1 |
猜年龄游戏的两种方法:
age = 50 flag = True while flag: user_input_age = int ( input ( "Age is:" )) if user_input_age = = age: print ( "Yes" ) flag = False elif user_input_age > age: print ( "is bigger" ) else : print ( "is smaller" ) print ( "End" ) age = 50 while True : user_input_age = int ( input ( "Age is:" )) if user_input_age = = age: print ( "Yes" ) break elif user_input_age > age: print ( "is bigger" ) else : print ( "is smaller" ) print ( "End" ) |
break语句和continue语句。
print中的end语句:
#print("hello world.",end="__") # \n \r\n \r #print("hello world.",end="__") #print("hello world.",end="__") num1 = 0 while num1< = 5 : print (num1,end = "_" ) num2 = 0 while num2< = 7 : print (num2,end = "-" ) num2 + = 1 num1 + = 1 print () # print(end="\n") #0_0-1-2-3-4-5-6-7- #1_0-1-2-3-4-5-6-7- |
输出#号,指定长高的长方形:
height = int ( input ( "height:" )) width = int ( input ( "width:" )) num_height = 1 while num_height < = height: num_width = 1 while num_width < = width: print ( "#" ,end = "") num_width + = 1 print () num_height + = 1 |