我从一本名为Invent With Python的书中得到了这个猜谜游戏的想法。我不喜欢原始剧本没有涵盖重新猜测数字或错误地使用不在1 - 20的数字的可能性,所以我修改了它。该程序运行良好,但是,如果/ elif / else代码块,我只是围着我的脑袋。
我想重写脚本而不必嵌套,如果在if中。我甚至无法开始关注如何做到这一点。任何人都可以帮助我 - 只是这个程序如何在没有嵌套的情况下工作的一个例子会很棒!
这是完整的小脚本:
from random import randint
from sys import exit
name = raw_input("Hello! What's your name? ")
print "Well %s, I'm thinking of a number between 1 and 20." % name
print "Since I'm a benevolent computer program, I'll give you 6 guesses."
secret_number = randint(1, 20)
guesses_left = 6
already_guessed = []
while guesses_left > 0:
try:
guess = int(raw_input("Take a guess: "))
if guess >= 1 and guess <= 20 and guess not in already_guessed:
already_guessed.append(guess)
guesses_left -= 1
if guess == secret_number:
print "You win! %d was my secret number!" % secret_number
exit(0)
elif guess < secret_number:
print "Your guess is too low!"
elif guess > secret_number:
print "Your guess is too high!"
elif guess in already_guessed:
print "You already guessed that!"
else:
print "Not a number between 1 - 20!"
print "Please try again!"
print "You have %d guesses left!" % guesses_left
except ValueError:
print "Invalid input! Please try again!"
答案 0 :(得分:5)
尝试这样,使用continue
退出循环的当前迭代,然后在循环的顶部重新开始。
你这里也有一个逻辑错误:
if guess <= 1 and guess >= 20 and guess not in already_guessed:
数字不可能小于或等于1,大于或等于20.您的and
应该是or
,如下所示:
if (guess <= 1 or guess >= 20) and guess not in already_guessed:
或更简单:
if 1 <= guess <= 20 and guess not in already_guessed:
另外,只保留try/except
可以实际引发异常的事情(或者如果发生异常则不应该发生:
from random import randint
import sys
name = raw_input("Hello! What's your name? ")
print "Well {}, I'm thinking of a number between 1 and 20.".format(name)
print "Since I'm a benevolent computer program, I'll give you 6 guesses."
secret_number = randint(1, 20)
guesses_left = 6
already_guessed = []
while guesses_left > 0:
print "You have {} guesses left!".format(guesses_left)
try:
guess = int(raw_input("Take a guess: "))
except ValueError:
print "Invalid input! Please try again!\n"
continue
# If the number is not between 1 and 20...
if not (1 <= guess <= 20):
print "Not a number between 1 - 20!"
print "Please try again!\n"
continue
if guess in already_guessed:
print "You already guessed that!\n"
continue
guesses_left -= 1
already_guessed.append(guess)
if guess == secret_number:
print "You win! {} was my secret number!".format(secret_number)
sys.exit(0)
elif guess < secret_number:
print "Your guess is too low!\n"
elif guess > secret_number:
print "Your guess is too high!\n"
以下是一个示例运行:
Hello! What's your name? :)
Well :), I'm thinking of a number between 1 and 20.
Since I'm a benevolent computer program, I'll give you 6 guesses.
You have 6 guesses left!
Take a guess: 2
Your guess is too low!
You have 5 guesses left!
Take a guess: 2
You already guessed that!
You have 5 guesses left!
Take a guess: 3
Your guess is too low!
You have 4 guesses left!
Take a guess: 7
Your guess is too high!
You have 3 guesses left!
Take a guess: 5
Your guess is too high!
You have 2 guesses left!
Take a guess: 4
You win! 4 was my secret number!
答案 1 :(得分:1)
只需将嵌套的if语句更改为elif,如下所示:
from random import randint
from sys import exit
name = raw_input("Hello! What's your name? ")
print "Well %s, I'm thinking of a number between 1 and 20." % name
print "Since I'm a benevolent computer program, I'll give you 6 guesses."
secret_number = randint(1, 20)
guesses_left = 6
already_guessed = []
while guesses_left > 0:
try:
guess = int(raw_input("Take a guess: "))
if guess <= 1 and guess >= 20 and guess not in already_guessed:
already_guessed.append(guess)
guesses_left -= 1
elif guess == secret_number:
print "You win! %d was my secret number!" % secret_number
exit(0)
elif guess < secret_number:
print "Your guess is too low!"
elif guess > secret_number:
print "Your guess is too high!"
elif guess in already_guessed:
print "You already guessed that!"
else:
print "Not a number between 1 - 20!"
print "Please try again!"
print "You have %d guesses left!" % guesses_left
except ValueError:
print "Invalid input! Please try again!"
这是我看到解决你的困境的最简单方法