Randint () là một hàm sẵn của mô -đun ngẫu nhiên trong Python3. Mô -đun ngẫu nhiên cho phép truy cập vào các chức năng hữu ích khác nhau và một trong số chúng có thể tạo các số ngẫu nhiên, đó là randint (). & Nbsp; cú pháp: & nbsp; is an inbuilt function of the random module in Python3. The random module gives access to various useful functions and one of them being able to generate random numbers, which is randint().
Syntax :
Tham số: & nbsp;
(start, end) : Both of them must be integer type values.Trả về: & nbsp;
A random integer in range [start, end] including the end points.Lỗi và ngoại lệ:
ValueError : Returns a ValueError when floating point values are passed as parameters. TypeError : Returns a TypeError when anything other than numeric values are passed as parameters.& nbsp; Mã số 1: & NBSP;Code #1 :
Python3
import random
____10(start, end) : Both of them must be integer type values.1 (start, end) : Both of them must be integer type values.2(start, end) : Both of them must be integer type values.3(start, end) : Both of them must be integer type values.4(start, end) : Both of them must be integer type values.5(start, end) : Both of them must be integer type values.6
(start, end) : Both of them must be integer type values.7(start, end) : Both of them must be integer type values.8(start, end) : Both of them must be integer type values.3 A random integer in range [start, end] including the end points.0 (start, end) : Both of them must be integer type values.5 A random integer in range [start, end] including the end points.2 A random integer in range [start, end] including the end points.3 A random integer in range [start, end] including the end points.4A random integer in range [start, end] including the end points.3 A random integer in range [start, end] including the end points.6
Các
(start, end) : Both of them must be integer type values.7(start, end) : Both of them must be integer type values.8ValueError : Returns a ValueError when floating point values are passed as parameters. TypeError : Returns a TypeError when anything other than numeric values are passed as parameters.0(start, end) : Both of them must be integer type values.5 A random integer in range [start, end] including the end points.0
Các
(start, end) : Both of them must be integer type values.7(start, end) : Both of them must be integer type values.8ValueError : Returns a ValueError when floating point values are passed as parameters. TypeError : Returns a TypeError when anything other than numeric values are passed as parameters.0 ____52
Đầu ra:
Random number between 0 and 10 is 5 Random number between -10 and -1 is -7 Random number between -5 and 5 is 2& nbsp; Mã số 2: Chương trình thể hiện giá trịerRor. & NBSP;Code #2 : Program demonstrating the ValueError.
Python3
import random
____10(start, end) : Both of them must be integer type values.1 (start, end) : Both of them must be integer type values.2(start, end) : Both of them must be integer type values.3(start, end) : Both of them must be integer type values.4(start, end) : Both of them must be integer type values.5(start, end) : Both of them must be integer type values.6
(start, end) : Both of them must be integer type values.7Pick your number to enter the lucky draw 8 Wrong Guess!! Pick your number to enter the lucky draw 9 Wrong Guess!! Pick your number to enter the lucky draw 0 Congrats!! You Win.7
Đầu ra:
Traceback (most recent call last): File "/home/f813370b9ea61dd5d55d7dadc8ed5171.py", line 6, in r1=random.randint(1.23, 9.34) File "/usr/lib/python3.5/random.py", line 218, in randint return self.randrange(a, b+1) File "/usr/lib/python3.5/random.py", line 182, in randrange raise ValueError("non-integer arg 1 for randrange()") ValueError: non-integer arg 1 for randrange()& nbsp; Mã số 2: Chương trình thể hiện giá trịerRor. & NBSP;Code #3 : Program demonstrating the TypeError.
Python3
import random
____10(start, end) : Both of them must be integer type values.1 (start, end) : Both of them must be integer type values.2(start, end) : Both of them must be integer type values.3(start, end) : Both of them must be integer type values.4(start, end) : Both of them must be integer type values.5(start, end) : Both of them must be integer type values.6
(start, end) : Both of them must be integer type values.7import8
(start, end) : Both of them must be integer type values.7(start, end) : Both of them must be integer type values.8(start, end) : Both of them must be integer type values.3 A random integer in range [start, end] including the end points.0 (start, end) : Both of them must be integer type values.5 A random integer in range [start, end] including the end points.2 A random integer in range [start, end] including the end points.3 A random integer in range [start, end] including the end points.4A random integer in range [start, end] including the end points.3 A random integer in range [start, end] including the end points.6
Traceback (most recent call last): File "/home/fb805b21fea0e29c6a65f62b99998953.py", line 5, in r2=random.randint('a', 'z') File "/usr/lib/python3.5/random.py", line 218, in randint return self.randrange(a, b+1) TypeError: Can't convert 'int' object to str implicitlyCácApplications : The randint() function can be used to simulate a lucky draw situation. Let’s say User has participated in a lucky draw competition. The user gets three chances to guess the number between 1 and 10. If guess is correct user wins, else loses the competition.
Python3
(start, end) : Both of them must be integer type values.7(start, end) : Both of them must be integer type values.8ValueError : Returns a ValueError when floating point values are passed as parameters. TypeError : Returns a TypeError when anything other than numeric values are passed as parameters.0(start, end) : Both of them must be integer type values.5 A random integer in range [start, end] including the end points.0
Các
(start, end) : Both of them must be integer type values.7(start, end) : Both of them must be integer type values.8ValueError : Returns a ValueError when floating point values are passed as parameters. TypeError : Returns a TypeError when anything other than numeric values are passed as parameters.0 ____52
Đầu ra:
& nbsp; Mã số 2: Chương trình thể hiện giá trịerRor. & NBSP;
Các
& nbsp; Mã số 3: Chương trình thể hiện kiểu mẫu. & NBSP;
Các
Đầu ra: & nbsp;
(start, end) : Both of them must be integer type values.28(start, end) : Both of them must be integer type values.29
& nbsp; Ứng dụng: Hàm randint () có thể được sử dụng để mô phỏng tình huống rút thăm may mắn. Hãy nói rằng người dùng đã tham gia vào một cuộc thi rút thăm may mắn. Người dùng có ba cơ hội để đoán số từ 1 đến 10. Nếu đoán là người dùng chính xác sẽ thắng, nếu không sẽ mất đối thủ. & NBSP;
import9 random0import random2
(start, end) : Both of them must be integer type values.36(start, end) : Both of them must be integer type values.41
(start, end) : Both of them must be integer type values.21(start, end) : Both of them must be integer type values.43(start, end) : Both of them must be integer type values.20
(start, end) : Both of them must be integer type values.36(start, end) : Both of them must be integer type values.7(start, end) : Both of them must be integer type values.47
random3 random4
random5random6 random7ValueError : Returns a ValueError when floating point values are passed as parameters. TypeError : Returns a TypeError when anything other than numeric values are passed as parameters.4(start, end) : Both of them must be integer type values.4(start, end) : Both of them must be integer type values.5__
random3 (start, end) : Both of them must be integer type values.03
random5(start, end) : Both of them must be integer type values.43(start, end) : Both of them must be integer type values.20
random5(start, end) : Both of them must be integer type values.05(start, end) : Both of them must be integer type values.1 (start, end) : Both of them must be integer type values.07
random5(start, end) : Both of them must be integer type values.09(start, end) : Both of them must be integer type values.1 (start, end) : Both of them must be integer type values.11
random5(start, end) : Both of them must be integer type values.13(start, end) : Both of them must be integer type values.1 (start, end) : Both of them must be integer type values.3
random5(start, end) : Both of them must be integer type values.17 (start, end) : Both of them must be integer type values.18(start, end) : Both of them must be integer type values.3(start, end) : Both of them must be integer type values.20
(start, end) : Both of them must be integer type values.21(start, end) : Both of them must be integer type values.222____11 (start, end) : Both of them must be integer type values.24(start, end) : Both of them must be integer type values.25(start, end) : Both of them must be integer type values.26(start, end) : Both of them must be integer type values.27
(start, end) : Both of them must be integer type values.21(start, end) : Both of them must be integer type values.7(start, end) : Both of them must be integer type values.88
(start, end) : Both of them must be integer type values.7(start, end) : Both of them must be integer type values.8(start, end) : Both of them must be integer type values.3 A random integer in range [start, end] including the end points.0 (start, end) : Both of them must be integer type values.5 A random integer in range [start, end] including the end points.2 A random integer in range [start, end] including the end points.3 A random integer in range [start, end] including the end points.4A random integer in range [start, end] including the end points.3 A random integer in range [start, end] including the end points.6
Pick your number to enter the lucky draw 8 Wrong Guess!! Pick your number to enter the lucky draw 9 Wrong Guess!! Pick your number to enter the lucky draw 0 Congrats!! You Win.