5 Replies - 935 Views - Last Post: 05 December 2013 - 09:25 AM Rate Topic: -----

#1 Kerplop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 17-September 13

Program running incorrectly in .exe file but not in python itself.

Posted 04 December 2013 - 08:34 PM

Here is the code for a simple game of hangman I'm doing with three categories...

import random
import os
#these are what will add the 'limbs' to the hangman after each wrong letter guess
hangmanpics = ['''

                +---+
                |   |
                    |
                    |
                    |
                    |
                    |
              =========== ''','''
                +---+
                |   |
                O   |
                    |
                    |
                    |
                    |
              =========== ''','''
                +---+
                |   |
                O   |
                |   |
                    |
                    |
                    |
              =========== ''','''
                +---+
                |   |
                O   |
               /|   |
                    |
                    |
                    |
              =========== ''','''
                +---+
                |   |
                O   |
               /|\  |
                    |
                    |
                    |
              =========== ''','''
                +---+
                |   |
                O   |
               /|\  |
               /    |
                    |
                    |
              =========== ''','''
                +---+
                |   |
                O   |
               /|\  |
               / \  |
                    |
                    |
              =========== ''']

#opening the file that contains all 50 states
infile = open('states.txt', 'r')
State = infile.read().replace('\n', ' ')
infile.close()
#opening the file that contains all the capital cities
infile = open('cities.txt', 'r')
City = infile.read().replace('\n', ' ')
infile.close()
#opening the file that contains all of the 1rst world countries
infile = open('countries1.txt', 'r')
Country = infile.read().replace('\n', ' ')
infile.close()

#these cases display the description of the chosen category, and 
def States():
    print('You will have to guess the name of one of the 50 states in the US (no spaces)')
    return State

def Cities():
    print('You will have to guess the name of one of the capital cities (no spaces)')
    return City

def Countries():
    print('You will have to guess this 1rst world country (no spaces)')    
    return Country
def err():
    print('You will get a random word from the above 3')
    return State + City+ Country

def MenuMessage():

    print('Select One Category')
    print(' 1: States')
    print(' 2: Capital cities')
    print(' 3: 1rst world countries')
    print(' 4: Randomize it') 

    choice = {"1": States,
              "2": Cities,
              "3": Countries}
    
    choose = input()
    return choice.get(choose,err)().split()

    
def getRandomword(wordlist):
    wordindex = random.randint(0,len(wordlist)-1)
    return wordlist[wordindex]

def display(hangmanpics,missedletters,correctletters,secretword):
    os.system('cls')
    print(hangmanpics[len(missedletters)])
    print()
    
    print('Missed Letters: ', end = ' ')
    for letter in missedletters:
        print(letter, end=' ')
    print()
    print()

    blanks = '_' * len(secretword)

    for i in range(len(secretword)):
        if secretword[i] in correctletters:
            blanks = blanks[:i] + secretword[i] + blanks[i+1:]

    for letter in blanks:
        print(letter,end=' ')
    print()
    print()


def playerGuess(alreadyGuessed):
    while True:
        print('Guess a Letter  ')
        guess = input()
        guess = guess.lower()
        if len(guess) != 1:
            print('Please only enter a single letter')
        elif guess in alreadyGuessed:
            print('You\'ve already guessed this letter, please try another')
        else:
            return guess

def playagain():
    print('Do you want to play again? (Yes(y) or No(n)). Choosing \'No\' will close the program')
    return input().lower().startswith('y')

print(hangmanpics[6])
words = MenuMessage()
missedletters = ''
correctletters = ''
secretword = getRandomword(words)

done = False

while True:
    display(hangmanpics,missedletters,correctletters,secretword)
    guess = playerGuess(missedletters + correctletters)

    if guess in secretword:
        correctletters = correctletters + guess
        found = True
        for i in range(len(secretword)):
            if secretword[i] not in correctletters:
                found = False
                break

        if found:
            print('Nice job!')
            print('The secret word was: ' +secretword.upper())
            done = True
    else:
        missedletters = missedletters + guess
        if len(missedletters) == len(hangmanpics)-1 :
            display(hangmanpics,missedletters,correctletters,secretword)
            print('Sorry buddy, the word was: ' +secretword)
            done = True

    if done:
        if playagain():
            os.system('cls')
            words = MenuMessage()
            missedletters = ''
            correctletters = ''
            done = False
            secretword = getRandomword(words)
        else:
            break



Basically my problem is is that if I double click on the .py file and it brings up the .exe of the program, it doesn't work properly in the .exe file but it works just fine then if I run it in IDLE. I use Python 3.2.

Here are some puushes to help show what I'm having an issue with.
In IDLE - http://puu.sh/5CxOc/286a5ca4ce.png
In the .exe file - http://puu.sh/5CxQN/89f0b33ff7.png

It's saying that if I enter in one single letter in the .exe, it thinks it isn't for some reason. The code hasn't been changed at all. Any idea or fixes to this? Any help is appreciated, thanks.

Sorry for double posting, but since there's no editing posts I feel I need to. Hope you guys are able to work with this without having me do something like uploading the text files or something :/

Is This A Good Question/Topic? 0
  • +

Replies To: Program running incorrectly in .exe file but not in python itself.

#2 Mekire   User is offline

  • D.I.C Head

Reputation: 118
  • View blog
  • Posts: 216
  • Joined: 11-January 13

Re: Program running incorrectly in .exe file but not in python itself.

Posted 05 December 2013 - 01:54 AM

What OS are you using and how are you creating the exe?

Also could you please add this line to your code (and remake the exe of course):
print(repr(guess))

As follows:
guess = guess.lower()
print(repr(guess))
if len(guess) != 1:
    print('Please only enter a single letter')

And tell me what it displays.

-Mek

Edit: Oh wait, I believe your language confused me. You aren't creating an exe file are you. You are just trying to run the file by double clicking.

This post has been edited by Mekire: 05 December 2013 - 02:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 Kerplop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 17-September 13

Re: Program running incorrectly in .exe file but not in python itself.

Posted 05 December 2013 - 03:25 AM

View PostMekire, on 05 December 2013 - 01:54 AM, said:

What OS are you using and how are you creating the exe?

Also could you please add this line to your code (and remake the exe of course):
print(repr(guess))

As follows:
guess = guess.lower()
print(repr(guess))
if len(guess) != 1:
    print('Please only enter a single letter')

And tell me what it displays.

-Mek

Edit: Oh wait, I believe your language confused me. You aren't creating an exe file are you. You are just trying to run the file by double clicking.


Yeah all I've been doing is double-clicking. I actually didn't know what making an .exe file required a process other than clicking it. And it shows me this when I put in that code http://puu.sh/5CLSy/d5344e2119.png.

And Windows 7, sorry.
Was This Post Helpful? 0
  • +
  • -

#4 Kerplop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 17-September 13

Re: Program running incorrectly in .exe file but not in python itself.

Posted 05 December 2013 - 04:45 AM

Edit - Either way, I don't know why the program doesn't work correctly when double clicking it but works fine when I run it in IDLE.
Was This Post Helpful? 0
  • +
  • -

#5 Mekire   User is offline

  • D.I.C Head

Reputation: 118
  • View blog
  • Posts: 216
  • Joined: 11-January 13

Re: Program running incorrectly in .exe file but not in python itself.

Posted 05 December 2013 - 07:02 AM

Ok. Your system is adding a carriage return character onto your string. This is apparently a known issue. It should be fixed if you install a more recent version of python 3 (I also am running on windows 7 with python 3.2.2 and don't have this glitch).

If you don't feel like doing this, this should fix your code:
guess = guess.lower().strip()
if len(guess) != 1:
    print('Please only enter a single letter')
but that strip wouldn't normally be necessary.

-Mek

This post has been edited by Mekire: 05 December 2013 - 07:02 AM

Was This Post Helpful? 0
  • +
  • -

#6 Kerplop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 17-September 13

Re: Program running incorrectly in .exe file but not in python itself.

Posted 05 December 2013 - 09:25 AM

View PostMekire, on 05 December 2013 - 07:02 AM, said:

If you don't feel like doing this, this should fix your code:
guess = guess.lower().strip()
if len(guess) != 1:
    print('Please only enter a single letter')
but that strip wouldn't normally be necessary.

-Mek


This worked, thank you SO much.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1