0

I started to code a guess the number type of game. When I execute the program, it either flows perfectly, either doesn't work...

import random
from random import randint

print("Welcome to guess the number!\nDo you want to play the game?")

question = input("")

if question == "Yes".lower():
print("Sweet! Let`s begin!\nGuess the number between 1 and 10!")

    number = random.randint(1, 10)
    guess = int(input("Take a guess!\n"))

    if guess > number:
        print("Your guess is too high")
        guess = int(input("Take a guess!\n"))

    if guess < number:
        print("Your guess is too low")
        guess = int(input("Take a guess!\n"))

    if guess == number:
        print("Your guess was correct!")

elif question == "No".lower():
    print("Too bad! Bye!")
    quit()

I absolutely no idea whether it happens because of the code, or pycharm is to blame!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ImaNoob
  • 1
  • 1
  • 2
    Hello and welcome to the great world of programs. In our world, “doesn`t work” is expressed in terms of detailed explanation and stack trace if it raises an exception. – spectras Oct 15 '16 at 14:44
  • The `print('Sweet'...` needs to be indented and you probably want to change if `question == "Yes".lower()` to `question.lower() == "yes"`. Other than that, your application kind of works although it depends what you want to achieve. – Alex Oct 15 '16 at 14:44

2 Answers2

0

You really need a while loop. If the first guess is too high then you get another chance, but it then only tests to see if it is too low or equal. If your guess is too low then your second chance has to be correct.

So you don't need to keep testing, you can simplify, but you should really do this at the design stage. Here is my version:

import random
#  from random import randint   << no need for this line

print("Welcome to guess the number!")

question = input("Do you want to play the game? ")

if question.lower() == "yes":
    print("Sweet! Let`s begin!\nGuess the number between 1 and 10!")

    number = random.randint(1, 10)
    guess = None                     # This initialises the variable

    while guess != number:           # This allows continual guesses

        guess = int(input("Take a guess!: ")) # Only need one of these

        if guess > number:
            print("Your guess is too high")
        elif guess < number:     
            print("Your guess is too low")
        else:   # if it isn't < or > then it must be equal!
            print("Your guess was correct!")

else:
    # why do another test?  No need.
    print("Too bad! Bye!")

    # No need for quit - program will exit anyway
    # but you should not use quit() in a program
    # use sys.exit() instead

Now I suggest you add a count of the number of guesses the player has before they get it right, and print that at the end!

Edit: You will note that my import statement differs from that given by @Denis Ismailaj. We both agree that only one import is required, but hold different opinions as to which one. In my version I import random, this means that you have to say random.randint whereas in the other you only say randint.

In a small program there isn't much to choose between them, but programs never get smaller. A larger program, which could easily be importing 6,7,8 or more modules, it is sometimes difficult to track-down which module a function comes from - this is known as namespace pollution. There will be no confusion with a well-known function like randint, and if you explicitly give the name of the function then it can easily be tracked back. It is only a question of personal preference and style.

With number of guesses added:

import random

print("Welcome to guess the number!")

question = input("Do you want to play the game? ")

if question.lower() == "yes":
    print("Sweet! Let`s begin!\nGuess the number between 1 and 10!")

    number = random.randint(1, 10)
    guess = None
    nrGuesses = 0                    # Added

    # Allow for continual guesses
    while guess != number and nrGuesses < 6:    # Changed

        guess = int(input("Take a guess!: ")) # Only need one of these

        nrGuesses += 1               # Added

        if guess > number:
            print("Your guess is too high")
        elif guess < number:
            print("Your guess is too low")
        else:   # if it isn't < or > then it must be equal!
            print("Your guess was correct!")

else:
    print("Too bad! Bye!")

print("Number of guesses:", nrGuesses) # Added
cdarke
  • 42,728
  • 8
  • 80
  • 84
  • I get this when I use os.exit() : This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items. I tried to use sys.exit() but it does the same.. – ImaNoob Oct 15 '16 at 15:43
  • My bad, it should have been `sys.exit()` Maybe that is coming from PyCharm? `quit()` should only be used in interactive python - see http://stackoverflow.com/questions/6501121/difference-between-exit-and-sys-exit-in-python (`exit()` and `quit()` do the same thing). – cdarke Oct 15 '16 at 15:51
  • Also for the number of guesses I triet to write this: – ImaNoob Oct 15 '16 at 16:20
  • edit : for the nr of guesses I tried to write this: nrGuesses = 0 while nrGuesses < 6: nrGuesses += 1 Also triet to indent it everywhere , but the nrGuesses returned is always 6...Idk how to solve this , any tips? – ImaNoob Oct 15 '16 at 16:23
  • Try `while guess != number and nrGuesses < 6:`. Not sure what you mean by "indent everywhere" - you have to be selective! – cdarke Oct 15 '16 at 18:52
  • I mean that i tried to put it everywhere in my code :D different places – ImaNoob Oct 15 '16 at 21:40
-1

Here is a variation on the 2 themes already offered as answers.
In this option the guessing part is defined as a function, which allows us to offer the option of repeatedly playing the game until the user is bored stiff! The other option offered is to not require the user to input "yes" or "no" but simply something that starts with "y" or "n".

from random import randint

def game():
    print("Guess the number between 1 and 10!")
    number = randint(1, 10)
    guess = None
    while guess != number:
        guess = int(input("Take a guess!\n"))
        if guess > number:
            print("Your guess is too high")
        elif guess < number:
            print("Your guess is too low")
        else:
            print("Your guess was correct!")

    return input("Another game y/n ") #return a value which determines if the game will be repeated

print("Welcome to guess the number!\nDo you want to play the game?")
question = input("")
repeat = "y" # define repeat so that the first iteration happens

if question.lower().startswith('y') == True:
    print("Sweet! Let`s begin!")
    while repeat.lower().startswith('y') == True: # while repeat == "y" keep playing
        repeat = game() # play the game and set the repeat variable to what is returned
print("Bye Bye!")
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60