简体   繁体   English

猜数字游戏Python

[英]Guess the number game Python

So, im new to python and i have this challenge: I have to make the Guess the Number game, between me and computer.所以,我是 python 的新手,我遇到了这个挑战:我必须在我和计算机之间进行猜数字游戏。 So this is where im at so far.所以这就是我目前所处的位置。

import random


the_number = random.randint(1, 4)
guess = 0

print(the_number)
while guess != the_number:
    guess = int(input("Please enter a number: "))
    if guess > the_number:
        print("Player guess lower...\n")
    elif guess < the_number:
        print("Player guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Player wins!")
        break


    guess = random.randint(1, 100)

    if guess > the_number:
        print("Computer guess lower...\n")
    elif guess < the_number:
        print("Computer guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Computer wins!")
    break


print("Thank you for playing")

I wanna know how to make this to not stop until one of us is right?我想知道如何让这在我们中的一个人正确之前不会停止?

# Import random library
import random
# Assign random value between 1-100
secret_number = random.randint(1, 100)
# get the user guess
guess = int(input("Guess the secret number between 1-100: "))

# Loop untill guess not equal to secret number
while guess != secret_number:
    if guess < secret_number:
        print("Sorry, your guess is too low.")
    else:
        print("Sorry, your guess is too high.")
    
    # guess the number again
    guess = int(input("Guess the secret number between 1-100: "))
 
# This statement will execute after coming out of while loop
print("You guessed the number!")

You can do something like this to make the computer smarter.你可以做这样的事情来让电脑更智能。

import random

the_number = random.randint(1, 4)
guess = 0

print(the_number)
minPossible = 0
maxPossible = 100

while guess != the_number:
    guess = int(input("Please enter a number: "))
    if guess > the_number:
        print("Player, guess lower...\n")
        if guess < maxPossible:
            maxPossible = guess - 1
    elif guess < the_number:
        print("Player, guess higher...\n")
        if guess > minPossible:
            minPossible = guess + 1
    else:
        print("Game Over! The number was", the_number, "The Player wins!")
        break

    guess = random.randint(minPossible, maxPossible)

    if guess > the_number:
        print("Computer, guess lower...\n")
        maxPossible = guess - 1
    elif guess < the_number:
        print("Computer, guess higher...\n")
        minPossible = guess + 1
    else:
        print("Game Over! The number was", the_number,"The Computer wins!")


print("Thank you for playing")

Your problem is the break statement at the end of the while loop.您的问题是 while 循环结束时的break语句。 The code is iterating through the loop one time, then the break is ending the loop.代码在循环中迭代一次,然后break结束循环。

To get the desired output, you just need to tweak the code a bit, the break in the if statement for the computer is out of the loop of else, so it will stop the execution no matter what.为了得到想要的 output,你只需要稍微调整一下代码,计算机的 if 语句中的break不在 else 的循环中,所以无论如何它都会停止执行。

To counter this condition, you can move the break inside the else block.为了应对这种情况,您可以将break移动到else块内。 By doing this, the program will only break if either CPU or you correctly guess the right number.通过这样做,程序只会在 CPU 或您正确猜对了数字时才会中断。

One more thing i wanted to point out is you are printing the selecting number in the program, if it is a guessing game, isn't it supposed to be in the back-end of the code.我想指出的另一件事是您正在程序中打印选择的数字,如果它是一个猜谜游戏,它不应该在代码的后端。 By entering the printed number, you can win the game in the first iteration.通过输入打印的数字,您可以在第一次迭代中赢得比赛。

And just to point out, your program is selecting the number from (1, 4) but the computer is set to guess the number from (1, 100) .需要指出的是,您的程序正在从(1, 4)中选择数字,但计算机设置为从(1, 100)中猜测数字。 Kindly tick the answer if it is correct.如果正确,请在答案上打勾。 The modified code is -修改后的代码是 -

import random


the_number = random.randint(1, 4)
guess = 0

while guess != the_number:
    guess = int(input("Please enter a number: "))
    if guess > the_number:
        print("Player guess lower...\n")
    elif guess < the_number:
        print("Player guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Player wins!")
        break

    guess = random.randint(1, 4)

    if guess > the_number:
        print("Computer guess lower...\n")
    elif guess < the_number:
        print("Computer guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Computer wins!")
        break


print("Thank you for playing")

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

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