绝对初学者的Python编程

时间:2010-12-27 23:26:49

标签: python python-2.x

我是一个python noob,我正在通过'绝对初学者的Python编程(第2版 - Python 2.3,但我使用2.7)'来掌握python。

这本书提出了要完成的挑战,而我无法绕过其中一个;我会非常感激任何帮助,因为在我继续前进之前我想先了解一下。

第3章,挑战3 - 猜猜我的号码:修改下面的代码以限制玩家猜测该号码的尝试次数。

我该怎么做呢?到目前为止我为设置一个变量所做的尝试都已经结束,答案显示用户是否得到正确答案。先谢谢你们。

  

猜猜我的号码

     

计算机选择1到100之间的随机数>玩家试图猜测它   计算机让玩家知道猜测是否太高,太低或对钱

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n"

# set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

# guessing loop
while (guess != the_number):
    if (guess > the_number):
        print "Lower..."
    else:
        print "Higher..."

guess = int(raw_input("Take a guess: "))
tries += 1

print "You guessed it!  The number was", the_number
print "And it only took you", tries, "tries!\n"

raw_input("\n\nPress the enter key to exit.")

到目前为止,我已尝试以下方法失败。

import random  

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n"

# set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
limit = 8

# guessing loop
while (guess != the_number and tries < limit):
    if (guess > the_number):
        print "Lower..."
    elif (guess < the_number):
        print "Higher..."
    else:
        print "You've used all " + limit -1 +"of your attempts \
and didn't get the right answer. Shame on You!"

    guess = int(raw_input("Take a guess: "))
    tries += 1

print "You guessed it!  The number was", the_number
print "And it only took you", tries, "tries!\n"

raw_input("\n\nPress the enter key to exit.")

6 个答案:

答案 0 :(得分:2)

你有一个尝试变量。如何检查内部,如果它达到一定值,你打印一条消息给用户并退出;)

答案 1 :(得分:1)

我认为这个练习想要教你的是break陈述。在您只有一个退出条件(猜测数字)之前,但现在您的尝试次数也有限。

直接这样做:

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n"

# set the initial values
the_number = random.randrange(100) + 1

limit = 5
tries = 0

# guessing loop
while True: # we will test the conditions separately in the loop, not here
    # take a guess
    guess = int(raw_input("Take a guess: "))
    tries += 1 

    # first check the number
    if (guess > the_number):
        print "Lower..."
    elif (guess < the_number):
        print "Higher..."
    else: # it can only be equal here
        print "You guessed it!  The number was", the_number
        print "And it only took you", tries, "tries!\n"
        break # exit the while loop

    # now the tries:
    if tries == limit:
        print "You've used all %d of your attempts \
and didn't get the right answer. Shame on You!" % limit
        break

raw_input("\n\nPress the enter key to exit.")

答案 2 :(得分:1)

我只是从编程和Python开始,使用同一本书。这是我提出的,它似乎工作!

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random  

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))

tries = 1
max_tries = 8

# guessing loop
while tries < max_tries and guess != the_number:

    if guess > the_number:
        print("Lower...")
    elif guess < the_number:
        print("Higher...")

    guess = int(input("Take a guess: "))
    tries += 1

# having it stop if person uses maximum number of tries
if tries >= max_tries:

    print("Ruh roh.  Looks like you're finished, pardner. The number was", the_number)

# text if person gets the right number within the number of tries.
else:
    if guess == the_number:
        print("You guessed it!  The number was", the_number)
        print("And it only took you", tries, "tries!\n")

input("\n\nPress the enter key to exit.")

答案 3 :(得分:0)

这样的事情:

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n"

# set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
max_tries = 5

# guessing loop
while ((tries < max_tries) && (guess != the_number)):
    if (guess > the_number):
        print "Lower..."
    else:
        print "Higher..."
    tries += 1
    guess = int(raw_input("Take a guess: "))

if tries == max_tries:
    print "Max # of tries exceeded"
else:
    print "You guessed it!  The number was", the_number
    print "And it only took you", tries, "tries!\n"

raw_input("\n\nPress the enter key to exit.")

答案 4 :(得分:0)

猜测没有循环,这意味着猜测只发生一次。您需要将整个猜测和测试结构放在某种循环中(forwhile)。

答案 5 :(得分:0)

你的尝试几乎正确“。我可以看到两个问题。第一个就在这里:

print "You guessed it!  The number was", the_number
print "And it only took you", tries, "tries!\n"

考虑如果最大尝试次数超出 ,您将如何执行上述操作。提示:您需要添加一行并对上面的两行进行微调。

第二个就在这里:

    else:
        print "You've used all " + limit -1 +"of your attempts \
and didn't get the right answer. Shame on You!"

else块会执行吗?回顾while条件,了解原因。同样,有一种简单的方法可以解决这个问题。