The Seven Legs Shooting Game
Several children are playing a game of patting their legs at seven, starting with 1 and patting their legs when they count to a multiple of seven or when the end number is seven. Now count from 1 to 99. Assuming that everyone is correct, how many times should they clap their legs?
The first idea is to use continue statement in the for loop to calculate the number of leg taps. First of all, assuming that the maximum number of leg taps is 99, every time the trigger meets the conditions, it will directly jump to the next loop, the last total minus 1 will not be executed, and when the conditions are not met, the total will be reduced by 1. Therefore, the total is actually reduced by the number of the conditions that do not meet the conditions, and the code is as follows:
total = 99 # Variables to record the number of leg taps for number in range(1,100): # Create a loop that counts from 1 to 99 if number&7 == 0: # Determine if it is a multiple of 7 continue # Jump to the next loop else: string = str(number) # Convert the value to a string to make it easier to determine whether the tail number is 7 or not if ('7'): # Determine if the tail number digit is 7 continue # Jump to the next loop total -= 1 #Can tap the legs minus 1 print("Count from 1 to 99 and clap your legs together.",total,"Second.") #Display the number of leg taps
Here we learn a new function as endswith(), let's first discuss what the endswith() function does.
Function: endswith()
Role: determine whether the string ends with the specified character or sub-string, often used to determine the file type
Related functions: determine the beginning of a string startswith()
The results of the run are as follows:
The second realization of the idea: by setting a counter, each trigger condition once the counter will be added 1. The code is as follows:
total = 0 # Variables to record the number of leg taps for number in range(1,100): # Create a loop that counts from 1 to 99 if number&7 == 0: # Determine if it is a multiple of 7 total +=1 # of leg taps plus 1 else: string = str(number) # Convert the value to a string to make it easier to determine whether the tail number is 7 or not if ('7'): # Determine if the tail number digit is 7 total +=1 # of leg taps plus 1 print("Count from 1 to 99 and clap your legs together.",total,"Second.") #Display the number of leg taps
Both of these can be accomplished, and it's interesting to note that the first is actually a reverse way of thinking; we're so used to addition that we rarely use subtraction. The second is a way of thinking that many of us are used to. In the process of learning python, it's important to think outside the box a little bit, and sometimes it's even faster to think in the opposite direction!
summarize
To this point, this article on the python realization of the seven shoot the legs of the game of thought in detail on the article is introduced to this, more related python realization of the seven shoot the legs of the game content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!