This forum is in archive mode. You will not be able to post new content.

Author Topic: [Python Game]Guess the number  (Read 2057 times)

0 Members and 1 Guest are viewing this topic.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
[Python Game]Guess the number
« on: July 15, 2013, 08:28:20 PM »

Code: (Python) [Select]
import random


title= '''
 _____ _     _____ ____  ____    _____  _     _____ 
/  __// \ /\/  __// ___\/ ___\  /__ __\/ \ /|/  __/ 
| |  _| | |||  \  |    \|    \    / \  | |_|||  \   
| |_//| \_/||  /_ \___ |\___ |    | |  | | |||  /_   
\____\\____/\____\\____/\____/    \_/  \_/ \|\____\ 
                                                     
 _      _     _      ____  _____ ____               
/ \  /|/ \ /\/ \__/|/  _ \/  __//  __\               
| |\ ||| | ||| |\/||| | //|  \  |  \/|               
| | \||| \_/|| |  ||| |_\\|  /_ |    /               
\_/  \|\____/\_/  \|\____/\____\\_/\_\ 




By Psycho_Coder         
                                                     
'''


def description():
    print '''
The game is simple a random number is chosen by this program everytime and you have to guess it right
If the number is less than what you guessed it will say "Too high guess" else "Too low" and if your
guess is correct then you are the winner.The number will be between 1 and 100 and you will get 5 chances
to guess it correctly
          '''


def getRandomNum():
    return random.randint(1, 100)


def game():   
    count=0
    MAX_NO_OF_GUESS=5
    num=getRandomNum()
    while count<MAX_NO_OF_GUESS:
        count +=1       
        print " Enter Your Guess :"
        guess = int(raw_input("Enter your guess: "))
        if guess < num:
            print "You guessed the number too low"
        elif guess > num:
            print "You guessed the number too High"
        else:           
            break;
   
    if guess==num:
        print "Congratulations! You guessed the number correctly."
        print "You Guessed the number correctly in ",str(count)," steps"
    if guess!=num:
        print "Aww! You Ran out of your chances."
        print "The Correct answer would have been :",str(num)       
       

if __name__ == "__main__":
    print title
    description()
    game()


Screenshot


http://i1066.photobucket.com/albums/u409/UNIVERSAHACKERS/b3a1434d-3915-47d9-bd6e-9bcd884b042f.jpg
« Last Edit: July 16, 2013, 06:40:02 PM by Psycho_Coder »
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: [Python Game]Guess the number
« Reply #1 on: July 15, 2013, 09:18:59 PM »
Somewhat messy, but it does the job.

For one thing, since you're using Python 2, the input() expression is unnecessary, as it returns an evaluated expression. (edit: unless you intended on weeding out people who will write stuff like 3+2)

Code: (Python) [Select]
print " Enter Your Guess :"
guess=input()
guess=int(guess)

...can be stripped down to:

Code: (Python) [Select]
guess = int(raw_input("Enter your guess: "))
Finally, the last three lines are best unified into a code block such as this:

Code: (Python) [Select]
if __name__ == "__main__":
    print title
    description()
    game()

So that it will call that variable and two functions only when run as a stand-alone script, rather than as a module.
« Last Edit: July 15, 2013, 09:26:50 PM by vezzy »
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: [Python Game]Guess the number
« Reply #2 on: July 16, 2013, 06:41:02 PM »
Somewhat messy, but it does the job.

For one thing, since you're using Python 2, the input() expression is unnecessary, as it returns an evaluated expression. (edit: unless you intended on weeding out people who will write stuff like 3+2)

Code: (Python) [Select]
print " Enter Your Guess :"
guess=input()
guess=int(guess)

...can be stripped down to:

Code: (Python) [Select]
guess = int(raw_input("Enter your guess: "))
Finally, the last three lines are best unified into a code block such as this:

Code: (Python) [Select]
if __name__ == "__main__":
    print title
    description()
    game()

So that it will call that variable and two functions only when run as a stand-alone script, rather than as a module.


Thank you for correcting me. I am new to python and this is not my main Language. I have edited the thread.
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline daxda

  • Peasant
  • *
  • Posts: 114
  • Cookies: 112
  • Not the guy you're looking for
    • View Profile
    • Daxda on Github
Re: [Python Game]Guess the number
« Reply #3 on: October 18, 2013, 12:35:04 PM »
I've also made a number guessing game lately :)
[gist]Daapii/7039650[/gist]
« Last Edit: October 18, 2013, 01:45:41 PM by daxda »

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: [Python Game]Guess the number
« Reply #4 on: November 02, 2013, 01:07:13 AM »
Code: (Python) [Select]
print " Enter Your Guess :"
guess=input()
guess=int(guess)

...can be stripped down to:

Code: (Python) [Select]
guess = int(raw_input("Enter your guess: "))

It could, but where is your input type validation when you do things like this all on one line, otherwise known as error handling? Granted, OP never had it in there either, but it's no excuse to have it all on one line. :)
« Last Edit: November 02, 2013, 01:09:55 AM by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: [Python Game]Guess the number
« Reply #5 on: November 02, 2013, 04:19:02 AM »
Screenshot unavailable ?
My secrets have secrets...

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: [Python Game]Guess the number
« Reply #6 on: November 02, 2013, 04:37:08 AM »
Screenshot unavailable ?

who needs it anyways?  ??? The source is given, and this is Python, you don't need to compile anything to run it yourself. :)
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.