Python implements Pig Latin mini-game instance code, piglatin
Preface:
This article focuses on Python's simple code for implementing pig Latin games. The details are as follows.
Pig Latin is a language game.
Steps:
1. Ask the user to enter an English word
2. Make sure that you enter a valid word.
3. convert words into Pig Latin
4. display the Conversion Result
I. Input
Function: raw_input () is used to output a string, wait for the keyboard to Enter a string, and end the input with Enter (or Return ).
original = raw_input("Enter a word:") print original
In the above process, the console will output What's your name ?, Wait for the user to enter a string on the keyboard until the Entert (or Return) Key ends, and assign the input string to name.
Ii. Check Empty
if len(original) > 0: print(original) else: print("empty") Iii. Check Alphabetical Characters
Function: isalpha () is used to check whether a string contains non-character symbols. If yes, False is returned. Otherwise, True is returned.
original = raw_input("Enter a word:") if len(original) > 0 and original.isalpha(): print(original) else: print("empty") 4. Create Some New Variables
pyg = 'ay' word = original.lower() first = word[0]
Above, pyg is the suffix; word is the result of lowercase of the original input string; first is the first letter of the word string
5. Concatenate
new_word = word + first + pyg
Vi. Slice
new_word = new_word[1:len(nwe_word)]
New_word [1: len (new_word)] indicates the local string between the second character and the last character.
VII. Result
pyg = 'ay' original = raw_input('Enter a word:') if len(original) > 0 and original.isalpha(): print original word = original.lower() first = word[0] new_word = word + first + pyg new_word = new_word[1:len(new_word)] print(new_word) else: print 'empty'
Summary
The above is all about the Python Implementation of Pig Latin mini-game instance code. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!