import ast
defscore_rank():
score_list = ast.literal_eval(input("Enter scores:"))
score_Rank =["A","B","C","D","不及格"]
full_mark =100for value in score_list:if value >= full_mark -10:print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[0]))elif value >= full_mark -20:print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[1]))elif value >= full_mark -30:print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[2]))elif value >= full_mark -40:print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[3]))else:print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[4]))
10.2
defreverse_list():# Read numbers as a string from the console
s =input("Enter numbers separated by spaces from one line: ")
items = s.split(",")# Extracts items from the string
numbers =[eval(x)for x in items]# Convert items to numbers
numbers.reverse()print(numbers)
10.3
defNumber_of_statistics():
s =input("Enter numbers separated by spaces from one line(ends with with 0): ")
items = s.split(",")# Extracts items from the string
numbers_list =[eval(x)for x in items]# Convert items to numbers
counts =100*[0]for i inrange(0,101):for value in numbers_list:if value == i:
counts[i]+=1
j =0for count in counts:if count >0:print(j,"occurs",count,"times"if count >1else"time")
j +=1
10.4
defmain():
s =input("Enter the numbers: ")
items = s.split()# Extracts items from the string
scores =[eval(x)for x in items ]# Convert items to numbers
numOfAbove =0
average =sum(scores)/len(scores)for score in scores:if score >= average:
numOfAbove +=0print("Average is "+str(average))print("Number of scores above or equal to the average "+str(numOfAbove))print("Number of scores below the average "+str(len(scores)- numOfAbove))
main()
10.5
defPrint_Numbers():
s =input("Enter numbers separated by spaces from one line:")
items = s.split(" ")# Extracts items from the string
list1 =[eval(x)for x in items]
list2 =set(list1)# 集合里面的元素不重复
list3 =list(list2)print("The distinct numbers are:")for i inrange(len(list2)):print(list3[i], end=" ")
10.6
import math
defprime():
NUM_OF_PRIMES =50# Store prime numbers
primeNumbers =[]
count =0# Count the number of prime numbers
number =2# A number to be tested for primeness
isPrime =True# Is the current number prime?print("The first 50 prime numbers are \n")# Repeatedly find prime numberswhile count < NUM_OF_PRIMES:# Assume the number is prime
isPrime =True
i =0while i < count and primeNumbers[i]<= math.sqrt(number):# If true, the number is not primeif number % primeNumbers[i]==0:# Set isPrime to false, if the number is not prime
isPrime =Falsebreak# Exit the for loop
i +=1# Print the prime number and increase the countif isPrime:
primeNumbers.append(number)
count +=1# Increase the countif count %10==0:# Print the number and advance to the new lineprint(number)else:print(number, end=" ")# Check if the next number is prime
number +=1
10.8
deffind_the_min_index():# Read numbers as a string from the console
s =input("Enter scores separated by spaces from one line: ")
items = s.split()# Extracts items from the string
numbers =[eval(x)for x in items]# Convert items to numbersprint("The index of the smallest element is "+str(indexOfSmallestElement(numbers)))defindexOfSmallestElement(list):min=list[0]
minIndex =0for i inrange(1,len(list)):ifmin>list[i]:min=list[i]
minIndex = i
return minIndex
10.10
defrevrse_list():# Read numbers as a string from the console
s =input("Enter numbers: ")
items = s.split()# Extracts items from the string
numbers =[eval(x)for x in items]# Convert items to numbers
reverse(numbers)print(numbers)defreverse(list):for i inrange(len(list)//2):list[i],list[len(list)- i -1]=list[len(list)- i -1],list[i]# 元素互换returnlist
10.12
import random
defmain():# Read numbers as a string from the console
s =input("Enter numbers: ")
items = s.split()# Extracts items from the string
numbers =[eval(x)for x in items]# Convert items to numbersprint("The gcd of", numbers,"is", gcd(numbers))defgcd(numbers):
g = numbers[0]for i inrange(1,len(numbers)):
g = gcd2(g, numbers[i])return g
# Return the gcd of two integersdefgcd2(n1, n2):
g =1# Initial gcd is 1
k =2# Possible gcdwhile k <= n1 and k <= n2:if n1 % k ==0and n2 % k ==0:
g = k # Update gcd
k +=1return g # Return gcd
10.1import astdef score_rank(): score_list = ast.literal_eval(input("Enter scores:")) score_Rank = ["A","B","C","D","不及格"] full_mark = 100 for value in score_list: if value >= full_mark - 10: print("stuend %d is %d