Python basic practice questions

1. Use the while loop to input 1 2 3 4 5 6 8 9 10

count = 1
while count< 11:
    if count == 7:   
        count+=1
        continue
    print(count)
    count+=1

 

2. Find the sum of all numbers from 1 to 100

counts = 1
num = 0
while counts <101:
    num +=counts
    counts+=1
print(num)

  

3. Output all odd numbers within 1-100

counte = 1
while counte < 101:
    if counte % 2 == 1:
        print(counte)
    counte+=1

  

4. Output all even numbers within 1-100

counte = 1
while counte < 101:
    if counte % 2 != 1:
        print(counte)
    counte+=1

  

5. Find the sum of all numbers 1-2+3-4+5 ... 99

counte = 1
num = 0
num1 = 0
while counte < 100:
    if counte % 2 != 1:
        num += -count
    else:
        num1 += counte
    counte+=1
print(num+num1)

  

6. User login (three chances to retry)

count = 0
while count<3:
    count +=1
    username = input("username:")
    password = input("password:")
    if username == 'alex' and password == '123456':
        print('Login successful!')
        break
    else:
        print('Please try again!')

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325897494&siteId=291194637