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.

Nội dung chính ShowShow

  • Thuật toán: Dưới đây là các bước:: Below are the Steps:
  • Làm thế nào để bạn tạo ra một số trong trò chơi Python đoán?
  • Làm thế nào để bạn thực hiện một trò chơi đoán trong Python 3?
  • Làm thế nào để bạn chơi đoán trò chơi số?
  • Làm thế nào để bạn đoán một thuật toán số?

Nhiệm vụ: Dưới đây là các bước: Below are the steps: Below are the steps:

  • Xây dựng một trò chơi đoán số, trong đó người dùng chọn một phạm vi.user selects a range.user selects a range.
  • Hãy nói rằng người dùng đã chọn một phạm vi, tức là, từ A đến B, trong đó A và B thuộc về số nguyên. A to B, where A and B belong to Integer. A to B, where A and B belong to Integer.
  • Một số số nguyên ngẫu nhiên sẽ được hệ thống chọn và người dùng phải đoán rằng số nguyên trong số lượng dự đoán tối thiểurandom integer will be selected by the system and the user has to guess that integer in the minimum number of guessesrandom integer will be selected by the system and the user has to guess that integer in the minimum number of guesses

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:-

& nbsp; Số lượng đoán tối thiểu = log2 (giới hạn trên - giới hạn dưới + 1)

Thuật toán: Dưới đây là các bước:: Below are the Steps:

  • Người dùng nhập giới hạn dưới và giới hạn trên của phạm vi.lower bound and upper bound of the range.lower bound and upper bound of the range.
  • Trình biên dịch tạo ra một số nguyên ngẫu nhiên giữa phạm vi và lưu trữ nó trong một biến cho các tài liệu tham khảo trong tương lai.
  • Để đoán lặp đi lặp lại, một vòng lặp trong thời gian sẽ được khởi tạo.
  • Nếu người dùng đoán một số lớn hơn một số được chọn ngẫu nhiên, người dùng sẽ nhận được đầu ra, hãy thử lại! Bạn đoán quá cao ”
  • Khác nếu người dùng đoán một số nhỏ hơn số được chọn ngẫu nhiên, người dùng sẽ nhận được đầu ra, hãy thử lại! Bạn đoán quá nhỏ ”
  • Và nếu người dùng đoán trong một số lượng phỏng đoán tối thiểu, người dùng sẽ nhận được một lời chúc mừng! Đầu ra.
  • Nếu không, nếu người dùng không đoán được số nguyên với số lượng dự đoán tối thiểu, anh ấy/cô ấy sẽ nhận được may mắn hơn vào lần tới! đầu ra.

Dưới đây là việc thực hiện thuật toán:

Python3

import random

import math

Các

Các

import0= import2

import3(import5import6

import7import8import9math0 random5math2 math3math4math5math6

math7math8math9

random50= random52

random53 random54math0 random5math2 math3math4math5__

=2random50math2= math3

Các

=2import17 import0== (1

(2import3((5import6

(7(8(9math9

(2=62

=2=64 =65

(2import3(=69math9

=2=64 (3

(2import3((7math9

import17 random00=

=2import3(random13 random14 random15

=2import3(random19math9

C

random21

random22

random23

import1 random25

random26

=2import1 random29

=2random31(random33random34

=2random36random37random38

=2random40

=2random26

(2random44(random46random47

(2random49(random51random52

(2import17random55

(2random26

random58random44(random61random47

(2random64

(2random66 import17random68

(2random26

random58random44(random74random47

(2random64

(2random66

(2random26

random58random44(random85random47

(2random64

(2random95

=2random97random53random99

=2import01 import02

random64

Đầ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..