Some simple exercises and Python exercises for new python beginners
Preface
This article mainly shares some simple Python exercises, which is a good exercise for beginners who are learning python. I will not talk about them much below. Let's take a look at the detailed introduction.
Question 1: Use a while loop to input 1 2 3 4 5 6 8 9 10
a = 0while a < 10: a +=1 if a == 7: continue print(a)
Question 2: Calculate the sum of all numbers from 1
Method 1:
a = 0b = 1while b <= 100: a = a + b b += 1print(a)
Method 2:
a = 0for i in range(101): a = a + i print(a)
Question 3: output all odd numbers in 1-100
a = 0while a <= 100: a += 1 if(a%2) == 0: print(a)
Question 4: output all even numbers in 1-100
Method 1:
a = 0while a <= 100: a += 1 if(a%2) == 0: print(a-2+1)
Method 2:
a = 0while a <= 99: a += 1 if(a%2) != 0: print(a)
Question 5: Calculate the sum of all numbers of 1-2 + 3-4 + 5... 99
a = 0b = 0while a < 99: a += 1 if a%2 == 1: b += a else: b -= aprint(b)
Question 6: User Login (three chances to retry)
A = 0 while a <3: name = input ("Enter the User name:") password = input ("enter the password :") a + = 1 if name = 'wxd 'and password = '000000': print ("Login successful") exit () else: print ("input error, enter ") print (" three input errors, exit ") again ")
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.