## 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
we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
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
GAME OVER

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().
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
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.
we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
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: 401
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.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
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!
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

Posts: 689
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: 401
Joined: Fri May 31, 2013 8:12 pm