随机INT并不在我while循环加起来

问题描述 投票:-1回答:2

减去或在这个循环将不工作

试图改变成回报,但它没有为我工作。我是个菜鸟...

from random import randint
import time

def Guess2(n):

    randomNo=randint(0,n)
    print("This time you will choose a number, and the Computer will attempt to guess your number!")
    print("Hello, what is your name:")
    myName=str(input())


    print("Alright "+myName+", "+"I will start to guess your number now!"+"\nchoose it, I will close my eyes.")
    number=int(input())
    time.sleep(2)
    print("Okay, I will open my eyes now!")
    time.sleep(2)
    print("Can I start guessing now? Just answer with 'no' , 'yes'")
    Answer=str(input())
    if Answer[0:]=="no":
        time.sleep(1)
        print("all this for nothing.. bye")
    if Answer[0:]=="yes":
        time.sleep(1)
        print("Alright let's go!")
        time.sleep(1)

    print("my first guess is: ",randomNo)

    while number !=randomNo:
        Userinput=input()
        if Userinput=="too big":
            print("okay, ",randomNo-1)
        if Userinput=="too small":
            print("okay, ",randomNo+1)
        if Userinput=="richtig":
            print("I win!")

应该+1或-1的结果与以前积少成多。也许你可以给我一些建议藏汉怎么去数猜到快:)

python
2个回答
0
投票

这是完善的版本,但有区算法。试试吧。

import time
from random import randint

def Guess2():
    toosmall = []
    toobig = []
    print("This time you will choose a number, and the Computer will attempt to guess your number!")
    print("Hello, what is your name:")
    myName=str(input())


    print("Alright "+myName+", "+"I will start to guess your number now!"+"\nchoose it, I will close my eyes.")
    number = int(input())
    time.sleep(2)
    print("Okay, I will open my eyes now!")
    time.sleep(2)
    print("Can I start guessing now? Just answer with 'no' , 'yes'")
    Answer=str(input())
    if Answer[0:]=="no":
        time.sleep(1)
        print("all this for nothing.. bye")
    if Answer[0:]=="yes":
        time.sleep(1)
        print("Alright let's go!")
        time.sleep(1)
    if number > 0:
        randomNo = randint(-1 * number, number + int(number/2))
    elif number > -14 and number < 0:
        randomNo = randint(0, number + (-2 * number))
    else:
        randomNo = randint(number - int(number/2), -1 * number)
    print("my first guess is: ",randomNo)

    while number !=randomNo:
        Userinput=input()
        toobig.append(number + 10000)
        toosmall.append(number - 10000)
        if min(toobig) - 2 == max(toosmall):
            print(f"Your number is {max(toosmall) + 1}")
            exit()
        else:
            if Userinput=="too big":
                toobig.append(randomNo)
                randomNo = randomNo - randint(1, int(abs(number/2)))
                if randomNo <= max(toosmall):
                    randomNo = max(toosmall) + 2
                    print("okay, ",  randomNo)
                else:
                    print("okay, ", randomNo)
                if min(toobig) - 2 == max(toosmall):
                    print(f"Your number is {max(toosmall) + 1}")
                    exit()        
            elif Userinput=="too small":
                toosmall.append(randomNo)
                randomNo = randomNo + randint(1, int(abs(number/2)))
                if randomNo >= min(toobig):
                    randomNo = min(toobig) - 2            
                    print("okay, ", randomNo)
                else:
                    print("okay, ", randomNo)
                if min(toobig) - 2 == max(toosmall):
                    print(f"Your number is {max(toosmall) + 1}")
                    exit()            
            elif Userinput=="richtig":
                print("I win!")

Guess2()

0
投票

NVM现在它工作。我重写了我的randomNo每个循环之后,现在它终于每次需要新的RandomNo现在。从随机导入randint进口时间

DEF Guess2(N):

randomNo=randint(0,n)
print("This time you will choose a number, and the Computer will attempt to guess your number!")
print("Hello, what is your name:")
myName=str(input())


print("Alright "+myName+", "+"I will start to guess your number now!"+"\nchoose it, I will close my eyes.")
number=int(input())
time.sleep(2)
print("Okay, I will open my eyes now!")
time.sleep(2)
print("Can I start guessing now? Just answer with 'no' , 'yes'")
Answer=str(input())
if Answer[0:]=="no":
    time.sleep(1)
    print("all this for nothing.. bye")
if Answer[0:]=="yes":
    time.sleep(1)
    print("Alright let's go!")
    time.sleep(1)

print("my first guess is: ",randomNo)

while number!=randomNo:
    Userinput=input()

    if Userinput=="too big":
        x1=randomNo-1
        print(x1)
        randomNo=x1

    if Userinput=="too small":
        x1=randomNo+1
        print(x1)
        randomNo=x1

    if Userinput=="richtig":
        print("I win!")
© www.soinside.com 2019 - 2024. All rights reserved.