Hi,
I'm trying to make a hangman game that should look like this:
Welcome to Hangman
______
Your guess: c
Success!
__cc__
Your guess: b
Failure! You have tried tried 1 times!
__cc__
.
.
.
Your guess: r
Success!
soccer
You won!
etc
In this game I'm trying to make it so that I first need to give a file - where game takes words - at commandline as a parameter to game. I've done that so far that program takes randomly a word from a certain file (here words.txt), prints blanks as a hintword and starts asking letters. My problem is that I can't make the program to check for the letters in word and replace blanks with guessed letter.
So far my code looks like that: -
import sys
-
-
if sys.argv[1] == 'words.txt':
-
list = open('words.txt','r')
-
a = list.readlines()
-
-
import random
-
-
b = random.choice(a)
-
-
print 'Welcome to Hangman'
-
c = '_'*(len(b)-1)
-
print c
-
for i in range(1,6):
-
mark = raw_input('Your guess: ')
-
check = b.find(mark)
-
if check != -1:
-
print 'Succes!', mark, 'is in word at position', check
-
c = c[:check] + mark + c[check + 1:]
-
print c
-
else:
-
print 'Failure! You have tried tried %d times!' %(i)
-
-
else:
-
print '[Errno2] No such file or directory: %s' %(sys.argv[1])
-
Can anyone tell me how to solve this problem? Sorry for the dots, but I didn't know any other way to get that structure showing right.
Edit: Thanks for the tip. I edited the code to show right :)
8 3728
I haven't taken a look at your problem yet, but you can enclose your code in code tags, [CODE] and [/CODE], and the parser won't eat the indentation.
Edit:
Thanks, that's much better.
For some reason, you can't replace a character in a string with another character. The only way I know to get around this is to slice the string into two pieces and put the new character between them. It seems like there must be a better way to do this, but here it is: - c = c[:check] + mark + c[check + 1:]
And list is a reserved word. You shouldn't be using it as a variable name.
Hope this helps.
Thanks for the help. It helped to replace those blanks with letters, but now I encountered a new one problem. If we have a word "soccer" and we are guessing the letter "c", program only takes the first letter and converts it, but not the other c. So here we would end in result "__c___". So in the end we will have result "soc_er".
I edited the current code for the first post.
How about replacing the letters from b with underlines after they have been guessed?
Edit:
Never mind, that would make the user have to guess the same letter more than once.
I think I need to fix that find-structure so that program wont stop finding at the first hit, but also checks the whole word for hits. Any recommendations for how to do this would be great as I am not very skilled with python... yet :)
I have a book on Python with a hangman game example in it. It works something like - Clear out c.
-
For each letter in b:
-
If the letter is equal to mark:
-
Add mark to c.
-
Else:
-
Add a blank to c.
Hope this helps.
Edit:
Sorry, still won't work. Try it like this: - Create a new string. Let's call it new_c.
-
For each letter in b:
-
If the letter is equal to mark:
-
Add mark to new_c.
-
Else:
-
Add a letter from the old c to new_c.
-
Replace c with new_c.
bvdet 2,851
Expert Mod 2GB
This returns a list of indexes of letter in word s: - def idxList(s, letter, idx=0):
-
''' Return a list of indices of "letter" in string "s".'''
-
if not type(s) == str:
-
raise AttributeError, 'Argument 0 must be a string'
-
idxList = []
-
while True:
-
idx = s.find(letter, idx)
-
if idx == -1:
-
return idxList
-
idxList.append(idx)
-
idx += 1
Example:
>>> idxList('mississippi', 's')
[2, 3, 5, 6]
>>>
To substitute the letters in the hint string, create a list of letters:
Iterate on the indexes list, and assign the list item to letter. - string_list[idx] = letter
Join the letters to form the new hint string:
Hi,
Thanks to you all for help. Somehow I wasn't able to make my code working exactly how you suggested, so I made it in different way. Maybe this coding isn't the best possible, but at least this works :) -
import sys
-
-
if sys.argv[1] == 'words.txt':
-
list1 = open('words.txt','r')
-
list2 = []
-
for word in list1.readlines():
-
sana = word.replace('\n','')
-
list2.append(sana)
-
-
import random
-
-
b = random.choice(list2)
-
-
print 'Welcome to Hangman'
-
hint = '_'*len(b)
-
print hint
-
-
counter = 0
-
-
while counter < 5:
-
if hint == b:
-
print 'You win!'
-
print 'Goodbye'
-
break
-
else:
-
pass
-
mark = raw_input('Your guess: ')
-
if mark == '':
-
counter += 1
-
print 'Failure! You have tried tried %d times!' %(counter)
-
else:
-
counter2 = 0
-
if mark in hint:
-
counter += 1
-
print 'Failure! You have tried tried %d times!' %(counter)
-
print hint
-
elif mark in b:
-
print 'Success!'
-
for i in b:
-
if mark == i:
-
x = list(hint)
-
x[counter2] = mark
-
hint = "".join(x)
-
counter2 += 1
-
if hint != b:
-
print hint
-
else:
-
pass
-
else:
-
counter += 1
-
print 'Failure! You have tried tried %d times!' %(counter)
-
if counter != 5:
-
print hint
-
else:
-
pass
-
-
else:
-
print 'Game over! you lose!'
-
print 'The word was %s' %(b)
-
print 'Goodbye'
-
-
else:
-
print "[Errno 2] No such file or directory: '%s'" %(sys.argv[1])
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: rainbowii7 |
last post by:
Calling all programmers for helllllllllllllllppppp!!! i am currently
doing a uni degree and our lecturers have set us the task of making a
game in JavaScript.
i chose to do a hangman game and...
|
by: tigrfire |
last post by:
So I'm trying to write a hangman game and the output is coming out a
little strange. Here's my code thus far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
...
|
by: princessfrost |
last post by:
Hi! I was wondering if someone could please help me with a hangman program that I have to do. I have some ideas, but really don't know what to do or where to start. My program needs to be:...
|
by: araujo2nd |
last post by:
Originally Posted by araujo2nd
my name is andre, im from south africa, i was wondering if any1 could help me with a hangman application, im now in grade 11 and have a huge portfolio piece to do by...
|
by: kaka_hunter |
last post by:
#include <iostream>
#include <fstream>
using namespace std;
const int max_tries=7;
int earnings=0;
int wordnum;
void getword ()
{
ifstream fin;
|
by: av3rage |
last post by:
I have never done any programming in my life but I have decided to go into engineering and in doing so we have to take this intro to programming course and I am pretty clueless. I am starting to get...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |