绝对初学者的Python编程
我刚开始学Python,正在通过《绝对初学者的Python编程(第二版 - Python 2.3,但我用的是2.7)》这本书来入门。
书里有一些挑战让我完成,但我在其中一个挑战上遇到了困难;如果有人能帮帮我,我会非常感激,因为我想在继续之前先搞明白这个。
第三章,挑战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 个回答
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.")
2
你这里有一个叫做 tries 的变量。你可以在 while 循环里面检查一下这个变量,如果它达到了某个特定的值,就给用户打印一条消息,然后退出程序;)
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.")