python中的累加器

时间:2013-01-09 01:58:28

标签: python while-loop accumulator

我必须创建一个累加器来计算用户猜测随机数的条目数。我已经找到了所有的声明,但我无法得到计算它花了多少条目的作品。谢谢你的帮助!

import random
secretNumber = random.randint(1,100)
secretNumber = int(secretNumber)
print("Guess a number between 1 and 100!")
number = input("Your guess: ")
number = int(number)
tries = 1
while number != secretNumber:
    if number > secretNumber:
        print("Too high!")
        number = input("Your guess: ")
        number = int(number)
    if number < secretNumber:
        print("Too low!")
        number = input("Your guess: ")
        number = int(number)
while number == secretNumber:
    print("You got it in",tries,"tries")
    break

我需要帮助的部分是在中断后实现尝试累加器

5 个答案:

答案 0 :(得分:3)

你要问的第一件事就是当你print tries时,你得到了什么? 实际上,你会发现你总是得到1。

因为,你没有增加。您没有添加,因为用户一直在猜测。

通常,您可以为每次猜测编写tries = tries + 1

tries = 1
while number != secretNumber:
    if number > secretNumber:
        print("Too high!")
        tries = tries + 1      # here is the addition
        number = input("Your guess: ")
        number = int(number)
    if number < secretNumber:
        print("Too low!")
        tries = tries + 1      # here is the addition
        number = input("Your guess: ")
        number = int(number)
while number == secretNumber:
    print("You got it in",tries,"tries")
    break

此代码仍有一些问题。第二个循环没有意义。循环听起来像循环。它一直运行,直到满足条件或某人(你)中断它并告诉它退出。

如果用户找到了该号码,那么while number !- secretNumber将成为False对吗? 它将退出第一个循环。因此,您可以跳过第二个循环并祝贺用户。

另一个小问题是双if语句。  if语句很昂贵。计算机必须测试才能猜对。但无论哪种方式,都有不同的方式来做多个条件。

if condition1 met:
    do this
elif condition2 met:
    do this
elif condition3 met:
    do this
elif more....
else:   # optional, but encourage, this is a default fallback case
    do this

如果number > secretNumberTrue,那么您无需在理论上测试number < secretNumber。它通过调整if .. elif .. else

使代码更清晰,逻辑更合理
import random
secretNumber = random.randint(1,100)
secretNumber = int(secretNumber)
print("Guess a number between 1 and 100!")
number = input("Your guess: ")
number = int(number)
tries = 1
while number != secretNumber:
    if number > secretNumber:
        print("Too high!")
        tries = tries + 1      # here is the addition
        number = input("Your guess: ")
        number = int(number)
    elif number < secretNumber:
        print("Too low!")
        tries = tries + 1      # here is the addition
        number = input("Your guess: ")
        number = int(number)
print("You got it in",tries,"tries")
# another way to print is
# print("You got it in %s tries" % tries)
# print("You got it in {t} tries".format(t=tries))

对于初学者,请使用print来帮助调试代码。

答案 1 :(得分:1)

只需放行

tries += 1

在循环中 - 此行将tries变量增加1。

我还可以通过从if语句中删除部分内容来缩短它,并删除第二个while循环(因为循环总是在没有理由放置循环的情况下发生一次):

while number != secretNumber:
    tries += 1
    if number > secretNumber:
        print("Too high!")
    if number < secretNumber:
        print("Too low!")
    number = input("Your guess: ")
    number = int(number)
print("You got it in",tries,"tries")

答案 2 :(得分:0)

import random
i = 0
rand_num = random.randint(1, 100)
while True:
    i += 1
    try:
        guess = int(input('Guess the number: ')
    except ValueError:
        print('Invalid input, try again')
        continue
    if guess < rand_num:
        print('Too low, try again')
    elif guess > rand_num:
        print('Too high, try again')
    else:
        print('You got it in ', tries, ' tries!')
        break

答案 3 :(得分:0)

如果你弄错了,你可以添加尝试次数:

...
while number != secretNumber:
    tries += 1
    if number > secretNumber:
        print("Too high!")
...

此外,最后而不是:

while number == secretNumber:
    print("You got it in",tries,"tries")
    break

你可以使用它:

print("You got it in",tries,"tries")

因为如果你得到正确的数字,它只会到达这一点。

另一件事,尝试最初应该等于0,没有人,因为在开始时你尝试了0次,而不是1次。

答案 4 :(得分:-1)

您想在while循环中添加额外的行tries += 1。这样做是为每次猜测添加1。那么你的代码就是:

import random
secretNumber = random.randint(1,100)
secretNumber = int(secretNumber)
print("Guess a number between 1 and 100!")
number = input("Your guess: ")
number = int(number)
tries = 1
while number != secretNumber:
    if number > secretNumber:
        print("Too high!")
        number = input("Your guess: ")
    number = int(number)
    if number < secretNumber:
        print("Too low!")
        number = input("Your guess: ")
        number = int(number)
while number == secretNumber:
    print("You got it in",tries,"tries")
    break