Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hangman Help.
#5
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

>>> nums = [8, 0, 1]
>>> random.choice(nums)
0
>>> random.choice(nums)
8
Next, 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
1
But 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
4
Combine 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
Reply


Messages In This Thread
Hangman Help. - by 2skywalkers - Jun-24-2018, 09:21 PM
RE: Hangman Help. - by Larz60+ - Jun-24-2018, 09:47 PM
RE: Hangman Help. - by ichabod801 - Jun-25-2018, 03:22 AM
RE: Hangman Help. - by 2skywalkers - Jun-25-2018, 08:36 PM
RE: Hangman Help. - by ichabod801 - Jun-26-2018, 02:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Hangman metro17 4 3,045 Sep-18-2019, 10:59 AM
Last Post: perfringo
  Trouble coding hangman Tay 1 2,380 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 2,414 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Python hangman help A1395 11 7,247 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Hangman 2skywalkers 3 73,722 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 12,142 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Simple Hangman Game Issue andrew95 2 4,371 Apr-02-2018, 02:24 PM
Last Post: andrew95
  Designing Hangman game spacetimeguy 2 5,157 Feb-02-2018, 08:55 PM
Last Post: spacetimeguy
  TKinter Hangman Game Bumble 1 20,622 Jul-19-2017, 06:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020