loop
I. for cycle
1. The real use is for iterative objects:
1) List: a = [1,2,3,4,5,[1,2,3],'aduh']
2) tuple: a = [1,2,3,[1,2,3],'aduh']
3) Dictionary: a = {key':'value','akdfg','list': [1,2,3],'tuple': [1,2,3]}
4) Sets: a = 1,2,4,2}
2. Counting function:
#range starts at 0 and defaults to 1 for i in range(10): print(i)
range generates an invariant sequence of values:
1) range(101) can produce an integer sequence of 0 to 100.
2) range (1,100) can produce an integer sequence of 1 to 99.
3) range(1, 100, 2) can produce an odd sequence of 1 to 99, of which 2 is the step size, i.e. the increment of the numerical sequence.
for i in range(10,0,-2): print(i) #Results: 10, 8, 6, 4, 2
a = [1,2,3] for i in a: print(i) #Result #1 #2 #3
a = [2,1,3] a.sort(reverse = True) print(a) #Results: [1, 2, 3]
3, iteration
for i in range(5): for j in range(5): for k in range(5): print(k)
Exercise: 9x9 multiplication table
for i in range(1,10): for j in range(1,10): if i >= j: k = i * j print("%d x %d = %d"%(j,i,k),end = "\t") print() #Method 2 for i in range(1,10): for j in range(1,i + 1): print("%d x %d = %d"%(j,i,k),end = "\t") print()
2. while loops (generally used for dead loops)
#Traversal with while str_ = 'Joker is a bod man!' i = 0 while i < len(str_): print(str_[i]) i += 1
3. Circulation exercises
#Luck draw: #1. Running kart: (1. Peak, 2. Toilet car, 3. Panda car, 4. Thank you for your patronage) #2.10 yuan a time, until no money, no money can be flushed #3. After recharging to a certain amount, pay attention to game health and rational consumption #4. Peak's winning probability is 0.0001%. import random money = 40 j = 0 for j in range(1000): print("Your balance is:%d"%money) for i in range(money): a = int(input("Whether to draw (1).Yes, 2.No):")) if a == 1: if money >= 10: print("A raffle kart:") wanjia = random.randint(0,10000) print("The number you drew is ____________.%d"%wanjia) if wanjia == 1: print("Congratulations on your winning the pinnacle") elif 1000 <= wanjia <= 2000: print("Congratulations on your winning the toilet car") elif 4000 <= wanjia <= 6000: print("Congratulations on your winning the Panda Car") else: print("I'm sorry you didn't win the prize.") money -= 10 else: print("The balance is insufficient. Please draw after recharging.") money1 = int(input("Please recharge:")) money += money1 print("Your balance is%d"%money1) j += 1 else: break if j > 3: print("**********Pay attention to game health and rational consumption***********")
function
First, role
Simplify the code. When you need to repeat some code and make only minor changes, you can use functions.
Two, structure
def func_name([params]): //Executive body return xxx
def Apple(end): fm = 1 for i in range(1,end + 1): fm *= i return fm num1 = Apple(7) num2 = Apple(3) num3 = Apple(4) print(num1 / (num2 * num3)) #Results: 35.0
Three, practice
#Log in to the mailbox. In the form of a function, the account is a function and the password is a function. def zhang(): hao = input("Login account:") return hao def mi(): ma = input("Login password:") return ma def Start(): z = zhang() m = mi() if m == '54685' and z == '56552': print("Login successfully") else: print("Error in account or password") Start()
#Good friends #1. Add Friends #2. Find out if you have friends #3. If so, return and wait for the other party's consent - > leave a message def add(name): names = ['dfg','dsfd','df'] if name in names: Print_or_mess() else: print("The user does not exist") def Print_or_mess(): print("Added, waiting for the other party's consent") res = input('Do you leave messages?[y/n]') if res == 'y': input("Please enter a message:") def Start(): name = input("Please enter a user name:") add(name) Start()
#Secretly add back deleted friends. money = 0 def you(name): names = ['dfg','dsfd','df'] if name in names: print("This user is already your friend.") else: chong() def chong(): global money #global variable if money >= 100: print("You have succeeded in sneaking in friends") money -= 100 else: print("Your fee is insufficient. Please recharge it. VIP") res = input('Whether to recharge[y/n]') if res == 'y': print("It's jumping....") res1 = float(input('Recharge amount:')) money += res1 chong() else: print("Add failure") def Star(): name = input("Please enter the friends you want to add:") you (name) Star()