本文介绍了纽布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好,我是小组的新手。试着在我的 上学习Python编程。我正在通过Michael Dawson的书籍Python编程为 绝对的初学者。 我想写一个数字猜谜游戏的程序。我希望能够在程序结束前让用户5尝试猜测数字。 我得到了用户想要的结果在第5次尝试后错过了。但是它不会在我的结束语中结束。而是要求 另一个猜测。 我感谢您提供的任何帮助。如果这不是这些类型的问题的正确组,我道歉并希望你能指导我到另一个NG。 谢谢, 乍得 随机导入 打印\tWelcome to''猜猜我的号码''!" 打印\ nI''m想到1到100之间的数字。 print" \ nTry尽可能少地猜测它。\ n" #设置初始值 the_number = random.randrange(100)+ 1 guess = int(raw_input("猜测:")) tries = 1 #Guessing Loop if(guess> the_number): print" Lower ... else: print" Higher ..." guess = int(raw_input(" Take another Guess:" ;)) 尝试+ = 1 如果尝试== 5: print" ;抱歉,你输了。 打印正确的数字是,数字 打印你猜对了!!" print"数字是:",the_number print"你在','尝试','尝试'中得到了正确的答案 raw_input (按Enter退出程序) Hi all, I am new to the group. Trying to learn Python programming on myown. I am working through Michael Dawson''s Book Python Programming for theabsolute beginner. I am tring to write a program that is a number guessing game. I want to beable to give the user 5 tries to guess the number before the program ends. I get the result that I want when the user misses after the 5th try. But itdoes not go down to my closing statement to end. Rather is askes foranother guess. I appreciate any help you can give. If this is not the correct group forthese types of questions, I apologize and hope that you can direct me toanother NG. Thanks,Chad import random print "\tWelcome to ''Guess my Number''!"print "\nI''m Thinking of a Number between 1 and 100."print "\nTry to Guess it in as few attempts as possible.\n" #Set the initial valuesthe_number = random.randrange(100) + 1guess = 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 another Guess:"))tries +=1if tries == 5:print "Sorry you lose."print "The Correct Number was ", the_numberprint "You guess correctly!!"print "The number was:", the_numberprint "You got it correct in", tries, "tries" raw_input("Press Enter to exit the program")推荐答案 Chad Everett写道:Chad Everett wrote:绝对的初学者。 我想写一个数字猜谜游戏的程序。我希望能够在程序结束前让用户5尝试猜测数字。 当用户在第5次尝试后未命中时,我得到了我想要的结果。但它并没有结束我的结束语。而是要求进行另一次猜测。 因为即使经过5次尝试,猜猜!= the_number。你不会超过 while循环直到它。当有5次未命中时,请尝试 设置 guess = the_number。 我感谢您提供的任何帮助。如果这不是这些类型问题的正确小组,我道歉并希望你能指导我到另一个NG。 谢谢, Chad 随意导入 打印\t欢迎来''猜猜我的号码''!打印\ n我想到的是数字介于1和100之间。 print\ n尝试尽可能少地猜测它。\ n" #设置初始值 thenumber = random.randrange(100)+ 1 guess = int(raw_input(" Take a guess:"))尝试= 1 #Guessing Loop while(guess!= the_number): if(guess> the_number): print" Lower ..." else: print" ;更高......" guess = int(raw_input(" Take another Guess:"))尝试+ = 1 如果尝试== 5:打印抱歉,你输了。打印正确号码是,the_nu mber print"你猜对了!!" print"数字是:",the_number print"你把它弄错了,试试, 尝试 raw_input("按Enter键退出程序) Hi all, I am new to the group. Trying to learn Python programming on my own. I am working through Michael Dawson''s Book Python Programming for the absolute beginner. I am tring to write a program that is a number guessing game. I want to be able to give the user 5 tries to guess the number before the program ends. I get the result that I want when the user misses after the 5th try. But it does not go down to my closing statement to end. Rather is askes for another guess.Because even after 5 tries, guess != the_number. You won''t get pastthe while loop until it does. When there have been 5 misses, trysettingguess=the_number. I appreciate any help you can give. If this is not the correct group for these types of questions, I apologize and hope that you can direct me to another NG. Thanks, Chad import random print "\tWelcome to ''Guess my Number''!" print "\nI''m Thinking of a Number between 1 and 100." print "\nTry 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 another Guess:")) tries +=1 if tries == 5: print "Sorry you lose." print "The Correct Number was ", the_number print "You guess correctly!!" print "The number was:", the_number print "You got it correct in", tries, "tries" raw_input("Press Enter to exit the program") me********@aol.com 写道: Chad Everett写道: Chad Everett wrote:大家好,我是小组的新手。试着在我自己的上学习Python编程。我正在通过迈克尔道森的书籍Python编程为绝对的初学者。 我想写一个数字猜谜游戏的程序。我希望能够在程序结束前让用户5尝试猜测数字。 当用户在第5次尝试后未命中时,我得到了我想要的结果。但它并没有结束我的结束语。而是要求进行另一次猜测。 Hi all, I am new to the group. Trying to learn Python programming on my own. I am working through Michael Dawson''s Book Python Programming for the absolute beginner. I am tring to write a program that is a number guessing game. I want to be able to give the user 5 tries to guess the number before the program ends. I get the result that I want when the user misses after the 5th try. But it does not go down to my closing statement to end. Rather is askes for another guess. 因为即使经过5次尝试,猜猜!= the_number。你不会越过 while循环直到它。如果有5次未命中,请尝试设置 guess = the_number。 Because even after 5 tries, guess != the_number. You won''t get past the while loop until it does. When there have been 5 misses, try setting guess=the_number. 我感谢您提供的任何帮助。如果这不是这些类型问题的正确小组,我道歉并希望你能指导我到另一个NG。 谢谢, Chad 随意导入 打印\t欢迎来''猜猜我的号码''!打印\ n我想到的是数字介于1和100之间。 print\ n尝试尽可能少地猜测它。\ n" #设置初始值 thenumber = random.randrange(100)+ 1 guess = int(raw_input(" Take a guess:"))尝试= 1 #Guessing Loop while(guess!= the_number): if(guess> the_number): print" Lower ..." else: print" ;更高......" guess = int(raw_input(" Take another Guess:"))尝试+ = 1 如果尝试== 5:打印抱歉,你输了。打印正确号码是,the_nu mber print"你猜对了!!" print"数字是:",the_number print"你把它弄错了,试试, 尝试 raw_input(按Enter退出程序) I appreciate any help you can give. If this is not the correct group for these types of questions, I apologize and hope that you can direct me to another NG. Thanks, Chad import random print "\tWelcome to ''Guess my Number''!" print "\nI''m Thinking of a Number between 1 and 100." print "\nTry 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 another Guess:")) tries +=1 if tries == 5: print "Sorry you lose." print "The Correct Number was ", the_number print "You guess correctly!!" print "The number was:", the_number print "You got it correct in", tries, "tries" raw_input("Press Enter to exit the program") 这里是您的更正版本: ##从这里它很好 随机导入 打印\tWelcome to''猜猜我的数字''!' 打印\ nI''m想到1到100之间的数字。 print" \ n。尝试猜猜尽可能少的尝试。\ n" #设置初始值 ##这里你总是得到100,randrange从开始到结束+ 1 the_number = random.randrange(0,101) ##这没关系 guess = int(raw_input(" Take a)猜测:)) 尝试= 1 #Guessing Loop while(guess!= the_number ): if(guess> the_number): print" Lower ..." else: print"更高...... guess = int(raw_input(" Take another Guess:")) 尝试+ = 1 如果尝试== 5: ##在5点之后错过中断显式离开循环 中断 ##然后测试看看最后猜测等于the_number 如果猜!= the_number: 打印抱歉,你输了。 print" The Correct Number is ,the_number else: print"你猜对了!!" print"数字是:",the_number print"你在尝试,尝试中得到了正确的答案 raw_input("按Enter键退出程序) here is your corrected version:##FROM here it''s goodimport random print "\tWelcome to ''Guess my Number''!"print "\nI''m Thinking of a Number between 1 and 100."print "\nTry to Guess it in as few attempts as possible.\n" #Set the initial values## Here you always get 100, randrange is from start to end+1the_number = random.randrange(0,101)##This is OKguess = 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 another Guess:"))tries +=1if tries == 5:## Missing break to explicitly leave loop after 5break## Then test to see if last guess equals the_numberif guess != the_number:print "Sorry you lose."print "The Correct Number was ", the_numberelse:print "You guess correctly!!"print "The number was:", the_numberprint "You got it correct in", tries, "tries" raw_input("Press Enter to exit the program") Chad Everett写道:Chad Everett wrote:大家好,我是小组的新手。试着在我自己的上学习Python编程。我正在通过迈克尔道森的书籍Python编程为绝对的初学者。 我想写一个数字猜谜游戏的程序。我希望能够在程序结束前让用户5尝试猜测数字。 当用户在第5次尝试后未命中时,我得到了我想要的结果。但它并没有结束我的结束语。而是要求进行另一次猜测。 我感谢您提供的任何帮助。如果这不是这些类型问题的正确小组,我道歉并希望你能指导我到另一个NG。 谢谢, Chad 随意导入 打印\t欢迎来''猜猜我的号码''!打印\ n我想到的是数字介于1和100之间。 print\ n尝试尽可能少地猜测它。\ n" #设置初始值 thenumber = random.randrange(100)+ 1 guess = int(raw_input(" Take a guess:"))尝试= 1 #Guessing Loop while(guess!= the_number): if(guess> the_number): print" Lower ..." else: print" ;更高......" guess = int(raw_input(" Take another Guess:"))尝试+ = 1 如果尝试== 5:打印抱歉,你输了。打印正确号码是,the_nu mber print"你猜对了!!" print"数字是:",the_number print"你把它弄错了,试试, 尝试 raw_input(按Enter退出程序) Hi all, I am new to the group. Trying to learn Python programming on my own. I am working through Michael Dawson''s Book Python Programming for the absolute beginner. I am tring to write a program that is a number guessing game. I want to be able to give the user 5 tries to guess the number before the program ends. I get the result that I want when the user misses after the 5th try. But it does not go down to my closing statement to end. Rather is askes for another guess. I appreciate any help you can give. If this is not the correct group for these types of questions, I apologize and hope that you can direct me to another NG. Thanks, Chad import random print "\tWelcome to ''Guess my Number''!" print "\nI''m Thinking of a Number between 1 and 100." print "\nTry 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 another Guess:")) tries +=1 if tries == 5: print "Sorry you lose." print "The Correct Number was ", the_number print "You guess correctly!!" print "The number was:", the_number print "You got it correct in", tries, "tries" raw_input("Press Enter to exit the program") 我个人认为这两种可能你得到的解决方案 非常pythonic。所有你真的需要做的就是让循环终止 条件正确(他自信地说,并且完全没有测试 all)将while行更改为 而猜!= the_number并尝试< = 5: 但该程序还有其他问题。在伪代码中 (可以帮助你思考问题的东西,而无法将它提供给解释器)你的解决方案应该是 虽然没有猜到和猜测< 5 猜数字 如果猜对了 祝贺用户 其他 乌鸦和擦拭用户的脸 目前你的代码将始终告诉用户她猜对了,即使在告诉她她之后也是b $ b丢失。看看这个(测试过的)重写是否对你有意义: 随机导入 print" \tWelcome to' '猜猜我的号码'!'" 打印\ n我想到1到100之间的数字。 print" \ n尽量少尝试猜猜它。\ n" #设置初始值 the_number = random.randrange(100)+ 1 guess = int(raw_input("猜猜:")) tries = 1 #Guessing Loop 而猜!= the_number并尝试< 5: if(guess> the_number): print" Lower ..." else: 打印更高...... guess = int(raw_input(" Take another Guess:")) 尝试+ = 1 如果猜= = the_number: 打印"你猜对了!!" print"数字是:" ;,the_number print"你在','尝试','尝试'中得到了正确的答案 else: print"抱歉,你输了。 打印正确的数字是,数字 问候 史蒂夫 - Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.python.org/pycon/ I don''t personally think the two possible solutions you''ve been givenare very pythonic. All you really need to do to get the loop terminationconditions right (he says confidently, and with absolutely no testing atall) is change the while line to while guess != the_number and tries <=5: but there are other things wrong with the program. In "pseudo-code"(stuff that helps you think about the problem without being able to feedit into an interpreter) your solution should be while not guessed and guesses < 5guess numberif guessedcongratulate userelsecrow and rub user''s face in it At present your code will always tell the user she guessed correctly,even after it''s told her she lost. See if this (tested) rewrite makessense to you:import random print "\tWelcome to ''Guess my Number''!"print "\nI''m Thinking of a Number between 1 and 100."print "\nTry to Guess it in as few attempts as possible.\n" #Set the initial valuesthe_number = random.randrange(100) + 1guess = int(raw_input("Take a guess:"))tries = 1 #Guessing Loop while guess != the_number and tries < 5:if (guess >the_number):print "Lower..."else:print "Higher..."guess = int(raw_input("Take another Guess:"))tries +=1 if guess == the_number:print "You guess correctly!!"print "The number was:", the_numberprint "You got it correct in", tries, "tries"else:print "Sorry you lose."print "The Correct Number was ", the_number regardsSteve--Steve Holden +44 150 684 7255 +1 800 494 3119Holden Web LLC www.holdenweb.comPyCon TX 2006 www.python.org/pycon/ 这篇关于纽布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 04:38