1
\$\begingroup\$

I created a simple 15-line number guessing game in Python 2.7:

# Guess the number game
username = raw_input("What is your name? -> ")
guess = input("%s, I'm thinking of a number between 1 and 10. -> " % username)

answer = 7 # sets the answer
answer = guess

if guess < 7:
    print "That's too low."

if guess > 7:
    print "That's too high."

if guess == 7:
    print "Good. Thanks for playing."

What is a proper way I could make it a little more efficient?

\$\endgroup\$
0

2 Answers 2

4
\$\begingroup\$

Contradicting statements

answer = 7 # sets the answer
answer = guess

Here, you are setting a variable only to immediately set it to another value in the very next line, effectively invalidating the first line. This also makes your code confusing: you do that thing you are doing in these lines, and then you start comparing the guess with this magic number 7 (which is was answer used to be).

Also, why is the answer being set to the guess? Those are two completely different things. Instead of what you are doing, to make your code much more understandable, you should get rid of this line:

answer = guess

Then, instead of comparing guess with 7, compare it with answer.

if guess < answer:
    print "That's too low."

if guess > answer:
    print "That's too high."

if guess == answer:
    print "Good. Thanks for playing."

Now your code is much more readable and follows better practices.


elif

Using elif is good because if the first conditional check fails, the rest will not be executed, which speeds up your code. Unfortunately, elif can not be used in every case due to specific code-based reasons.

Luckily for you, your code can use elif. So, instead of having three separate if statements, you should have one with two elifs:

if guess == answer:
    print "Good. Thanks for playing."

elif guess < answer:
    print "That's too low."

else:
    print "That's too high."

I also re-arranged your code. Now, the most unique conditional comes first. And, instead of a final elif, I put an else because if the guess is not greater than or equal to the answer, it has to be less than it.

\$\endgroup\$
0
\$\begingroup\$

Here is a cool method

x = int(input('Guess the number '))
dif = x - 7
if x == 7: print('right on!')
else: print ("That's too " + (bool(1 + sign(dif))*'high' or bool(1 + sign(-dif))*'low'))

The logic is to use bool operator, and how it can interact with strings using * operator.

Of course you can work more on the guessing mechanism and how the game actually works, this just makes giving feedback easier.

Edit, thanks to @Graipher in comments print("That's too {}".format(["low", "high"][guess > answer])

A much more elegant way.

\$\endgroup\$
6
  • \$\begingroup\$ If you want to use a boolean, it is easier to use it as an index: "That's too {}".format(["low", "high"][guess > answer]). \$\endgroup\$
    – Graipher
    Jul 3, 2018 at 9:44
  • \$\begingroup\$ Oh, thank you. This is good stuff, I'm just starting out in python, still learning so :) \$\endgroup\$ Jul 3, 2018 at 9:49
  • 2
    \$\begingroup\$ Note that both this method and the one suggested by @Graipher in the comments make use of language 'hacks' and are not generally more readable than the longer, ordinary approach. \$\endgroup\$
    – Daniel
    Jul 3, 2018 at 11:06
  • 1
    \$\begingroup\$ @Daniel Indeed. Slightly less hackish would be "That's too {}".format("low" if guess < answer else "high") (besides the long version, of course). \$\endgroup\$
    – Graipher
    Jul 3, 2018 at 11:08
  • \$\begingroup\$ But sometimes, such hacks can improve the code's performance. For example, here if the answer is high, the code has to go through an extra statement of checking for if, while in Daniel's code it essentially is the step in which the assignment also takes place. Small things i guess, but i couldn't help but notice. \$\endgroup\$ Jul 3, 2018 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.