GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Nov-2008, 06:20
GariusZee GariusZee is offline
New Member
 
Join Date: Nov 2008
Posts: 3
GariusZee is on a distinguished road
Exclamation

Simple guessing game: missing spaces


I am a newbie when it comes to writing code. I did some research and tought that I will learn Python first, as it will be usefull ofr my future goals.

I have tried to do some exercises and have created a very simple guessing game. The game works, however I am having a problem with the code. I am wondering if someone will be able to look at the code and tell me what I am doing wrong. Thank you very much



Python Code:
print 'Hello, what is your name?'
myName = raw_input()

import random

guessesTaken = 0

number = random.randint(1, 20)

print 'Well,' + myName + ', I am thinking of a number between 1 and 20.'

while guessesTaken < 6:
    print 'Take a guess.'

    guess = raw_input()
    guess = int(guess)

    guessesTaken = guessesTaken + 1

    if guess < number:
            print 'Your guess is cold.'

    if guess > number:
            print 'Your flying in the clouds.'

    if guess == number:
        break

if guess == number:
        guessesTaken = str(guessesTaken)
        print 'Congratulations, I think you cheated' +  myName 
        print 'You guessed the number in'  +   guessesTaken   +   'guesses!'

if guess != number:
    number = str(number)
    print 'Haha, the number was'  + number

The problem i am getting is that when I run the game the last sentence doesn't seperate and is together

Code:
Congratulations, I think you cheatedJosh You guessed the number in2guesses! >>> ================================ RESTART ================================ >>>

What am I doing wrong
Last edited by LuciWiz : 10-Nov-2008 at 06:32. Reason: Please insert your Python code between [py] & [/py] tags
  #2  
Old 10-Nov-2008, 06:37
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Help


Without being a Python programmer, I assume filling-in the desired spaces when printing out the strings ought to do it:

Python Code:
print 'You guessed the number in '  +   guessesTaken   +   ' guesses!'
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 10-Nov-2008, 07:47
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,627
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Simple guessing game: missing spaces


LuciWiz is right, you just have to add the spaces manually.

Additionally, I thought I'd help optimize your code. Don't think that you did it wrong. As long as it works, it's a success.

I just wanted to give you some pointers that will make your life easier in the future because you will end up writing less code and it will be easier to maintain.
Python Code:
import random    #ensure your imports are first to prevent errors

#the raw_input() function allows for a text prompt so you don't have to have
#a separate print

myName = raw_input('Hello, what is your name?')    

guessesTaken = 0

number = random.randint(1, 20)

#add spaces around inserts to prevent word bunching
print 'Well, ' + myName + ' , I am thinking of a number between 1 and 20.'    

while guessesTaken < 6:
#again, you can put the prompt directly in raw_input(), and you can wrap
#the whole thing in the int() function

    guess = int(raw_input("Take a guess."))    

    guessesTaken += 1    #shortcut 

    if guess < number:
            print 'Your guess is cold.'

    elif guess > number:    #use elif for related if statements
            print 'Your flying in the clouds.'

    elif guess == number:
        break    #you could also make a function call

if guess == number:    #a function call would eliminate this duplicate statement
        guessesTaken = str(guessesTaken)
        #don't forget the spaces
        print 'Congratulations, I think you cheated ' +  myName 
        print 'You guessed the number in '  +   guessesTaken + ' guesses!'

#you don't need to test for guess != number; just use an else statement
else:
    #you don't need to convert the number to a string first
    print 'Haha, the number was '  + str(number)

Alternatively, you can also try string formatting the numbers. For example, the last line could be
Python Code:
print "Haha, the number was %d" % number 
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #4  
Old 10-Nov-2008, 20:44
GariusZee GariusZee is offline
New Member
 
Join Date: Nov 2008
Posts: 3
GariusZee is on a distinguished road

Re: Simple guessing game: missing spaces


Thank you guys for all your help. Thanks for the pointers I realy appreciate it. I need all the tips and tricks I can get my hands on. Thanks again
  #5  
Old 12-Nov-2008, 21:51
GariusZee GariusZee is offline
New Member
 
Join Date: Nov 2008
Posts: 3
GariusZee is on a distinguished road

Re: Simple guessing game: missing spaces


Sorry for the doublepost guys I have another question to ask.

I would like to improve this code make a more efficent version. I don't want people to write the code, but to give me some ideas and tips. I would like to test myself, so if you have any tips please put them here but not in code please

Thank you
  #6  
Old 13-Nov-2008, 07:54
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,627
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Simple guessing game: missing spaces


Well, my first suggestion is to put a function or two in it.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:02.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.