官术网_书友最值得收藏!

Interactivity

When creating a program which runs on the command line, there are essentially only two ways to get input from the user.

The first is by parsing command-line arguments. These are the extra information written on the same line when running an executable from the command line. For example: python3 -i blackjack.py. Here, we  have passed in a flag of -i telling the interpreter to end in interactive mode, and the filename blackjack.py.

The second is the one which we have used throughout our blackjack game – input. The input function allows the user to type anything in to the command line and returns this as a string. As you may have noticed from our constant need to use the lower function and a while loop to validate the user's choices, this is far from ideal.

Compare this to the various ways with which we collected information from the user in Chapter 1, Meet Tkinter. We replicated the input function's capabilities with an Entry widget. Whilst we could use multiple Entry widgets and validation loops in a graphical interface, this would not solve the problem.

In order to collect a choice of two options from the user, such as in the case of choosing between hit and stick, we could simply use two buttons. This means the user cannot pick an option which was not given.

When choosing to play again, it would make more sense to use one of the message box windows to get the user's choice. The askyesno box seems like the perfect choice for this job. This again removes the need to validate whether the user's answer is one we expected.

Even if the command-line version of the game had a way of conveniently gathering user input, there's also the question of familiarity.

主站蜘蛛池模板: 巴里| 民乐县| 铜陵市| 商水县| 万年县| 定西市| 惠安县| 若羌县| 乌海市| 佛冈县| 前郭尔| 虞城县| 新邵县| 竹北市| 东丰县| 奉新县| 仁化县| 屏山县| 浦北县| 晴隆县| 河东区| 施甸县| 通江县| 云和县| 宁海县| 临泽县| 莒南县| 四川省| 涞源县| 柘荣县| 舞阳县| 防城港市| 安平县| 汽车| 乌鲁木齐市| 南华县| 北京市| 伊金霍洛旗| 卢氏县| 商河县| 丹江口市|