Jun-26-2018, 02:49 AM
First of all, you don't need to shuffle and then pick an item from the list. The random module can do both at once with the choice function
See how he prints before the loop and at the end of the loop? Try switching it to print just at the start of the loop.
In a real hangman game, you lose on the sixth wrong guess. So I would start with
Finally, there is his for loop inside the while loop, again with range(len(something)). Here, the zip function is very useful. It its most basic usage, it takes two lists and returns a sequence of a pair of the first item in each list, a pair of the second item in each list, and so on.
>>> nums = [8, 0, 1] >>> random.choice(nums) 0 >>> random.choice(nums) 8Next, looping over range(len(whatever)) is looping over the indexes of a list. It is much better to loop over the list itself.
>>> for num in nums: ... print(num) ... 8 0 1But you don't even need to do that to set up display. You can multiply a list to extend it multiple times.
>>> [801] * 8 [801, 801, 801, 801, 801, 801, 801, 801]Note that this works fine with numbers and strings, but can cause problems with other objects. Don't do this with lists.
See how he prints before the loop and at the end of the loop? Try switching it to print just at the start of the loop.
In a real hangman game, you lose on the sixth wrong guess. So I would start with
count = 6
, and then subtract one each time there is a wrong guess (each time guess not in answer
). You loop would be while count
, which will loop until count == 0
. You will also need to break out of the loop if they guess correct("_" not in display
). The break statement jumps out of the loop.Finally, there is his for loop inside the while loop, again with range(len(something)). Here, the zip function is very useful. It its most basic usage, it takes two lists and returns a sequence of a pair of the first item in each list, a pair of the second item in each list, and so on.
>>> count = [1, 2, 3] >>> list(zip(nums, count)) [(8, 1), (0, 2), (1, 3)] >>> for num, ordinal in zip(nums, count): ... print(num + ordinal) ... 9 2 4Combine this with the common technique of starting with an empty list and appending to it each time through the loop.
>>> sums = [] >>> for num, ordinal in zip(nums, count): ... sums.append(num + ordinal) >>> sums [9, 2, 4]That's a lot I just dumped on you. I would take it one piece at a time, make the appropriate change to his code, and then make sure the program still works before moving on to the next piece.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures