Function exercises

1. Accuracy of typing software

Write functions to calculate the accuracy of string matching (similar to typing software)

orginStr is the original content, and userStr is the user's input

2. Simulation roulette lottery game:

The turntable is divided into three parts: first prize, second prize and third prize

When the wheel turns, it's random,

If the range is between [0,0.08], it represents the first prize

If the range is between [0.08, 0.3], it represents the second prize

If the range is between [0.3, 1], it represents the third prize

1000 people participated in the simulation activity, and the number of prizes of each level should be prepared during the simulation game

import random     
dict={
    'The first prize':(0,0.08),
    'Second award':(0.08,0.3),
    'Third Award':(0.3,1)
}
def fun():
    count=random.random()
    for k,v in dict.items():
        if  v[0]<=count<v[1]:
            return k
resultdict={}
for i  in range(1000):
    res=fun()
    if res not in resultdict:
        resultdict[res]=1
    else:
        resultdict[res]+=1
for k,v in resultdict.items():
    print(k,'----->',v)

 

3. Ground mouse game

Write code to simulate the little game of ground mouse i,

Let's say there are 5 holes in total, and the mouse will have a random hole in them

A man opens a hole at random. If there is a mouse, it means he has caught it,

If not, continue to hit the gopher, but the gopher may jump to another hole

4. Using functions and dictionaries to realize the administrator background member management system

def userAdd():        ###userAdd add user's function
    print("Add member information".center(50, '*'))
    addUser = input("Add member name:")      ####addUser receives the added user
    if addUser in userspass:              #####userspass dictionary for storing user name and password
        print ("user%s Already exist" % (addUser))
    else:
        addPasswd = input("Password:")                 ###addPasswd accepts the password entered
        userspass[addUser] = addPasswd
        print("Add user%s Success" % (addUser))


def userdel():       ###userdel delete user's function
    print("Delete member information".center(50, '*'))
    delUser = input("Delete member name:")       ##### delUser receives the user name to delete
    if delUser not in userspass:
        print('%s user does not exist' %(delUser))
    else:
        userspass.pop(delUser)         #####If the dictionary deletes the key, the corresponding value will be deleted

        print("Delete member%s Success!" % (delUser))


def catuser():         ###catuser function to view information
    print("View member information".center(50, '*'))
    for k, v in userspass.items():   ####Used to traverse key and value
        print(k, '--->', v)
 ###   print(userspass.items())    ###It can also be used to view all key values

print("Administrator login".center(50, '*'))
inuser = input('UserName: ')
inpasswd = input('Password:')
userspass={}

if inuser == 'admin' and inpasswd == 'admin':
    print("Administrator login succeeded!")
    print("Membership management".center(50, '*'))
    while True:
        print("""
                            //Operation directory

               1 -    Add member information
               2 -    Delete member information
               3 -    View member information
               4 -    Sign out
            """)
        choice = input("Please select your action:")

        if choice == '1':
            userAdd()
        elif choice == '2':
            userdel()
        elif choice == '3':
            catuser()
        elif choice == '4':
            print('Exit succeeded!!')
            exit()
        else:
            print("Please enter the correct choice")
else:
    print("Administrator login failed!")

5. Define a function. The input is an integer and the output is the sum of the squares of the bits of the integer

And then input three numbers of K, a and B to determine how many numbers in (a, b) make f (I) × k = I

def f(n):
    sum=0
    n=str(n)
    for i in n:
        sum+=int(i)**2
    return sum
print(f(13))
print(f(207))
###Receive variables k,a,b
s=input('Enter three numbers:')
#Storage shaping: k,a,b
li=[]
for item in s.split():
    li.append(int(item))
k,a,b=li
##Judge whether the conditions are met:
count=0
for i in range(a,b+1):
    if k*f(i)==i:
        count+=1
print(count)

 

 

Posted by scbookz on Sun, 05 Jan 2020 21:59:17 -0800