Catalog
1. Use code to implement the following business logic
Write the code with the following variable name = aleX. Please implement each function as required.
Remove the spaces on both sides of the value corresponding to the name variable and output the processing results
Determine whether the corresponding value of the name variable starts with "al" and output the result
Determine whether the corresponding value of the name variable ends with "X" and output the result
Replace "l" with "p" in the value corresponding to the name variable and output the result
The value corresponding to the name variable is divided according to "l" and the result is output.
Capitalize the value of the name variable and output the result
Change the value of the name variable to lowercase and output the result
Output the second character of the value corresponding to the name variable?
Please output the first three characters of the value corresponding to the name variable?
Please output the last two characters of the value corresponding to the name variable?
Please output the index position of "e" in the corresponding value of the name variable.
-
Get the subsequence and remove the last character. For example: oldboy gets
o
l
d
b
oAnswer:
# Examples given name = " aleX" # 1. Remove the spaces on both sides of the value corresponding to the name variable and output the processing results. name1 = name.strip() print(name1) # 2. Determine whether the corresponding value of the name variable begins with "al" and output the result. bool_start = name.startswith('al') print(bool_start) # 3. Determine whether the corresponding value of the name variable ends with "X" and output the result. bool_end = name.endswith('X') print(bool_end) # 4. Replace "l" with "p" in the corresponding value of the name variable and output the result. name_replace = name.replace('l','p') print(name_replace) # 5. Separate the value of name variable according to "l" and output the result. name_split = name.split('l') print(name_split) # 6. Capitalize the value of the name variable and output the result name_upper = name.upper() print(name_upper) # 7. Reduce the value of the name variable to lowercase and output the result name_lower = name.lower() print(name_lower) # 8. Output the second character of the value corresponding to the name variable? print(name[2]) # 9. Output the first three characters of the value corresponding to the name variable? print(name[:3]) # 10. Output the last two characters of the value corresponding to the name variable? print(name[-2:]) # 11. Output the index position of "e" in the value corresponding to the name variable print(name.find('e')) # 12. Get the subsequence and remove the last character. For example: oldboy gets for i in range(len(name)-1): print(name[i])
2. Write age guessing games with the following requirements
(Reference: https://www.cnblogs.com/nickchen121/p/11069989.html)
- There may be users who accidentally enter spaces after entering age, such as 18, please do so.
- There may be malicious input from users which will lead to program error. If you are amused, please do something about it.
- If the user hasn't guessed correctly three times, he can choose to continue playing or quit (custom exit condition)
- If the user guesses correctly, you can choose two prizes (only one prize at a time): {0:'buwa', 1:'bianxingjingang', 2:'aoteman', 3:'python from introduction to abandonment'}
- Users can withdraw from the program after choosing the prize. Users can also withdraw from the program directly without choosing the prize.
- It's normal to have no idea. Knock on me three times first.
Answer:
import random # Random one age age = random.randint(0,120) # Prize List price_dict = {0:'buwawa',1:'bianxingjingang',2:'aoteman',3:'<python From Initial to Abandoned'} # Define a variable to store the number of games count = 0 while True : guess_age = input('Please enter your guessed age (range 0).-120 Between:') # Clear the possible spaces when the user enters first guess_age = guess_age.strip() # Determine whether the information entered by the user is all numbers if guess_age.isdigit() != True: print('Your input is incorrect. Please re-enter it.') continue guess_age = int(guess_age) if guess_age > age: print('Sorry, you guessed big. Please guess again.') elif guess_age < age: print('Sorry, your guess is small. Please guess again.') else: choose = int(input('Congratulations. You guessed right. You can choose the prize.,Input 0-3 Choose:')) print(f'The prizes you choose are:{price_dict[choose]}') break count += 1 if count == 3: num = input('Three chances have been exhausted. Do you want to continue playing? y,Exit please press q):') if num == 'y': count = 0 continue elif num == 'q': print('You opted out of the game.') break else: print('You entered incorrectly and quit the game') break