Hướng dẫn number guessing game python - trò chơi đoán số python
Hầu hết các chuyên viên máy tính từ nền tảng CS (Khoa học máy tính), hãy nghĩ về dự án đầu tiên của họ sau khi thực hiện ngôn ngữ lập trình. Ở đây, bạn sẽ nhận được dự án đầu tiên của bạn và dự án cơ bản, trong bài viết này.Computer Science) background, think of their very first project after doing a Programming Language. Here, you will get your very first project and the basic one, in this article.Computer Science) background, think of their very first project after doing a Programming Language. Here, you will get your very first project and the basic one, in this article. Show Nội dung chính ShowShow
Nhiệm vụ: Dưới đây là các bước: Below are the steps: Below are the steps:
Analysis: & nbsp; & nbsp; & nbsp; & nbsp; Giải thích 1: Nếu người dùng nhập vào phạm vi, hãy để nói từ 1 đến 100. và trình biên dịch được chọn ngẫu nhiên 42 là số nguyên. Và bây giờ trò chơi đoán đã bắt đầu, vì vậy người dùng đã nhập 50 như dự đoán đầu tiên của anh ấy/cô ấy. Trình biên dịch hiển thị lại thử lại! Bạn đoán quá cao. Điều đó có nghĩa là số ngẫu nhiên (tức là, 42) không rơi vào phạm vi từ 50 đến 100. Đó là tầm quan trọng của việc đoán một nửa phạm vi. Và một lần nữa, người dùng đoán một nửa trong số 50 (bạn có thể cho tôi biết lý do tại sao không?). Vì vậy, một nửa của 50 là 25. Người dùng nhập 25 như đoán thứ hai của anh ấy/cô ấy. Trình biên dịch lần này sẽ hiển thị, Hãy thử lại! Bạn đoán quá nhỏ ”. Điều đó có nghĩa là các số nguyên dưới 25 (từ 1 đến 25) là vô dụng để đoán. Bây giờ phạm vi để đoán người dùng là ngắn hơn, tức là, từ 25 đến 50. thông minh! Người dùng đã đoán một nửa phạm vi này, do đó, người dùng đoán 37 là đoán thứ ba của anh ấy/cô ấy. & nbsp; lần này một lần nữa trình biên dịch hiển thị đầu ra, Hãy thử lại! Bạn đoán quá nhỏ ”. Đối với người dùng, phạm vi đoán đang trở nên nhỏ hơn theo mỗi dự đoán. Bây giờ, phạm vi đoán cho người dùng là từ 37 đến 50, trong đó người dùng đoán 43 là đoán thứ tư của mình. Lần này, trình biên dịch sẽ hiển thị một đầu ra, hãy thử lại! Bạn đoán quá cao. Vì vậy, phạm vi đoán mới cho người dùng sẽ từ 37 đến 43, một lần nữa mà người dùng đoán được một nửa phạm vi này, nghĩa là 40 như đoán thứ năm của anh ấy/cô ấy. & nbsp; lần này trình biên dịch hiển thị đầu ra, Hãy thử lại! Bạn đoán quá nhỏ ”. Để lại dự đoán thậm chí còn nhỏ hơn từ 41 đến 43. Và bây giờ người dùng đoán 41 là đoán thứ sáu của mình. Đó là sai và hiển thị đầu ra, hãy thử lại! Bạn đoán quá nhỏ ”. Và cuối cùng, người dùng đã đoán đúng số đó là 42 như dự đoán thứ bảy của anh ấy/cô ấy.Explanation 1: If the User inputs range, let’s say from 1 to 100. And compiler randomly selected 42 as the integer. And now the guessing game started, so the user entered 50 as his/her first guess. The compiler shows “Try Again! You guessed too high”. That’s mean the random number (i.e., 42) doesn’t fall in the range from 50 to 100. That’s the importance of guessing half of the range. And again, the user guesses half of 50 (Could you tell me why?). So the half of 50 is 25. The user enters 25 as his/her second guess. This time compiler will show, “Try Again! You guessed too small”. That’s mean the integers less than 25 (from 1 to 25) are useless to be guessed. Now the range for user guessing is shorter, i.e., from 25 to 50. Intelligently! The user guessed half of this range, so that, user guessed 37 as his/her third guess. This time again the compiler shows the output, “Try Again! You guessed too small”. For the user, the guessing range is getting smaller by each guess. Now, the guessing range for user is from 37 to 50, for which the user guessed 43 as his/her fourth guess. This time the compiler will show an output “Try Again! You guessed too high”. So, the new guessing range for users will be from 37 to 43, again for which the user guessed the half of this range, that is, 40 as his/her fifth guess. This time the compiler shows the output, “Try Again! You guessed too small”. Leaving the guess even smaller such that from 41 to 43. And now the user guessed 41 as his/her sixth guess. Which is wrong and shows output “Try Again! You guessed too small”. And finally, the User Guessed the right number which is 42 as his/her seventh guess.Explanation 1: If the User inputs range, let’s say from 1 to 100. And compiler randomly selected 42 as the integer. And now the guessing game started, so the user entered 50 as his/her first guess. The compiler shows “Try Again! You guessed too high”. That’s mean the random number (i.e., 42) doesn’t fall in the range from 50 to 100. That’s the importance of guessing half of the range. And again, the user guesses half of 50 (Could you tell me why?). So the half of 50 is 25. The user enters 25 as his/her second guess. This time compiler will show, “Try Again! You guessed too small”. That’s mean the integers less than 25 (from 1 to 25) are useless to be guessed. Now the range for user guessing is shorter, i.e., from 25 to 50. Intelligently! The user guessed half of this range, so that, user guessed 37 as his/her third guess. This time again the compiler shows the output, “Try Again! You guessed too small”. For the user, the guessing range is getting smaller by each guess. Now, the guessing range for user is from 37 to 50, for which the user guessed 43 as his/her fourth guess. This time the compiler will show an output “Try Again! You guessed too high”. So, the new guessing range for users will be from 37 to 43, again for which the user guessed the half of this range, that is, 40 as his/her fifth guess. This time the compiler shows the output, “Try Again! You guessed too small”. Leaving the guess even smaller such that from 41 to 43. And now the user guessed 41 as his/her sixth guess. Which is wrong and shows output “Try Again! You guessed too small”. And finally, the User Guessed the right number which is 42 as his/her seventh guess. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Tổng số dự đoán = 7 Total Number of Guesses = 7 Total Number of Guesses = 7 & nbsp; & nbsp; Giải thích 2: Nếu phạm vi đầu vào của người dùng, hãy nói từ 1 đến 50. và trình biên dịch được chọn ngẫu nhiên 42 là số nguyên. Và bây giờ trò chơi đoán bắt đầu. Vì vậy, một nửa của 50 là 25. Người dùng nhập 25 như dự đoán đầu tiên của anh ấy/cô ấy. Trình biên dịch lần này sẽ hiển thị, Hãy thử lại! Bạn đoán quá nhỏ ”. Điều đó có nghĩa là các số nguyên dưới 25 (từ 1 đến 25) là vô dụng để đoán. Bây giờ phạm vi để đoán người dùng là ngắn hơn, tức là, từ 25 đến 50. thông minh! Người dùng đoán một nửa phạm vi này, do đó, người dùng đoán 37 là đoán thứ hai của anh ấy/cô ấy. & nbsp; lần này một lần nữa trình biên dịch hiển thị đầu ra, Hãy thử lại! Bạn đoán quá nhỏ ”. Đối với người dùng, phạm vi đoán đang trở nên nhỏ hơn theo mỗi dự đoán. Bây giờ, phạm vi đoán cho người dùng là từ 37 đến 50, mà người dùng đoán 43 là đoán thứ ba của mình. Lần này, trình biên dịch sẽ hiển thị một đầu ra, hãy thử lại! Bạn đoán quá cao. Vì vậy, phạm vi đoán mới cho người dùng sẽ từ 37 đến 43, một lần nữa mà người dùng đoán được một nửa phạm vi này, nghĩa là 40 như dự đoán thứ tư của anh ấy/cô ấy. & nbsp; lần này trình biên dịch hiển thị đầu ra, Hãy thử lại! Bạn đoán quá nhỏ ”. Để lại dự đoán thậm chí còn nhỏ hơn từ 41 đến 43. và bây giờ người dùng đoán 41 là đoán thứ năm của mình. Đó là sai và hiển thị đầu ra, hãy thử lại! Bạn đoán quá nhỏ ”. Và cuối cùng, người dùng đã đoán đúng số đó là 42 như đoán thứ sáu của anh ấy/cô ấy. Explanation 2: If the User inputs range, let’s say from 1 to 50. And compiler randomly selected 42 as the integer. And now the guessing game started. So the half of 50 is 25. The user enters 25 as his/her First guess. This time compiler will show, “Try Again! You guessed too small”. That’s mean the integers less than 25 (from 1 to 25) are useless to be guessed. Now the range for user guessing is shorter, i.e., from 25 to 50. Intelligently! User guessed half of this range, so that, user guessed 37 as his/her second guess. This time again the compiler shows the output, “Try Again! You guessed too small”. For the user, the guessing range is getting smaller by each guess. Now, the guessing range for user is from 37 to 50, for which the user guessed 43 as his/her third guess. This time the compiler will show an output “Try Again! You guessed too high”. So, the new guessing range for users will be from 37 to 43, again for which the user guessed the half of this range, that is, 40 as his/her fourth guess. This time the compiler shows the output, “Try Again! You guessed too small”. Leaving the guess even smaller such that from 41 to 43. And now the user guessed 41 as his/her fifth guess. Which is wrong and shows output “Try Again! You guessed too small”. And finally, the User Guessed the right number which is 42 as his/her sixth guess. Explanation 2: If the User inputs range, let’s say from 1 to 50. And compiler randomly selected 42 as the integer. And now the guessing game started. So the half of 50 is 25. The user enters 25 as his/her First guess. This time compiler will show, “Try Again! You guessed too small”. That’s mean the integers less than 25 (from 1 to 25) are useless to be guessed. Now the range for user guessing is shorter, i.e., from 25 to 50. Intelligently! User guessed half of this range, so that, user guessed 37 as his/her second guess. This time again the compiler shows the output, “Try Again! You guessed too small”. For the user, the guessing range is getting smaller by each guess. Now, the guessing range for user is from 37 to 50, for which the user guessed 43 as his/her third guess. This time the compiler will show an output “Try Again! You guessed too high”. So, the new guessing range for users will be from 37 to 43, again for which the user guessed the half of this range, that is, 40 as his/her fourth guess. This time the compiler shows the output, “Try Again! You guessed too small”. Leaving the guess even smaller such that from 41 to 43. And now the user guessed 41 as his/her fifth guess. Which is wrong and shows output “Try Again! You guessed too small”. And finally, the User Guessed the right number which is 42 as his/her sixth guess. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; tổng số dự đoán = 6Total Number of Guesses = 6Total Number of Guesses = 6 Vì vậy, số lượng tối thiểu của các dự đoán phụ thuộc vào phạm vi. Và trình biên dịch phải tính toán số lượng đoán tối thiểu phụ thuộc vào phạm vi, chính nó. Đối với điều này, chúng tôi có một công thức:-
Thuật toán: Dưới đây là các bước:: Below are the Steps:
Dưới đây là việc thực hiện thuật toán: Python3
Các Các
Các
C
‘
Đầu ra: Dưới đây là đầu ra của chương trình trênBelow is the output of the above ProgramBelow is the output of the above Program Đầu ra cho trò chơi đoán Làm thế nào để bạn tạo ra một số trong trò chơi Python đoán?So below is how you can write a program to create a number guessing game using Python: import random n = random. randrange(1,10) guess = int(input("Enter any number: ")) while n!= guess: if guess < n: print("Too low") guess = int(input("Enter number again: ")) elif guess > n: print("Too high! Làm thế nào để bạn thực hiện một trò chơi đoán trong Python 3?Mã hóa trong trình soạn thảo văn bản của bạn Nhập Num ngẫu nhiên = ngẫu nhiên.randint (1, 10) đoán = không có trong khi đoán! = num: guess = input ("đoán một số từ 1 đến 10:") đoán = int (đoán) nếu đoán == num: in ("Xin chúc mừng! Bạn đã thắng! Làm thế nào để bạn chơi đoán trò chơi số?Một trò chơi đoán số là một trò chơi đoán đơn giản trong đó người dùng được cho là đoán một số từ 0 đến n trong tối đa 10 lần thử.Trò chơi sẽ kết thúc sau 10 lần thử và nếu người chơi không đoán được con số, và sau đó anh ta thua trò chơi.a user is supposed to guess a number between 0 and N in a maximum of 10 attempts. The game will end after 10 attempts and if the player failed to guess the number, and then he loses the game.a user is supposed to guess a number between 0 and N in a maximum of 10 attempts. The game will end after 10 attempts and if the player failed to guess the number, and then he loses the game. Làm thế nào để bạn đoán một thuật toán số?Đây là các bước thực tế (thuật toán) chúng tôi vừa sử dụng:... Thêm phạm vi cao nhất + phạm vi thấp nhất - 1 = Có thể đoán .. Chia những dự đoán có thể cho 2 vòng lên (đoán của bạn), hỏi đây có phải là số của bạn không?. Nếu dự đoán của bạn ở mức thấp: phạm vi thấp nhất = dự đoán của bạn +1 .. Nếu dự đoán của bạn là cao: .... Nếu dự đoán của bạn không ở mức thấp hoặc cao: .... Repeat.. |