Python single loop exercise

Learn python for the first time and ask the boss for correction,

         1. There are 1020 watermelons. After half of the total is sold on the first day, two more are sold. After that, the remaining half is sold every day. How many days can they be sold out?

         It will be sold out in 8 days

sum = 1020
day = 0

while( sum > 0) :
    day += 1
    sum = sum - (sum // 2+2)
print(str(day)+'We can sell out watermelon in one day')

        2. The problem of monkeys eating peaches: on the first day, monkeys picked several peaches and ate half of them immediately. They didn't enjoy it, so they ate one more. The next morning, they ate half of the remaining peaches and one more. After that, they ate the remaining half and one of the previous day every day. When I wanted to eat again on the 10th morning, I saw that there was only one peach left. How many were picked on the first day?

sum = 1

for i in range(9 ,0, -1):
    sum = (sum+1)*2
    print('The first%s Days before eating%s A peach' % (i, sum))

         4. Arbitrarily input an integer (less than 10 bits) and output its total number of bits.

num= float(input('Please enter a number\n'))

if(num<0 and num>999999999):
    print('Please re-enter')
    while (num>1):
     num /=10
print('It has',a,'position')

                4. Calculate the sum of all integers within 1000 that cannot be divided by 7

while method  

a = 0
sum = 0
    if(a%7 != 0 ):
        sum += a
print('The sum that cannot be divided by 7 is:',sum)

for method  

# sum = 0
# for i in range(0,1000):
#     if (i % 7 != 0) :
#         sum += i
# print('cannot be divided by 7, the sum is: ', sum)

The running result is

          5. The principal of 10000 yuan is deposited in the bank with an annual interest rate of 3%. Every year, the principal and interest are added together as the new principal. After 5 years, the principal obtained is

for i in range(1,6):
    monay =10000*((1+0.003)**i)

          6. Calculate the result of 1 + 2-3 + 4-5 + 6-7... + 100

The first method is to judge whether it is even, even plus, odd minus,

In the second way, find the odd and even number, subtract the odd number from the even number and add 2


 for i in range (2,101):


         sum +=i


         sum -=i


sum1 = 0 #oushu

sum2= 0  #jishu

for i in range(1,101):

    if(i%2 ==0):

        sum1 +=i


        sum2 +=i



        7. The thickness of a piece of paper is about 0.08mm. How many times can it be folded in half to reach or exceed the height of Mount Everest (8848.13m)

i = 0

h = 0.00008 #Thickness of paper         (mm)

f = 8848.13 #Height of Mount Everest (m)




print('Fold in half',i,'It can reach or exceed the height of Mount Everest')

         8. Enter a positive integer from the console and calculate the factorial of the number. For example, if you enter 5, the factorial is 5 * 4 * 3 * 2 * 1

result = 1

n= int(input('Please enter a number'))

for i in range(1,n+1):

    result = result * i

    print('The first%s The result of factorial is%s' % (i, result))

print('The factorial result of this number is:',result)

9. Calculate total sales amount
The retail price of a book in the book wholesale store is 26.5 yuan / copy. If the customer buys more than 100 copies (including 100 copies) at one time, the price of each book will be 10% off. If the customer buys more than 500 copies (including 500 copies) at one time, the price of each book will be 20% off and 1000 yuan will be returned to the customer. Please calculate the amount payable for the purchase of 8 copies, 150 copies and 600 copies respectively. The number of books required to be purchased is entered from the console.


List the operation steps first, and then refer to step to complete the code.
Some parameters should appropriately obtain the entered value from the keyboard.

while a:
    shuliang = int(input('How many books did you buy\n'))
        price = shuliang*26.5
        a = bool(input('Enter any key to continue. Press enter to exit'))
    elif(shuliang>=100 and shuliang<500):
        price = shuliang*26.5*0.9
        a = bool(input('Enter any key to continue. Press enter to exit'))
        price =shuliang*26.5*.8-1000
        a = bool(input('Enter any key to continue. Press enter to exit'))
print('Thank you for using')


Tags: Python

Posted on Fri, 08 Oct 2021 15:02:01 -0400 by jola