python basic exercises, adding in succession

Determine whether the number entered by the user is a leap year

Leap year definition: the year divisible by 4

#Input is a function of custom input content
year = input("Please enter the year number:")

#The xxx.isdigit method detects whether a string consists of only numbers
if year.isdigit():
    year = int(year)
    if year % 4 == 0:
        #{}. Format is a way to format output
        print("{} It is a leap year.".format(year))
    else:
        print("{} Not a leap year.".format(year))
else:
    print("Please enter the year number")

 

Three chances to guess the number A generated by the program, after each time, whether the prompt is correct or greater than or less than A, after three times, the game is over

import random
A = random.randint(1,100)
print(A)
for i in range(3):
    B = input("Please input 1-100 Number between,Includes 1 and 100:")
    if B.isdigit():
        B = int(B)
        if 1<= B <= 100:
            if B == A:
                print("Congratulations, that's right")
                print("Game over")
                break
            elif B > A:
                print("The first {} second, Guess the number is a little bigger".format(i+1))
                if i+1 == 3:
                    print("Game over")
            else:
                print("The first {} second, Guess the number is a little small".format(i+1))
                if i+1 == 3:
                    print("Game over")
        else:
            print("Please restart the game,And enter the number that meets the requirements")
    else:
        print("Please restart the game,And enter the number that meets the requirements")
        break


import random
times = 3
i = 1
A = random.randint(1,100)
while times:
    B = input("Please input 1-100 Number between,Includes 1 and 100:")
    if B.isdigit():
        B = int(B)
        if 1<= B <= 100:
            if B == A:
                print("Congratulations, that's right")
                print("Game over")
                break
            elif B > A:
                print("The first {} second, Guess the number is a little bigger".format(i))
                if i == 3:
                    print("Game over")
                times -= 1
                i += 1
            else:
                print("The first {} second, Guess the number is a little small".format(i))
                if i == 3:
                    print("Game over")
                times -= 1
                i += 1
        else:
            print("Please restart the game,And enter the number that meets the requirements")
    else:
        print("Please restart the game,And enter the number that meets the requirements")
        break

  

Print 1-100 all odd numbers

for i in range(1,101):
    if i % 2 == 1:
        print(i)

  

For a long step, if you go up 2 steps, you will have 1 step left at last; if you go up 3 steps, you will have 2 steps left at last; if you go up 5 steps, you will have 4 steps left at last; if you go up 6 steps, you will have 5 steps left at last; if you go up 7 steps, you will have no one step left at last

a = 0
while a < 1000:
    if a % 2 ==1:
        if a % 3 == 2:
            if a % 5 == 4:
                if a % 6 == 5:
                    if a % 7 == 0:
                        print(a)
    a += 1

  

For the program to verify the user's password, the user has only three opportunities to enter an error, but if the input contains "*", it will not be counted

nums = 3
password = "hhhhh1"
while nums:
    intpu_password = input("Please enter your password:")
    if "*" in intpu_password:
        print("Password cannot contain*Number,Please re-enter")
    elif intpu_password == password:
        print("Password correct")
        break
    else:
        print("Password error,Left {} Second chance".format(nums -1 ))
        nums -= 1

  

Find all Narcissus between 100-999

If a 3-digit number is equal to the sum of its cubes, it is called narcissus number, for example: 153 = 1 ^ 3 + 5 ^ 3 + 3 ^ 3, because 153 is a narcissus number

for i in range(100,1000):
    ls = list(str(i))
    a = int(ls[0])
    b = int(ls[1])
    c = int(ls[2])
    if i == a ** 3 + b ** 3 + c ** 3:
        print(i)

  

There are three colors of red, yellow and green balls, including three red balls, three yellow balls and six green balls. First mix the 12 balls in the box, take eight balls out of the box, and calculate the color matching of these balls

for red in range(0,4):
    for yellow in range(0,4):
        for green in range(2,7):
            if red + yellow + green == 8:
                print("Red ball has {} individual".format(red))
                print("Yellow ball has {} individual".format(yellow))
                print("Green ball has {} individual".format(green))
                print("------------------------")

  

Tags: Python less

Posted on Wed, 04 Dec 2019 12:40:51 -0800 by JakeTheSnake3.0