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

Python questions

編輯:Python

Python Problem.

1、 Prompt the user to enter a valid year from the keyboard , Print whether this year is a leap year on the screen . notes : Any year that meets one of the following two conditions is a leap year . (1) Can be 4 Divide but not be 100 to be divisible by . (2) Can be 400 to be divisible by .

Code

year=int(input(" Please enter the year :"))
if(year%4 == 0 and year%100 != 0):
print(" Leap year ")
elif(year%400 == 0):
print(" Leap year ")
else:
print(" It's not a leap year ")

Running results

2、 Enter three numbers from the keyboard , Output its largest .

Code

a=float(input("a="))
b=float(input("b="))
c=float(input("c="))
if a>b and a>c :
print(" The maximum value is :a")
elif b>c :
print(" The maximum value is :b")
else:
print(" The maximum value is :c")

function

3、 Programming , Solve the problem of monkeys eating peaches . The monkey picked some peaches on the first day , Half eaten immediately , Not yet , Another one . The next morning I ate half of the rest of the peaches , Another one . After that, I ate half and one of the rest of the day before every morning . To the first 10 When you want to eat one morning , There's only one peach left . Please pick how many peaches on the first day .

Code

def f(n):
if n > 10 or n < 1:
return 0
elif n == 10:
return 1
else:
return 2 * (f(n+1)+1)
print(f(1))

Running results

4、 Enter a string of Western characters , Count the English capital letters 、 Small letters in English 、 Space 、 Number of numbers and other characters .

Code

s=input(" Please enter the characters :")
upper=0 # Capital
lower=0 # Lowercase letters
space=0 # Space
digit=0 # Numbers
other=0 # other
for i in s:
if i.isupper():
upper += 1
elif i.islower():
lower += 1
elif i.isspace():
space += 1
elif i.isdigit():
digit += 1
else:
other += 1
print(" Capital {}, Lowercase letters {}, Space {}, Numbers {}, Other characters {}".format(upper,lower,space,digit,other))

Running results

5、 Enter a... From the keyboard 2-1000 Integer between n, Decompose it by prime factor , Output the formula after decomposition . for example : Input 60, Output 60=2*2*3*5

Code

n=int(input(" Please enter a number :"))
print(n , end ="")
i = 2
s = ""
while i <= 1000 :
if n % i == 0:
s += str(i)
n = int(n/i)
continue
i += 1
print(" = {}".format("*".join(s)))

Running results

6、 utilize random Standard library , Write a program to randomly generate 20 individual 6 Bit code , The password is required to consist of only letters A-Z And number 1-9 form .

Code

import random
n1 = [chr(i) for i in range(65,90)]
n2 = [i for i in range(1,10)]
n=n1+n2
for i in range(20):
for j in range(6):
print(random.choice(n),end="")
print()

Running results

7、 Write function level, It is required that the grade can be judged according to the entered grades . Level setting :90-100 For excellence 80-89 For good 70-79 It's medium 60-69 Pass for Less than 60 For failing .

Code

score = float(input(" Please enter your score :"))
if 90 <= score <= 100:
print(" good ")
elif 80 <= score <= 89:
print(" good ")
elif 70 <= score <=79:
print(" secondary ")
elif 60 <= score <= 69:
print(" pass ")
else:
print(" fail, ")

Running results

8、 Write function sumall, It is required to receive any number of integers and output the sum of all integers . For example, the input 2,4,6 The output 12, Input 3,5,7,9 The output 24. Tips : nums = tuple(map(int, input("please input numbers:").split(','))) # Convert multiple input numbers to tuple form .

Code

nums = tuple(map(int, input("please input numbers:").split(',')))
def sum_nums(nums):
sum=0
for i in nums:
sum += i
return sum
print(sum_nums(nums))

Running results


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