Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

random - Computer guessing number python

I'm very new to programming and am starting off with python. I was tasked to create a random number guessing game. The idea is to have the computer guesses the user's input number. Though I'm having a bit of trouble getting the program to recognize that it has found the number. Here's my code and if you can help that'd be great! The program right now is only printing random numbers and won't stop even if the right number is printed that is the problem

import random
tries = 1
guessNum = random.randint(1, 100)
realNum = int(input("Input a number from 1 to 100 for the computer to guess: "))
print("Is the number " + str(guessNum) + "?")
answer = input("Type yes, or no: ")
answerLower = answer.lower()

if answerLower == 'yes':
        if guessNum == realNum:
            print("Seems like I got it in " + str(tries) + " try!")
        else:
            print("Wait I got it wrong though, I guessed " + str(guessNum) + " and your number was " + str(realNum) + ", so that means I'm acutally wrong." )
else:
    print("Is the number higher or lower than " + str(guessNum))
    lowOr = input("Type in lower or higher: ")
    lowOrlower = lowOr.lower()
    import random 
    guessNum2 = random.randint(guessNum, 100)
    import random 
    guessNum3 = random.randint(1, guessNum)
    while realNum != guessNum2 or guessNum3:
        if lowOr == 'higher':
            tries += 1
            import random 
            guessNum2 = random.randint(guessNum, 100)
            print(str(guessNum2))
            input()
        else:
            tries += 1
            import random 
            guessNum3 = random.randint(1, guessNum)
            print(str(guessNum3))
            input()
    print("I got it!")
      
           
       
       








input()
question from:https://stackoverflow.com/questions/65602244/computer-guessing-number-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about something along the lines of:

import random
realnum = int(input('PICK PROMPT
'))
narrowguess = random.randint(1,100)
if narrowguess == realnum:
  print('CORRECT')
  exit(1)
print(narrowguess)
highorlow = input('Higher or Lower Prompt
')
if highorlow == 'higher':
  while True:
    try:
        guess = random.randint(narrowguess,100)
        print(guess)
        while realnum != guess:
          guess = random.randint(narrowguess,100)
          print(guess)
          input()
        print(guess)
        print('Got It!')
        break
    except:
      raise
elif highorlow == 'lower':
  while True:
    try:
        guess = random.randint(1,narrowguess)
        print(guess)
        while realnum != guess:
          guess = random.randint(1,narrowguess)
          print(guess)
          input()
        print(guess)
        print('Got It!')
        break
    except:
      raise

This code is just a skeleton, add all of your details to it however you like.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...