简体   繁体   English

反向猜数游戏 Python

[英]Reverse Number Guessing Game Python

So, recently i have been trying to program a reverse number guessing game whereby the computer tries to guess the number i have in mind.所以,最近我一直在尝试编写一个反向猜数游戏,让计算机尝试猜测我心中的数字。 The output i should get is as shown below:我应该得到的 output 如下所示:

Enter the range: 6
Think of a random number between 1 and 6 and press enter once done!
Is it smaller than 4, 'y' or 'n'?y
Is it smaller than 2, 'y' or 'n'?n
Is it smaller than 3, 'y' or 'n'?y
Wonderful it took me 3 questions to find out that you had the number 2 in mind!

These are my codes so far:到目前为止,这些是我的代码:

import random

maxNum = int(input('Enter the range: '))
input('Think of a random number between 1 and ' + str(maxNum) + ' and press enter once done!')
lowBound = 1
highBound = maxNum
response = ''
noOfGuesses = 0
numberHasBeenGuessed = False
randomNumber = random.randint(lowBound,highBound)

while not numberHasBeenGuessed:
    noOfGuesses += 1
    response = input("Is it smaller than " + str(randomNumber) + ", 'y' or 'n'?")
    if response == "n" or response == 'N':
        lowBound = randomNumber + 1   
        randomNumber = random.randint(lowBound,highBound)
    elif response == "y" or response == "Y":
        highBound = randomNumber - 1
        randomNumber = random.randint(lowBound,highBound)
    else:
        print ('Please only type y, Y, n or N as responses')
        
numberHasBeenGuessed = True        
print('Wonderful it took me ' + str(noOfGuesses) + ' attempts to guess that you had the number ' + str(randomNumber) + ' in mind')

The main algorithm is working but somehow it cant detect it when the number has been 'guessed'.. does anyone know why?主要算法正在运行,但不知何故,当数字被“猜到”时,它无法检测到它。有人知道为什么吗?

I will greatly appreciate the help:)我将不胜感激帮助:)

You have numberHasBeenGuessed = True outside of the while loop - which means that it can't get called until the loop is over - so it will be forever stuck.您在 while 循环之外有numberHasBeenGuessed = True - 这意味着在循环结束之前无法调用它 - 所以它将永远卡住。

When it gets to your guess - lets say I have the number 7. Your program asks 'Is 7 smaller or larger than 7?'当你猜测时 - 假设我有数字 7。你的程序会问“7 是小于还是大于 7?” Obviously that makes no sense, 7 is 7. 7 is not smaller or larger than 7: and your program knows that and crashes.显然这没有意义,77。7 不小于或大于 7:您的程序知道这一点并崩溃。 So you need to introduce a new user input option:所以你需要引入一个新的用户输入选项:

    import random
    
    maxNum = int(input('Enter the range: '))
    input('Think of a random number between 1 and ' + str(maxNum) + ' and press enter once done!')
    lowBound = 1
    highBound = maxNum
    response = ''
    noOfGuesses = 0
    numberHasBeenGuessed = False
    randomNumber = random.randint(lowBound,highBound)
    
    while not numberHasBeenGuessed:
        noOfGuesses += 1
        response = input("Is it smaller than " + str(randomNumber) + ", 'y', 'n', or 'Thats it'?")
        if response == "n" or response == 'N':
            lowBound = randomNumber + 1   
            randomNumber = random.randint(lowBound,highBound)
            print(randomNumber)
        elif response == "y" or response == "Y":
            highBound = randomNumber - 1
            randomNumber = random.randint(lowBound,highBound)
            print(randomNumber)
        elif response == "Thats it": #New input option 'Thats it'
            numberHasBeenGuessed = True #Stops the while loop
        else:
            print ('Please only type y, Y, n or N as responses')
               
    print('Wonderful it took me ' + str(noOfGuesses) + ' attempts to guess that you had the number ' + str(randomNumber) + ' in mind')

Now, you can input 'Thats it' when your number is guessed and the program finishes with the output you want.现在,当您猜到您的号码并且程序以您想要的 output 结束时,您可以输入“就是这样”。 (Of course, change the inputs and what not to what you want) (当然,改变输入和什么不是你想要的)

Also final programmer tip: comment your code with hashtags, so you know (and others helping) what each part does.还有最后的程序员提示:用标签评论你的代码,这样你就知道(和其他帮助)每个部分的作用。

Here is a version using try/except这是一个使用try/except的版本

import random

maxNum = int(input('Enter the range: '))
input('Think of a random number between 1 and ' + str(maxNum) + ' and press enter once done!')
lowBound = 1
highBound = maxNum
response = ''
noOfGuesses = 0
numberHasBeenGuessed = False
randomNumber = random.randint(lowBound,highBound)

while not numberHasBeenGuessed:
        noOfGuesses += 1
        response = input("Is it smaller than " + str(randomNumber) + ", 'y', 'n', or 'Thats it'?")
        if response == "n" or response == 'N':
            lowBound = randomNumber + 1
            
            try:
                randomNumber = random.randint(lowBound,highBound)
            except ValueError:
                numberHasBeenGuessed = True
        elif response == "y" or response == "Y":
            highBound = randomNumber - 1

            try:
                randomNumber = random.randint(lowBound,highBound)
            except ValueError:
                numberHasBeenGuessed = True
        else:
            print ('Please only type y, Y, n or N as responses')
        
           
print('Wonderful it took me ' + str(noOfGuesses) + ' attempts to guess that you had the number ' + str(randomNumber) + ' in mind')

When it encounters a ValueError , it will take it that it has found your number.当它遇到ValueError时,它会认为它找到了你的号码。 Please make sure there are no other situations that can result in an incorrect answer with this code, as I obviously haven't tested it thoroughly.请确保没有其他情况会导致此代码的答案不正确,因为我显然没有对其进行彻底测试。

You could also put the try/except clauses in a def to make it more compact as they are the same pieces of code, but I wasn't sure if that was allowed or not for your project, so I left it.您也可以将try/except子句放在def中以使其更紧凑,因为它们是相同的代码片段,但我不确定您的项目是否允许这样做,所以我保留了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM