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

Python programming problem daily practice Day2 (with answers)

編輯:Python

Python Practice programming problems every day day2

    • Python Practice programming problems every day day1( Answer attached )
    • One 、 By 8 Divisible numbers
    • Two 、 multiplication table
    • 3、 ... and 、 Judgement primes
    • Four 、 Repeated string
    • 5、 ... and 、 Password game
    • Conclusion

The most effective way to improve your programming ability is to knock the code

Author's brief introduction : Hello, I am programming ID
Personal home page : Programming ID Of csdn Blog
Series column :Python
Recommend a programming question brush artifact Click jump to enter the website

Python Practice programming problems every day day1( Answer attached )

One 、 By 8 Divisible numbers

subject : Write a while Loop to judge whether the decimal value corresponding to the input string is 8 Divisible numbers , Boolean variable required active To control the timing of the end of the cycle .
Use... At the beginning of each cycle print() Statement to output a string on one line “Please enter a positive integer!\nEnter ‘quit’ to end the program.” ,
If the read string is equal to ’quit’, Boolean variable active The value of is changed to False,
Otherwise, convert the string to an integer , If it can be 8 Divisible is 8 Multiple , Then use print() Statement to output a similar string on one line ’80 is a multiple of 8.' The sentence of ,
Otherwise use print() Statement to output a similar string on one line ’4 is not a multiple of 8.‘ The sentence of ,
Then the cycle ends , Once again into the while Condition test in the loop .
Input description
Make sure that each line of input has only numbers or strings ’quit’, And ensure that the numbers are legal , The scope is [1, 100].

active = True
while active:
num = raw_input()
print("Please enter a positive integer!\nEnter 'quit' to end the program.")
if num=='quit':
active = False
elif int(num) not in range(1,101):
active = False
elif int(num)%8==0:
print('{} is a multiple of 8.'.format(num))
else:
print('{} is not a multiple of 8.'.format(num))

Two 、 multiplication table

subject : Shizhenxiang is reciting the multiplication table , He wants to print out the multiplication table . Now enter a number n, Please print the... In the 99 multiplication table 1~n That's ok .( The first multiplier is the line number , Formulas are separated by spaces )
Input description
Enter an integer n,1\le n\le 91≤n≤9
Output description
Before outputting 99 multiplication table n That's ok .
Example 1
Input :3
Output

1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
n = int(input())
for i in range(1, n + 1): # In the interval (1, n + 1) in 
for j in range(1, i + 1):
ans = i * j # Multiply the result 
# With f start , Contains {} The expression is replaced by the value of the expression when the program runs 
print(f"{
i}*{
j}={
ans}", end = " ")
print() # Each number is multiplied to a new line 

3、 ... and 、 Judgement primes

subject : Prime numbers are divided by 1 And myself , There is no other factor . Niuniu wants to know a number n Is it a prime , Please use Python The program helps him judge .
Input description
Enter a positive integer n,n>2
Output description
If it's a prime number , The output Yes, Otherwise output No.
Example 1
Input :7
Output :Yes

num = input()
flag = "Yes"
if num <= 2:
print "wrong input"
else:
for i in range(2,num+1):
if i * i < num:
if num % i == 0:
flag = "No"
break
else:
break
print flag

Four 、 Repeated string

subject : character string target Is a long string , Please in string target Find the pattern string in patten Number of occurrences , And find the first place where it appears .( Substring non overlapping statistics , If not found , The position is -1)
Input description
First line input target, Second line input patten.
All strings contain only upper and lower case letters .
Output tracing Statement :
The first line outputs patten stay target Is the number of times , It's a nonnegative number .
The second line outputs patten stay target The first place in , If not, output -1.( Position from subscript 0 Start )
Example 1
Input
IamNiuNiuFromNiuKeWang
Niu

Output
3
3

t = raw_input()
p = raw_input()
print t.count(p)
print t.find(p)

5、 ... and 、 Password game

subject : Xiao Ming and his sister play password games together , Xiaoming, as the sender, will send a 4 Give the whole number of digits to my sister , Mei Mei will crack the password after receiving it .
The solution is as follows : Add... To every number 3 Divided by 9 Replace this digit with the remainder of , And then I will 1 Position and number 3 Bit digital switching , The first 2 Position and number 4 Bit digital switching .
Please output the password that Niu Mei cracked .
Input description
Enter a four digit integer .
Output description
Output the cracked password , In the form of four digits .
Example 1
Input :1234
Output :6745
remarks : Input will not have a prefix 0, But the output should be kept in front 0

num = int(input())
a = num//1000 #1
b = (num-a*1000)//100 #2
c = (num-a*1000-b*100)//10#3
d = (num-a*1000-b*100-c*10)#4
new_c = (a+3)%9 #4
new_d = (b+3)%9 #5
new_a = (c+3)%9 #6
new_b = (d+3)%9 #7
new_num = new_a*1000+new_b*100+new_c*10+new_d
if new_a != 0:
print(new_num)
else:
print('0' + str(new_b*100+new_c*10+new_d))

subject : Xiao Ming 、 Younger sister 、 Xiao Hong discusses the size of her lucky number together . According to the input, record the lucky numbers of the three people in a list , Then find out what is the smallest lucky number ? And output the sorted results of the list .
Input description
Enter three integers in sequence .
Output description
The first line outputs the smallest lucky number of the three .
The second line outputs the sorted list .
Example 1
Input

5
4
3
Output
3
[3, 4, 5]

a = []
niu = int(input())
mei = int(input())
kele = int(input())
a.append(niu)
a.append(mei)
a.append(kele)
print(min(a))
print(sorted(a))

Conclusion

Very easy to use a brush question website ! Let's work together ! come on. !!!
The difficulty of the topic can be chosen by oneself
Program the answer online ,( You can also check the answers by yourself ) Very convenient
Programmers brush question artifact website Click the link to register and you can brush the question


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