Home > front end >  Guess game - Function - Improvement
Guess game - Function - Improvement

Time:10-26

I am trying to improve my python skills and tried to reproduce a guess game where the machine tries to guess one number. You can find below the function. After the first input, the machine does not continue and have the following output:

We will play a guess game.
Is this 50 your number?
0 means too low, 1 this is the number and 2 means too high 1
0 means too low, 1 this is the number and 2 means too high 1
Congrats ! You guess after 1 tries

You can see that we have a line "0 means too low, 1 this is the number and 2 means too high 1" for nothing while it should directly say 'Congrats ! You guess after 1 tries'. Did I misplace the input?

Below you can see the function I wrote (any suggestions for improvement are welcome !!)

def guess_game():
    m = 50
    count = 0
    print('We will play a guess game.')
    print('Is this '   str(m)   ' your number?')
    myinput = input('0 means too low, 1 this is the number and 2 means too high')
    while myinput != 1:
        myinput = input('0 means too low, 1 this is the number and 2 means too high')
        count  = 1
        if myinput == '0':
            print('Not the right one. Too low')
            for m in range(m,m 1):
                m  = 1
            print('Is this '   str(m)   ' your number?')
        elif myinput == '2':
            print('Not the right one. Too high.')
            for m in range(m,m 1):
                m -= 1
            print('Is this '   str(m)   ' your number?')
        else: 
            print('Congrats ! You guess after '   str(count)   ' tries')
            break

I tried to change the place of the input() but it seems like the error is recurring.

CodePudding user response:

try it now like this:

def guess_game():
m = 50
count = 0
print('We will play a guess game.')
print('Is this '   str(m)   ' your number?')
myinput = input('0 means too low, 1 this is the number and 2 means too high #1')
while myinput != 1:
    count  = 1
    if myinput == '0':
        print('Not the right one. Too low')
        for m in range(m,m 1):
            m  = 1
        print('Is this '   str(m)   ' your number?')
    elif myinput == '2':
        print('Not the right one. Too high.')
        for m in range(m,m 1):
            m -= 1
        print('Is this '   str(m)   ' your number?')
    else:
        print('Congrats ! You guess after '   str(count)   ' tries')
        break
    myinput = input('0 means too low, 1 this is the number and 2 means too high -from loop-')

CodePudding user response:

 def guess_game():
        m = 50
        count = 0
        print('We will play a guess game.')
        print('Is this '   str(m)   ' your number?')
        myinput = input('0 means too low, 1 this is the number and 2 means too high')
        myinput = int(myinput)
        while myinput != 1:
            myinput = input('0 means too low, 1 this is the number and 2 means too high')
            count  = 1
            if myinput == '0':
                print('Not the right one. Too low')
                for m in range(m,m 1):
                    m  = 1
                print('Is this '   str(m)   ' your number?')
            elif myinput == '2':
                print('Not the right one. Too high.')
                for m in range(m,m 1):
                    m -= 1
                print('Is this '   str(m)   ' your number?')
            else: 
                print('Congrats ! You guess after '   str(count)   ' tries')
                break
        print('Congrats ! You guess after '   str(count)   ' tries')

Convert to int and insert the congrats print outside the loop

  • Related