if
-statements,但是不确定如何更改代码段以减少使用。import random
def playchosengame(chosengame):
if chosengame == 1:
doorchoice = int(raw_input("How many doors would you like to play with? > "))
onegameatatime(doorchoice)
elif chosengame == 2:
numberofgames = int(raw_input("How many games would you like to play? > "))
strategy = int(raw_input("Would you like to always change your answer (1) or keep it the same (2)? > "))
doorchoice = int(raw_input("How many doors would you like to play with? > "))
onestrategy(numberofgames,strategy,doorchoice)
else:
print "What did you say?"
def onestrategy(chosennumberofgames,chosenstrategy,doorchoice):
# for playing x number of Monty Hall games with one type of strategy
wincount = 0
i = 1
for i in range(1,chosennumberofgames + 1):
possibleanswers = range(1,doorchoice + 1)
correctanswer = random.randint(1,doorchoice)
incorrectpossibilities = possibleanswers
incorrectpossibilities.remove(correctanswer)
youranswer = random.randint(1,doorchoice)
if youranswer == correctanswer:
otherremaining = random.choice(incorrectpossibilities)
else:
otherremaining = youranswer
incorrectpossibilities.remove(otherremaining)
print "The correct answer is NOT one of these: %r" % incorrectpossibilities
if youranswer == correctanswer:
print "Which means it is either your answer: %r, or the only other remaining option, %r." % (correctanswer, otherremaining)
if chosenstrategy == 1:
finalanswer = otherremaining
else:
finalanswer = correctanswer
else:
print "Which means it is either your answer: %r, or the only other remaining option, %r." % (youranswer, correctanswer)
if chosenstrategy == 1:
finalanswer = correctanswer
else:
finalanswer = youranswer
print "You chose %r" % finalanswer
if finalanswer == correctanswer:
wincount += 1
print "You win!"
else:
print "You lose!"
i += 1
print "You won %r out of %r games. Congrats!" % (wincount, i - 1)
def onegameatatime(doorchoice):
# for playing one Monty Hall game at a time.
playagain = 1
while playagain == 1:
possibleanswers = range(1,doorchoice + 1)
correctanswer = random.randint(1,doorchoice)
incorrectpossibilities = possibleanswers
incorrectpossibilities.remove(correctanswer)
youranswer = int(raw_input("Pick a door number from 1 - %r > " % doorchoice))
if youranswer == correctanswer:
otherremaining = random.choice(incorrectpossibilities)
else:
otherremaining = youranswer
incorrectpossibilities.remove(otherremaining)
print "The correct answer is NOT one of these: %r" % incorrectpossibilities
if youranswer == correctanswer:
print "Which means it is either your answer: %r, or the only other remaining option, %r." % (correctanswer, otherremaining)
else:
print "Which means it is either your answer: %r, or the only other remaining option, %r." % (youranswer, correctanswer)
finalanswer = int(raw_input("Which do you choose? > "))
if finalanswer == correctanswer:
print "You win!"
else:
print "You lose!"
playagain = int(raw_input("Play again? (1 = yes, 0 = no) > "))
print "Thanks for playing!"
gamechoice = int(raw_input("Play one game at a time (1) or play your chosen number of games with one strategy (2)? > "))
playchosengame(gamechoice)
#1 楼
欢迎使用一般的代码审查和编码方法:根据PEP8编写的代码–在您的情况下,这意味着对变量名和函数名使用
snake_case
,请在逗号后和运算符之间添加空格。一般而言,您的代码确实看起来不错,这里和那里都有一些小问题,但请通读指南并尝试遵守它们。功能:
incorrect_possibilities = possible_answers
使possible_answers
无效-稍后再执行incorrect_possiblities.remove(correct_answer)
时,这也会更改possible_answers
。要获得副本,您需要使用更好的副本版本,或者只是incorrect_possibilities = possible_answers[:]
。后者使用切片来表示整个数组,并复制切片区域。使用与
chosen_strategy
相关的三元数-根据选择的策略,您可以选择两个选项之一,可以将其编码为final_answer = correct_answer if chosen_strategy == 1 else your_answer
,并且在其他情况下也类似。也使用与打印输出有关的三元数–这也可以在打印输出中使用:
print("Which means it is either your answer: {}, or the only other remaining option, {}.".format(
correct_answer if your_answer == correct_answer else your_answer,
other_remaining if your_answer == correct_answer else correct_answer))
使用较新的
print('You chose {}'.format(final_answer)
语法–这是Python 2中新代码和Python 3中代码的首选版本。有关示例和说明,请参阅格式规范迷你语言。使用文档字符串
""" ... """
,而不是注释描述模块,函数,类和方法时,请参见# ...
。 -如果您使用文档字符串,则可以由您的IDE或文档工具使用它们,这取决于Pythonic方式。 :-) 避免使用一次性临时变量–如果使用
playagain
来保持游戏的正常进行,并将其结尾更改为while True:
,则可以删除if int(raw_input("Play again (0 = no)?")) == 0: break
。 (最好在break
之前加一个换行符,它确实会打破while循环)可能在主循环上添加循环或验证-如果您在一次玩一个游戏之间进行选择时输入了一些内容,或玩选定数量的游戏,您可以输入错误内容并终止脚本。可以通过在其周围添加一个while循环来增强此功能。
还请注意,如果您输入非数字文本,则代码很容易折断...考虑制作一个通用的int输入函数,该函数在
try ... except
函数周围使用input
。介绍
if __name__ == '__main__':
习惯用法-以前的代码通常用于使代码可作为模块重用,并且在顶层尽可能少地使用代码。结合main()
函数,您的代码可轻松重用和扩展。您可以使用(可能通过while循环扩展):
def main():
game_choice = int(raw_input("Play one game at a time (1) or play your chosen number of games with one strategy (2)? > "))
play_chosen_game(game_choice)
if __name__ == '__main__':
break
请考虑缩短文本长度–您的文本相当有价值,可以从略微缩短文本中受益。并且命名是很好的(单词之间不要使用下划线)。通过学习使用三元运算符(
a if b else c
),以及使用与顶级代码和文本长度相关的功能的一点道理,您可以从中受益。#2 楼
阅读BibleStyle Guide查看PEP-8(Python的样式指南)。从您的代码中迅速跳出来的是您有多少个变量和函数
lotsofwordsstucktogetherwithoutanyspacing
。这些很难读。首选的Python语法是snake_case
。即:def play_chosen_game(game_choice):
...
更容易解析。
Python不像C / Java
不必预先声明变量,也不需要手动递增。您在
one_strategy
中的主循环是:i = 1
for i in range(1, num_games + 1):
...
i += 1
这是错误的,原因如下:(1)您不需要初始的
i = 1
(2)结尾的i += 1
没有意义(3)range()
是错误的构造(4)当您确实需要i
时,在循环末尾引用num_games
。最好选择以下内容:for _ in xrange(num_games):
...
_
的命名清楚表明变量并不重要-您只是在做num_games
次。另外,我们遍历1..9
或0..8
都没关系,因此请选择一个更容易拼写的字母。 在此基础上,我们只想计算获胜次数,所以让我们明确地说一下:
wins = 0
for _ in xrange(num_games):
wins += play_game(num_doors, strategy_choice)
移动所有逻辑您已经进入了自己的功能循环。
捡起门
捡起门的逻辑有点难以理解。让我们简化一下。我们从所有门开始:
doors = range(num_doors)
我们随机选择一个正确的门,然后随机选择一个以供选择:
answer = random.randint(0, num_doors - 1)
first_choice = random.randint(0, num_doors - 1)
接下来,根据策略,我们要么留下,要么选择另一扇门:
if strategy_choice == 1:
# pick a new door that isn't first_choice or some random wrong one
goat = random.choice(
[i for i in xrange(num_doors) if i not in (first_choice, answer)]
)
final_answer = random.choice(
[i for i in xrange(num_doors) if i not in (first_choice, goat)]
)
else:
# stay put
final_choice = first_choice
更直接。
#3 楼
对于您编写代码的第二周来说,这是一件好事!我建议阅读Python的官方样式指南PEP8。
我还建议阅读一些我们评价最高的标记了python的问题,以启发人们如何改进/制作东西。
逗号后空格:
在某些情况下,逗号后空格不见了:
for i in range(1,chosennumberofgames + 1):
PEP8建议在逗号后提供一个空格。
snake_case
:您应该在
snake_case
中命名变量,这也是PEP8所建议的。incorrectpossibilities = possibleanswers
重复逻辑:
我看到很多重复逻辑,例如:
if youranswer == correctanswer:
print "Which means it is either your answer: %r, or the only other remaining option, %r." % (correctanswer, otherremaining)
else:
print "Which means it is either your answer: %r, or the only other remaining option, %r." % (youranswer, correctanswer)
>
我建议使用不同的方法简化这些操作:
answer_to_print_one = your_answer
answer_to_print_two = correct_answer
if your_answer == correct_answer:
answer_to_print_one = correct_answer
answer_to_print_two = other_remaining
print "Which means it is either your answer: {a1}, or the only other remaining option, {a2}.".format(a1=answer_to_print_one, a2=answer_to_print_two)
使用
"" %
:这是一个过时且低效的字符串连接方式:
使用
"{string}".format(string=your_string)
styl e级联。使用
class
:目前,您有一堆随机的关系函数,用于传递输入,参数等。
使用
class
代替,这样可以:正确链接功能
在类级别存储参数,减少对参数的需求
存储游戏的多个实例
#4 楼
您的名字很清楚而且很好,但是您的一些名字存在冗长的问题,尤其是冗长的词。例如,def playchosengame(chosengame):
您意识到这是这样的可读性:
def play(chosengame):
当然,您只需用
play(gamechoice)
如果还有另一个播放音乐的功能,则可能需要添加
play_game
,但就目前的情况而言,由于参数的命名方式几乎没有歧义。更重要的是,这些行要好读,而不是试图使每个名称告诉您有关其值的所有信息。
评论
您知道没有任何分隔符就很难读很多单词。也许那是值得思考的。下划线_are_the_standard_IIRC。