Python simple question 3

1. Number of Narcissus

The number of Narcissus is a three digit number. The sum of the three powers of the number in each digit is equal to itself

(for example: 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153) the code is as follows:

Method 1:
for i in range(100,1000): # Conduct for loop
    num = i
    a = num % 10 # Take out the number of digits
    num = num // 10 # Divide down
    b = num % 10 # Take out tens
    num = num // 10
    c = num % 10
    num = num // 10
    if a ** 3 + b ** 3 + c ** 3 == i: # Judge according to conditions
        print(i)
//Method two:
def flower_number1(num):
    length = len(str(num)) # How many bits are there to find out the parameter
    count = length
    sum = 0
    while count: # Circulate
        sum += (num // 10 ** (count - 1) % 10) ** length  
    # The priority of operator power is higher than multiplication and division, and the lower formula is the same as the upper formula
        # sum += (num // (10 ** (count - 1))% 10) ** length
        count -= 1
    if sum == num:
        return True
    else:
        return False
//Method three:
def flower_number2(num):
    str1 = str(num) # Assign to generate a new string
    count = len(str1) # Calculate the length of the string
    length = count
    sum = 0
    while count:
        sum += int(str1[count-1]) ** length
  # Take the first digit of the string, which is equivalent to num The ones, tens, and multiplication of
        count -= 1
    if sum == num:
        return True
    else:
        return False

2. Perfect number

Find all the perfect numbers between 1 and 9999
The perfect number is the sum of all factors except itself, which is exactly equal to the number itself
For example: 6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14
import math # Import mathematics module
for i in range(1,10000): # loop
    sum = 0
    for j in range(1,int(math.sqrt(i)) + 1): # math.sqrt()Is the square root.
        # print(j)
        if i % j == 0: # Judge whether it is a factor
            sum = sum + j
            if  j != 1 and i / j != j: # It cannot be added when the factor is itself. Only one factor can be added when two factors are the same
                sum += i / j
    if sum == i:
        print(i)

3. Chicken and money

One Rooster 5 yuan, one hen 3 yuan, three chickens 1 yuan, buy 100 chickens with 100 yuan
How many cocks and hens are there
for cock in range(21): # There are twenty roosters at most
    for hen in range(34): # Thirty three hens at most
        chick = 100 - cock - hen # Figure out the number of chickens
        if cock * 5 + hen * 3 + chick / 3 == 100: # How much does it cost to buy chicken
            print("Cock:%d,Hen:%d,Chick:%d"%(cock,hen,chick))

Fibonacci series

Output the first 100 numbers of Fibonacci series
0,1,1,2,3,f(n) = f(n-1) + f(n-2)
a = 1
b = 0
print(b) #Output first number
for _ in range(99): # Ninety nine cycles
    b,a = a,a+b  # Exchange the order of two numbers
    print(a)

5. Number of palindromes

Determine whether the input positive integer is the palindrome number

Palindrome number is the same number that arranges a positive integer from left to right and right to left

num = input("Please enter a number:") # Enter a number
str1 = num # num In string form
length = len(str1) # Find the length of the string
count = length // 2 # Downward Division
flag = True  # Set a flag bit
for i in range(count):
    if str1[i] != str1[length-i-1]: # Judge whether it is the same
        flag = False
        break
if flag:
    print("%d Palindrome number"%num)
else:
    print("%d Not palindrome" % num)

6. Craps gambling game

If the player shakes two dice for the first time, the player wins at 7 or 11
If the dealer wins at 2:3:12, the game will continue
The player wants the dice again. If he shakes out 7 points, the dealer wins
If the player wins the first swing
Otherwise, the game will continue. The player will continue to shake the dice
When players enter the game, they have a 1000 yuan bet and lose all the money. The game is over
from random import randint # Import random number module
money = 1000 # You have 1000 yuan in all
while money > 0: # If you have money, you can keep playing
    while True:
        stake = int(input("Please note:"))
        if stake > 0 and stake <= money: # If the input is not correct, input again until it is correct
            break
    flag = False # Set flag to judge
    dice1 = randint(1,6) + randint(1,6) # Roll the dice
    print("The number of points the player first shakes out is:%d"%dice1)
    if dice1 == 2 or dice1 == 3 or dice1 == 12 :
        print("Zhuang Jia Sheng")
        money -= stake # Chuang Jiasheng, players lose money
        print("The balance is:%d"%money)
    elif dice1 == 7 or dice1 == 11:
        print("Player wins")
        money += stake # Player wins, dealer loses money
        print("The balance is:%d" % money)
    else:
        flag = True # Game goes on, draw
    while flag:
        print("Player shakes color again")
        dice2 = randint(1,6) + randint(1,6) # Shake the dice again
        print("The number of points the player shakes out for the second time is:%d"%dice2)
        if dice2 == dice1:
            print("Player wins")
            money += stake
            flag = False
            print("The balance is:%d" % money)
        else:
            print("Game continues")
            flag = False
            print("The balance is:%d" % money)
print("You can go")

Keywords: Python

Added by Static_Nexus on Wed, 06 Nov 2019 23:45:32 +0200