Python Forum
Need help and suggestions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help and suggestions
#1
I need your suggestions and modification for this simple game. Please help.
from random import randint
guesses = 0
print("Hello. Let's play some guessing game")
print("The rule is I think of a number and you'll have to guess it.")
print("I'll give you hints if your guess is too high or low")
while guesses < 5:
    guess = int(input("Enter a number from 1 to 99: "))
    guesses += 1
    print("This is your guess", guesses)
    if guess < generator():
        print("Guess is low")
    elif guess > generator():
        print("Guess is high")
    elif guess == generator():
        guesses = str(guesses)
        print("You guess it in: ", guesses + "guesses")
    elif guess != generator():
        number = str(number)
        print("Sorry. The secret number was: ", number)

def generator():
    return randint(1, 10)

def rand_guess():
    random_number = generator()
    guess_left = 3
    flag = 0
    while guess_left > 0:
        guess = int(input("Pick your number to enter the lucky draw\n"))
        if guess == random_number:
            flag = 1
            break
        else:
            print("Wrong guess")
        guess_left -= 1
    if flag is 1:
        return True
    else:
        return False
Reply
#2
What would you like suggestions about and in what way would you like to modify it ?
Reply
#3
(May-06-2019, 12:17 AM)jsoberano Wrote:
    if flag is 1:
        return True
    else:
        return False
Don't use is like that. It should only be used to check for object identity, such as something is None. flag is 1 will not work on all versions of python, and only works on cpython because small integers are cached instead of reused (ie: please don't rely on implementation details). More generally, if you CAN use ==, then you SHOULD use ==.

Also, that whole if statement at the end can just be replaced with return flag == 1. Or, more simply, as just return flag, since it'll only ever be 1 or 0.

Also, because flag only ever has two values, and those values imply on/off or yes/no or good/bad, it should be a bool with a value of either True or False, instead of 1/0.
Reply
#4
I noticed few things:
1. rand_guess() function is not called at all
2. generator() function is called multiple times throghout the game, constantly changing the actual number (this shouldn't happen, it should be called once before each game and the result should be kept and used for comparison with the actual guesses)
3. generator() function has the upper limit of 10, the description of the game says that number up to 99 should be picked
4. number variable is never initialized
5. elif guess != generator() is never satisfied because previous conditions include ">" and "<"
6. generator() function is called before being initialized (you can just cut/paste definition of that function over the main "while guesses" loop
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Suggestions on my code YorgosTheProgramer 2 3,318 Mar-13-2017, 01:22 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020