# Method 1 # from 2 Start calculating i = 2# Define a variable to hold the result sum=0while i <= 100: if i % 2 == 0: sum = sum + i else: sum = sum - i i += 1print("2-3+4-5+6...+100=",sum)
# Method 2 n=3sum=2while n<=100 : #n Yes 2 Remainder if n % 2 != 0: sum = sum - n n = n + 1 else: sum = sum + n n = n + 1print("2-3+4-5+6...+100=",sum)
Running results :
2、 Get a number from the keyboard , And then calculate its factorial , For example, the input is 3, So that's the calculation 3! Result , And the output .Tips :
1! be equal to 1
2! be equal to 1*2
3! be equal to 1*2*3
n! be equal to 1*2*3*...*n
n = int(input(" Please enter a non negative number :")) # Negative numbers are not factorials def factorial(n): if n == 0: return 1 # 0 The factorial is 1 else: return n * factorial(n - 1)if __name__ == '__main__': result=factorial(n) print("{} The factorial of is :{}".format(n,result))
3、 Users enter test scores , When the score is higher than 90( contain 90) When printing A; Otherwise, if the score is higher than 80( contain 80) When printing B; Otherwise, if the score is higher than 70( contain ) When printing C; Otherwise, if the score is higher than 60( contain 60) When printing D; Print the rest E.try: score=float(input(' Please enter the test scores :')) if score>=90: print('A') elif 80<=score<90: print('B') elif 70<=score<80: print('C') elif 60<=score<70: print('D') else: print('E')except Exception as e: print(' Your input is wrong !')
4、 Suppose the one-year term interest rate is 3.52%, It will take several years to double the fixed deposit with interest and capital ( for example : How many years will it take 10000 To become 20000)?save_money = float(input(" Please input the money you want to deposit in the bank :"))print(" You saved {} Yuan to the bank ".format(save_money))total_money = save_money * 2 # Define variables to hold the total amount of money year = 1 # Define variables for recording years while save_money < total_money: save_money *= (1 + 0.0352) year += 1print(" The term interest rate is 3.52%, need {} The annual principal and interest can be doubled !".format(year))
5、 Will list a =["I","T","e","s","t","e","r"] Concatenate into strings , Please implement it in many ways .# Method 1 String function call a = ["I","T","e","s","t","e","r"]print("".join(a))# Method 2 for loop a = ["I","T","e","s","t","e","r"]s = ""for item in a: s += itemprint(s)
author : Empty bad uncle Blog
文章目錄學習目標了解Sobel算子,Scharr算子和拉普拉