Close

21/11/2017

Prototype of a text game Part 2

Next we use a for — in loop, which I have before, if I remember correctly, you didn’t. A for — in loop runs as many iterations as elements in the object is placed after the keyword in. In our case, this is a list of actions. The function enumerate() is used to ensure that at each stage of the cycle in the computer’s memory was not the only element of the list (I called him a temporary variable action, but generally this can be set more or less loose manner), but its ordinal position (number). In other words, the for — in loop means the following: “for each item element within the object elements execute the code block, located after the colon”.

Image result for game in Python

We use a loop function enumerate to display a listing of all available actions, which are variable actions, which became since the previous version not one strength and list string’s.

In programming, counting starts from zero (i.e. ordinal ‘[A]sk’ — 0, ‘[B]ribe’ — 1, and so on), and therefore to the first sequence number was not 0, the start count to the number you need to add one. Also number need to convert from integer data type to string data type using the function str(). We are 100% sure that each variable number will first be of the type integer, and therefore no additional checks are not do. Finally, we output each element of the list of actions as a menu item using the print () function. Inside it we put a variable of type string using the + character. That’s why we translated for each iteration of the loop the variable number in the string data type.

LINES 39-79: we use the symbol, which introduces the player to the function lower () to lowercase letters become upper case, so typing the commands easier. Accordingly, in the rest of the file I changed in terms of lowercase letters to uppercase, everywhere put single quotes to keep the unity of style, and also added the ability to input not only the first letters of words, but of numbers, by which they are in the menu. Inside the branch exiting the game I use the function clear_screen () to output after the game the screen just the message with gratitude — without any waste. Completes file creation sample-class Game, so the game will start immediately after we run the interpreter.

File bureaucrat.py became much smaller after I smashed its components on the other two files, but no change has not done.

LINES 1-8: in addition to the blessings module, already described in the comments to the file game.py I also imported the contents of the file text.py otherwise the bureaucrats will not know what to say.

LINE 13-23: the initialization of the class Bureaucrat I added a gender attribute. To select an attribute use the random() function from the random module, randomly giving the number of type float (that is, with a lot of decimal places) from 0 to 1. Of conventional construction it appears that with a probability of 49.5% of a bureaucrat to be a woman, with a probability of 49.5% male, and in every hundredth case, the bureaucrat will be included in the number of people that are United under the term intersex. Grades and actions I put directly as arguments to the function random.choice(), to make the code more concise.

LINES 25-43: finally, I made it with connection strings, functions from a module, blessings, and special characters so that the main action of a bureaucrat appear more beautiful. Important characteristics of the rank and in the mood bold, and where necessary, use the newline character \n.

Unfortunately, italic and some other text formatting of blessings not earned in the “Terminal” of Mac OS X. but even in these lines you can find references to the guess what game!

Image result for game in Python

Here is our game in the terminal window now. Much better than the assemblies of the mess of text that was there before.

Although we have solved only a small part of the problems facing us, at the current stage, the prototype is suitable for the level of proof of concept. So, next time I will not only continue to address priorities identified in the previous release, but will write a comprehensive design document that will focus on the upcoming stages of development. If you do not understand something, write comments under the material and social networks — will be glad and any other feedback. If you are more experienced in programming people than I, with pleasure will listen to substantive criticism. Thank you and until next time!

Leave a Reply

Your email address will not be published. Required fields are marked *