sledgefox
sledgefox

Reputation: 19

Python Guess number game - While loop not responding

I am a relative newcomer and was trying to create a game where the user and the comp take turns at guessing each others games. I know I am doing something wrong in the while / if operators when the user enters a string eg. Y.

In two places:
1. where the user decides who should go first (if a in ["Y", "y"]
2. while loop in compguess function (while ans = False:) it just skips down.

a = input("Shall I guess first? (Y/N):")

userscore = 1
compscore = 1

def compguess():
    global compscore
    low = 0
    high = 100
    c = 50
    ans = False
    while ans = False:
        print ("Is the number", c , "?")
        d = input("Enter Y if correct / L if your number is lower or H if higher:")
        if d in ["Y" , "y"]:
            ans = True
        elif d in ["H"]:
            high = c
            c = int((c + low) / 2)
            compscore = compscore + 1
        elif d in ["L"]:
            low = c
            c = int((c + high) / 2)
            compscore = compscore + 1
        else:
            print ("invalid input")

def userguess():
    global userscore
    g = r.randint(1,100)
    h = input("Enter your guess number:")
    i = int(h)
    while g != i:
        if g > i:
            h = input("You guessed lower. Guess again:")
            i = int(h)
            userscore = userscore + 1
        else:
            h = input("You guessed higher. Guess again:")
            i = int(h)
            userscore = userscore + 1

if a in ["Y", "y"]:
    compguess()
    userguess()
else:
    userguess()
    compguess()

if userscore == compscore:
    print("Its a tie !!")
elif userscore < compscore:
    print ("Congrats ! You won !")
else:
    print ("I won !! Better luck next time")

Upvotes: 0

Views: 65

Answers (3)

vzendara
vzendara

Reputation: 350

I'd change your while loop condition to:

while not ans:

Also, in r.randint(1,100), r is undefined. If you want to use a function from random, you need to import it:

You could do either import random or from random import randint. If you want to keep the r, you can do import random as r.

You should also indicate when the guessing games end. Although it's implied they flow right into each other and I was confused at first.

The wording is incorrect for the computer guessing:

Is the number 50 ?
Enter Y if correct / L if your number is lower or H if higher:H
Is the number 25 ?
Enter Y if correct / L if your number is lower or H if higher:L
Is the number 37 ?
Enter Y if correct / L if your number is lower or H if higher:Y
I won !! Better luck next time

Initial guess is 50. Then, I say my number is higher. Then the subsequent guess is lower. You reversed either the math or the phrasing.

Upvotes: 1

Pacijent
Pacijent

Reputation: 149

print ("Is the number", c , "?")
    d = input("Enter Y if correct / L if your number is lower or H if higher:")
    if d in ["Y" , "y"]:
        ans = True
    elif d in ["H"]:
        high = c
        c = int((c + low) / 2)
        compscore = compscore + 1
    elif d in ["L"]:
        low = c
        c = int((c + high) / 2)
        compscore = compscore + 1
    else:
        print ("invalid input")

Here you check if d is H (higher) and you calculate like it is lower ((50 + 0) /2) = 25 So you need to switch that

Also you forgot one more = in while ans = False:

Upvotes: 0

DAXaholic
DAXaholic

Reputation: 35328

while ans = False:

should be

while ans == False:

Upvotes: 0

Related Questions