## Help with a beginners task!

This is the place for queries that don't fit in any of the other categories.

### Help with a beginners task!

Hello everybody! I am completely new to Python. I started yesterday by watching some youtube videos.
I got as far in my tutoials as to make a little number guessing name. The game randomizes a number and your task is to guess it... You all probably have done this task yourselves.

Anyway i wanted to pimp my game a bit so after a while I figured out how to make it tell me if my guess was higher or lower than the wanted number.. (Figured it out all by myself wohoo! )
Though I feel my solution might not be the best because it will generate a lot of code if I add a lot of guesses to the game.

Here is the code so you might get things into context:

Code: Select all
`import randomimport sysenter = ("Please press Enter to continue...")computernum = random.randint(1, 100)print ("Hello. Welcome to SUPER MEGA GUESSING GAME!")print ("Please guess a number between 1 and 100")guess1 = eval(input("My first guess is: "))if guess1 == computernum:    print ("You made it you lucky bastard! Free Cookies!")    input (enter)    sys.exit(0)elif guess1 > computernum:    print ("The number you entered is too high.. Try again")    guess2 = eval(input("My second guess is "))    if guess2 == computernum:        print ("You made it you lucky bastard! Free Cookies!")        input (enter)        sys.exit(0)    elif guess2 > computernum:        print ("The numbered you entered is to high I'm afraid... Please try one last time: ")        guess3 = eval(input("My third guess is: "))        if guess3 == computernum:             print ("You made it you lucky bastard! Free Cookies!")             input (enter)             sys.exit(0)        else:            print ("GAME OVER... ")            computernum = str(computernum)            print ("The number was"+computernum)            input (enter)            sys.exit(0)    elif guess2 < computernum:        print ("The numbered you entered is to low I'm afraid... Please try one last time: ")        guess3 = eval(input("My third guess is: "))        if guess3 == computernum:             print ("You made it you lucky bastard! Free Cookies!")             input (enter)             sys.exit(0)        else:            print ("GAME OVER... ")            computernum =str(computernum)            print ("The number was"+computernum)            input (enter)            sys.exit(0)elif guess1 < computernum:    print ("The number you entered is too low.. Try again")    guess2 = eval(input("My second guess is :"))    if guess2 == computernum:        print ("You made it you lucky bastard! Free Cookies!")        input (enter)        sys.exit(0)    elif guess2 < computernum:        print ("The numbered you entered is to low I'm afraid... Please try one last time: ")        guess3 = eval(input("My third guess is: "))        if guess3 == computernum:             print ("You made it you lucky bastard! Free Cookies!")             input (enter)             sys.exit(0)        else:            print ("GAME OVER... ")            computernum = str(computernum)            print ("The number was "+computernum)            input (enter)            sys.exit(0)`

As you see it's not very effective (Even though it works)
What i want help with is how make this more efficiant!
Is there anyone who can help me?
Thanks for helping out a nooblet
//Thellamaking
Thellamaking

Posts: 2
Joined: Mon Sep 23, 2013 6:18 pm

### Re: Help with a beginners task!

your going to want to get rid of the eval:
Code: Select all
`eval(input("My first guess is: "))`

and either check the string returned by input() or convert it to an int via the built-in function int()

and learning functions would remove a lot of this repetitive code
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam

metulburr

Posts: 1943
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: Help with a beginners task!

metulburr wrote:your going to want to get rid of the eval:
Code: Select all
`eval(input("My first guess is: "))`

and either check the string returned by default or convert it to an int via the built-in function int()

and learning functions would remove a lot of this repetitive code

Hmmm...

When I remove the eval it won't recognize the correct answer.
I am using the 3.3.2 version if that matters..

I made a quick code to show what i mean:

Code: Select all
`import randomimport sysenter = ("Please press enter to continue...")print ("Hello and welcome to this magical number guessing game!")print ("Please choose a number between 1 and 100")num = random.randint(1, 1)Guess1 = input("My first guess is: ")if Guess1 == num:    print ("You win TEH GAME!")    input (enter)    sys.exit(0)else:    print ("Sorry that number isn't the one!")    print ("Please try again... 2 tries remaining.")    Guess2 = input("My second guess is the number: ")if Guess2 == num:    print ("Congratulations you've won!")    input (enter)    sys.exit(0)else:    print ("You loose the game.. I was only joking with 2 more tries")    print ("This was your last chance!")    print (" GAME OVER ")    input (enter)    sys.exit(0)`

In this code if I press on it should say that i win right?
But without the eval it doesn't.

I just get it like this:
Hello and welcome to this magical number guessing game!
Please choose a number between 1 and 100
My first guess is: 1
Sorry that number isn't the one!
Please try again... 2 tries remaining.
My second guess is the number: 1
You loose the game.. I was only joking with 2 more tries
This was your last chance!
GAME OVER
Please press enter to continue...

If i add Eval (input) it works tho'
Mabye I should read up a bit more on this input...
Thellamaking

Posts: 2
Joined: Mon Sep 23, 2013 6:18 pm

### Re: Help with a beginners task!

He meant to say that you should use int() in place of eval().
Join the #python-forum IRC channel on irc.freenode.net for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from

micseydel

Posts: 2250
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Help with a beginners task!

Code: Select all
`if guess1 == computernum:`

thats because guess1 is of type string and computernum is of type int
you cannot compare an int and a string and get the expected results. input() always returns the input as a string.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam

metulburr

Posts: 1943
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: Help with a beginners task!

You could also learn about while loops. That would eliminate the need for a LOT of the lines in your code
dboxall123

Posts: 122
Joined: Fri Jul 12, 2013 5:28 pm

### Re: Help with a beginners task!

In case you're interested in the thought behind the "eval() is bad" comments: http://nedbatchelder.com/blog/201206/ev ... erous.html

Marbelous

Posts: 308
Joined: Fri May 31, 2013 8:12 pm

### Re: Help with a beginners task!

int() (or float()) is not just safer with untrusted input, it's semantic. It's about nice code, not just security. Plus the exception you get from int() is much more helpful than one you'd get from eval() if you gave it wrong input.
Join the #python-forum IRC channel on irc.freenode.net for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from

micseydel

Posts: 2250
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Help with a beginners task!

I agree you should try and check out while loops and functions, this will help you reduce your code, and build smarter things faster. Good job on your first day though!
New Users, Read These Hints

Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap

Posts: 657
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: Help with a beginners task!

Kebap wrote:I agree you should try and check out while loops and functions, this will help you reduce your code, and build smarter things faster. Good job on your first day though!

AND he used Code Tags!

Marbelous

Posts: 308
Joined: Fri May 31, 2013 8:12 pm

Return to General Coding Help

### Who is online

Users browsing this forum: snippsat, Yahoo [Bot] and 4 guests