当前位置:网站首页>Day4 branch and loop jobs

Day4 branch and loop jobs

2022-06-26 04:59:00 The story of Ula

Basic questions

  1. Print according to the range of grades entered pass perhaps fail, .

    achievement = int(input(' Please enter the grade :'))
    
    
  2. Print according to the entered age range adult perhaps A minor , If the age is not within the normal range (0~150) Print This is not a person !.

     age = int(input(' Please enter age :'))
     if age > 150 or age < 0:
         print(' This is not a person ')
     elif age >= 18:
         print(' adult ')
     else:
         print(' A minor ')
    
    
  3. Enter two integers a and b, if a-b The result is an odd number , The result is output , Otherwise, the prompt message will be output a-b The result is not an odd number

    a = int(input(' Please enter an integer :'))
    b = int(input(' Please enter an integer :'))
    if (a - b) % 2 == 0:
        print('a - b The result is not an odd number ')
    else:
        print(a - b)
    
  4. Use for Cyclic output 0~100 All in 3 Multiple .

    for i in range(0, 101, 3):
       print(i)
    
  5. Use for Cyclic output 100~200 The inner single digit or ten digit can be 3 Divisible number .

    for i in range(100, 201):
       if i // 10 % 10 % 3 == 0 or i % 10 % 3 == 0:
           print(i)
    
    
  6. Use for Cycle statistics 100~200 The median ten is 5 The number of

    for i in range(100, 200):
        if i // 10 % 10 == 5:
            print(i)
    
  7. Use for Loop printing 50~150 All can be 3 Divisible but not by 5 Divisible number

    for i in range(51, 151, 3):
        if i % 5 != 0:
            print(i)
    
  8. Use for Cycle calculation 50~150 All can be 3 Divisible but not by 5 The sum of divisible numbers

    count = 0
    for i in range(51, 151, 3):
        if i % 5 != 0:
            count += i
    print(count)
    
  9. Statistics 100 The inner single digits are 2 And can be 3 The number of integers .

    for i in range(12, 101, 3):
        if i % 10 == 2:
            print(i)
    

Advanced questions

  1. Enter any positive integer , Ask him how many digits ?

    Be careful : You can't use strings here , Only loop

    num = int(input(' Please enter a positive integer :'))
    
    for i in range(0, num):
        if num // 10 ** i == 0:
            break
            
     print(i)
    
  2. Print out all the daffodils , The so-called narcissus number refers to a three digit number , Its figures ⽴ The sum of the squares is equal to the number itself .例 Such as :153 yes

    ⼀ individual ⽔ Fairy flower number , because 1³ + 5³ + 3³ be equal to 153.

    for i in range(100, 1000):
        bw = i // 100
        sw = i // 10 % 10
        gw = i % 10
        if bw ** 3 + sw ** 3 + gw ** 3 == i:
            print(i)
    
  3. Judge whether the specified number is a prime number ( Prime numbers are prime numbers , In addition to 1 A number other than itself that cannot be divided by other numbers )

    num = int(input(' Please enter one digit :'))
    
    for i in range(2, num):
        if num % i == 0:
            print(f'{
            num} Not primes ')
            break
        else:
            print(f'{
            num} Prime number ')
    
  4. Output 9*9 formula . Program analysis : Branch and column considerations , common 9 That's ok 9 Column ,i The control line ,j Control the column .

    for i in range(1, 10):
        for j in range(1, i + 1):
            print(f'{
            i}* {
            j}= {
            i * j} ', end=' ')
        print()
    
  5. This is the classic " A hundred horses and a hundred burdens " problem , There are a hundred horses , Carry a hundred loads , Big horse, big horse 3 Dan , On the back of a horse 2 Dan , Two ponies carry 1 Dan , How big is it , in , How many ponies each ?( You can directly use the exhaustive method )

    for i in range(34):
        for j in range(51):
            if i * 3 + j * 2 + (100 - i - j) / 2 == 100:
                print(f' Malaysia {
            i} A medium horse {
            j} A pony {
            100- i - j } horse ')
    
原网站

版权声明
本文为[The story of Ula]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180509132200.html