Python implements Pig Latin mini-game instance code, piglatin

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.