随机int不会在我的while循环中加起来

时间:2019-02-02 23:33:48

标签: python

在此循环中减法或加法不起作用

试图改变成回报,但对我没有用。我是菜鸟...

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。也许您也可以给我一些建议,如何使数字更快地被猜出:)

2 个答案:

答案 0 :(得分:0)

nvm现在可以工作了。我在每次循环后都重写了我的randomNo,现在终于每次都使用新的RandomNo。 来自随机进口randint 导入时间

def Guess2(n):

PHP

答案 1 :(得分: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()