程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python exercises

編輯:Python

python Exercises

1、 A supermarket held a lottery , The winning condition is that the sum of the numbers of the membership card number is 16. Members enter their own four digit card number , That is, you can know whether you have won the prize . Such as : The winning result :true

#1
num = input(" Please enter the four digits of the membership card :")
if int(num[0]) + int(num[1]) + int(num[2]) + int(num[3]) == 16:
result = True
print(" The winning result :", result)
else:
print(" unfortunately , Please do it again ")
#2
num1 = int(num)
a = num1 // 1000
b = num1 // 100 - 10 * a
c = num1 // 10 - a * 10 - b * 100
d = num1 - a * 1000 - b * 100 - c * 10
print(a + b + c + d)
if a + b + c + d == 16:
print(" congratulations , I won the prize ")
else:
print(" unfortunately , Please do it again ")

2、 The user enters two numbers , Judge if it's an odd number , The output is as follows :“ Is odd ?true/false”

num1 = int(input(" Please input the number 1:"))
num2 = int(input(" Please input the number 2:"))
c =True
def panduan(num):
if (num % 2) == 0:
return False
else:
return True
a = str(panduan(num1))
b = str(panduan(num2))
print(a)
print(" Is odd ?" + a + "/" + b)

3、 The user enters an integer , Judge whether it is 7 Multiple , With boolean Value of output .

num = int(input(" Please enter :"))
if num % 7 == 0:
print(" yes 7 Multiple :",True)
else:
print(" yes 7 Multiple :", False)

4、 Leap years only need to meet one of the following conditions (1)1、 Can be 4 to be divisible by , But can't be 100 to be divisible by ; (2)2、 Can be 400 to be divisible by Please according to the year , Whether the output is a leap year .

year = int(input(" Enter a year : "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} It's a leap year ".format(year))
else:
print("{0} It's not a leap year ".format(year))
else:
print("{0} It's a leap year ".format(year))
else:
print("{0} It's not a leap year ".format(year))

5、 Calculate the perimeter and area of the rectangle

a = int(input(" Please enter a long :"))
b = int(input(" Please enter width :"))
def sc(x,y):
return a * b , 2 * (a + b)
s , c = sc(a,b)
print(" The perimeter of :",s)
print(" The perimeter of :",c)

6、 The implementation of sorting and exchanging two numbers

num1 = int(input(" Please input the number 1:"))
num2 = int(input(" Please input the number 2:"))
num3 = 0
#1 Introducing third party variables 
num3 = num2
num2 = num1
num1 = num3
#2python Self contained 
num1 , num2 = num2 , num1
#3 No third party variables 
num1 = num1 +num2
num2 = num1 -num2
num1 = num1 - num2

7、 Print shopping receipt , Output is as follows :
goods The unit price Number amount of money
T T-shirt ¥245 2
Tennis shoes ¥570 1
Tennis racket ¥320 1

discount :0.8
The total amount of consumption :
Actual payment :¥1500
Give change : Points earned from this shopping ( consumption 100 Yuande 3 branch ):

num1 = int(input(" buy T Number of shirts :"))
num2 = int(input(" How many tennis shoes to buy :"))
num3 = int(input(" Buy the number of tennis rackets :"))
price1 = 245
price2 = 570
price3 = 320
a = 0.8
b = 1500
price_totle = price1 * num1 + price2 * num2 + price3 * num3
jifen = (b - price_totle * a) // 100 * 3
print("{:*^30}".format(" Consumption list "))
print(" goods \t\t"," The unit price \t"," Number \t"," amount of money ")
print("T T-shirt \t\t",price1,"\t",num1,"\t\t",price1 * num1)
print(" Tennis shoes \t",price2,"\t",num2,"\t\t",price2 * num2)
print(" Tennis racket \t",price3,"\t",num3,"\t\t",price3 * num3)
print()
print(" discount :",a)
print(" The total amount of consumption :",price_totle * a)
print(" Actual payment :",b)
print(" Give change :",b - price_totle * a)
print(" Points earned from this shopping :",jifen)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved