Python Forum
New to python and need help
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to python and need help
#1
Hi, I am very new to python and programming, and I have to create an artificial intelligence that will try to guess a number that the user thinks of. I will reply with either higher or lower to tell it what to guess next. There also needs to be a way for it to check if the user's statements are contradicting themselves.
I have attempted it and gotten close. (Python 3.6.0 Btw)

from random import randint
again = 1
while int(again) == 1:
      print("Think of a number between 1 and 5 for the computer to guess") 
      guess = (randint(1,5))
      win = 0
      low = 1
      high = 5
      while (win < 1):
            print("The computer guessed " + str(guess))
            response = input("Is your number higher, lower or was that correct? (Put 1 for higher, 2 for lower and 3 for correct) ")
            print("  ")
            guess = int(guess)
            response = int(response)
            if low > high:
                win = win + 1
                again = input("You either cheated, or were incorrect with your clues. Do you want to play again? (1 for yes, 0 for no) ")
            else:
                guess = (randint(low ,high))
                if response == 1:
                    low = int(guess) + 1
                elif response == 2:
                    high = int(guess) - 1
                else:
                    win = win + 1
                    again = input("It guessed it! Do you want to play again? (1 for yes, 0 for no) ")
It doesn't use the "higher, lower" clues and incorectly says that the statements contradict.
Reply
#2
There's two problems that I see. One, you are only making one guess each game. You need to move guess = randint(1,5) into the inner while loop (note that the parentheses I removed from that line were not necessary). Then, as you said, it's not taking into account the information about low and high. That's because you're not using the low and high variables when you make the guess. It should really be guess = randint(low, high).

Also, guess is an integer, it is never anything but an integer. So you don't need to constantly turning it into an integer with int().
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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