Function uses exercises

# / Usr / bin / env python3! 
# - * - Coding: UTF-8 - * -
# @time: 2019/9/3 10:42
# @author: "little more meat"
# @email: [email protected]
@file #: doroo_homework20190902_func.py
# @Software: PyCharm


"" "
the TODO:. 1. All numbers input by the user after taking the remainder 20 multiplying the number of digits entered by the user uncertain
" ""


#-defined functions, the user is determined a string is not a number (including positive and negative numbers, decimals)
DEF input_isint (STR):
iF str.replace ( '.', '',. 1) .isdigit (): # determines whether or not a positive number, including fractional
return True
elif str.startswith ( '-') and str [1:]. replace ( '.', '', 1) .isdigit (): # determines whether negative, including fractional
return True
the else:
return False


DEF func_calculate (* args):
Determining the number of users # input parameter is empty, the process directly returns 0
IF args .__ len __ () == 0:
return 0
the else:
CAL =. 1
for Element in args:
CAL * = Element
return CAL% 20 is


NUM = [] # values stored user number input
# user input for data calculation, the input letter n ended input, a non-n then computes
True the while:
user_input = iNPUT (F "Please enter {len (num) +1} th digit (inputting letters used to make input operations n end):")
IF user_input.lower () == 'n':
BREAK
# call the function determines whether the user input is not a number
elif input_isint (user_input):
num.append (float (user_input))
the else:
( "! you entered is not a number, please re-enter \ the n-") Print
# call the function to calculate the user computing data entered
the result = func_calculate (* NUM)
Print ( "all the numbers you entered after multiplying the result of 20 to take the remainder is:% f" the result%)


# TODO: 2, the length of the write function to check the incoming list If greater than 2, then the content remains only the first two lengths, and returns the new content
DEF check_len (* args):
IF args .__ len __ ()> 2:
args = args [: 2] # two lengths before removing the parameter content
return list (args) # returned as a list


# verification function
li = [ ' big brother ', 1,24,567,]
Print (check_len (Li *)) incoming into the reference list #


# TODO: 3, the definition of a function, and passing a string dictionary, the dictionary is determined whether the value of the string, If the string is not in the dictionary, it is added to the dictionary and returns a new dictionary
DEF str_is_in_dict (string, dic):
IF string in dic.values (): # judge whether the string passed in the value of the dictionary, if in return Ture
return True
the else:
dic.update (string = string) # string value is not in the dictionary, add to the dictionary. key value is hardcoded String
return DIC


# verification function
S = 'ABC'
D = {. 1: 'John Doe', 2: 'John Doe'}
Result = str_is_in_dict (S, D)
Print (Result)


# TODO: 4, by defining a calculator function, call function with two arguments, then prompted to select the plus [1] [2] [3] Save [4] In addition to operations by, after selecting the operation returns the corresponding value.
def calculator (tup, oper): # calculator function, the first parameter is a tuple type, comprising two operands. The second parameter is the operator.
a, b = tup # tuple unpacking
IF Oper == '+':
return A + B
elif Oper == '-':
return ab &
elif Oper == '*':
return A * B
the else:
return A / B


True the while:
FIRST_NUM = iNPUT ( "Please enter the first operand:")
second_num = iNPUT ( "Please enter the second operand:")
# call the function defined in the first question, the content is determined whether the input value
input_isint IF (FIRST_NUM) and input_isint (second_num):
# on both operands are converted to floating point numbers, write tuple
tuple_num = (a float (FIRST_NUM), a float (second_num))
BREAK
the else:
print ( "! Your input operand is not numeric please re-enter \ n-")
# define operators dictionary
oper_dict = { '1': ' +', '2': '-', '3': '*', '4': '/'}
sELECT iNPUT = ( "Please select [1] [2] plus Save [3] [4] in addition to by your actions:")
# If the user input is selected in the range of key values dictionary, called the calculator function calculation.
oper_dict.keys in the SELECT IF ():
EQ = Calculator (tuple_num, oper_dict.get (the SELECT))
Print (EQ)
the else:
Print ( "! Please enter an incorrect choice")


"" "
5, looking for a soccer team aged 15 to 22-year-old girl doing cheerleaders (including 15-year-old and 22 years old) was added.
after writing a program that asks the user's gender and age, and then displays a message indicating whether the person can join the team, asked 10 times the total number of outputs to meet the conditions.
"" "


DEF lalateam (Age, Sex):
IF Sex == 'female' and 15 <= int (Age) <= 22:
Print ("! Congratulations, you can join the cheerleading \ n "






count = 0 # demographic eligible
for Times in the Range (10):
user_sex = the INPUT (f "Please enter {times + 1} user's gender:")
user_age = the INPUT (f "Please enter {times + 1} user's age: ")
# of age to determine whether the input is an integer, if the call is an integer function to determine whether the requirements of cheerleading. In line with demographic criteria.
user_age.isdigit IF ():
IF lalateam (user_age, user_sex):
COUNT = 1 +
the else:
Print ( "! Sorry, you do not meet the requirements of the cheerleading \ the n-")
Print ( "The number of qualifying has% d" % count)

Guess you like

Origin www.cnblogs.com/momoon/p/11498881.html