如何计算Python中的用户输入数量

时间:2018-06-25 17:53:38

标签: python count

找到数字后,我很难负担要计算的猜测数。

#nav-list {

display: flex;
align-items: center; 

} 

4 个答案:

答案 0 :(得分:0)

尝试一下:

def play_game():
    print("Enter the upper limit for the range of numbers: ")
    limit = int(input())
    number = random.randint(1, limit)
    print("I'm thinking of a number from 1 to " + str(limit) + "\n")
    count = 1 #new line
    while True:
        guess = int(input("Your guess: "))
        if guess < number:
            print("Too low.")
        elif guess > number:
            print("Too high.")
        elif guess == number:
            print("You guessed it in " + str(count) + " tries.\n")
        count = count+1 #new line

答案 1 :(得分:0)

从变量开始存储猜测的数目

...
count = 0
...

然后在每次猜测时将其递增

...
guess = int(input("Your guess: "))
count += 1
...

答案 2 :(得分:0)

您应将count的{​​{1}}初始化为1,并将increment初始化为loop

import random
def play_game():

    print("Enter the upper limit for the range of numbers: ")
    limit = int(input())
    number = random.randint(1, limit)
    print("I'm thinking of a number from 1 to " + str(limit) + "\n")
    count = 1
    while True:
        guess = int(input("Your guess: "))
        if guess < number:
            print("Too low.")
        elif guess > number:
            print("Too high.")
        elif guess == number:
            print("You guessed it in " + str(count) + " tries.\n")
            return
        count+=1
play_game()

示例输出:

Enter the upper limit for the range of numbers: 
10
I'm thinking of a number from 1 to 10

Your guess: 3
Too low.
Your guess: 7
Too low.
Your guess: 9
Too low.
Your guess: 10
You guessed it in 4 tries.

答案 3 :(得分:0)

import random
highest = 10

answer = random.randrange(1,highest)
guess = 0
count = 0

print("please guess a number between 1 and {}".format(highest))

while guess != answer:
    guess = int(input())
    if count == 4:
        exit(print("you exceeded number of chances"))
    if guess == answer:
        print("well done you have guessed it correctly and the answer is 
                {}".format(guess))
        break
    else:
        if guess < answer:
            print("please guess higher")
        else:
            print("please guess lower")
        count = count +1