Hướng dẫn how do you input 10 numbers in python? - làm thế nào để bạn nhập 10 số trong python?

Rất đơn giản, tôi thực hiện chương trình này cho bạn theo yêu cầu của bạn rằng người dùng phải nhập 10 số và chương trình tìm thấy nó là chính hay không.

thử cái này:

for i in range[0, 10]:
    num = int[input['Enter Number:']]
    if num > 1:
        # check for factors
        for i in range[2, num]:
            if [num % i] == 0:
                print[num, "is not a prime number"]
                print[i, "times", num // i, "is", num]
                break
        else:
            print[num, "is a prime number"]

    # if input number is less than
    # or equal to 1, it is not prime
    else:
        print[num, "is not a prime number"]

Lưu ý: Trong một vòng lặp, một lần đầu vào từ người dùng 10 lần nếu bạn muốn lấy đầu vào khi bắt đầu thì bạn cũng có thể đến đó và sau đó bạn có thể chạy một vòng lặp o danh sách đó. Sự lựa chọn là của bạn! In a for loop a take input from user 10 times if you want to take inputs in the starting then you can also to that and you can then run a loop o that list. Choice is yours!

Không ai đưa ra mã tôi sẽ viết, điều này không tạo ra một danh sách mà thay vào đó theo dõi câu trả lời trong một vòng lặp. Logic phức tạp hơn một chút, nhưng nó tránh được việc lưu trữ các giá trị [ở đây nó không thực sự quan trọng, vì nó chỉ là 10 giá trị, nhưng một vấn đề tương tự với đầu vào từ một chương trình khác, có thể có nhiều dữ liệu hơn là phù hợp với bộ nhớ].

maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']

Lưu ý rằng tôi đang coi các giá trị khác không là True, vì vậy

maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
0 là một thử nghiệm cho sự kỳ quặc [vì nó cho phần còn lại khác không và Python coi các giá trị khác không là
maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
1]. Tương tự, các thử nghiệm
maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
2 rằng
maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
3 không phải là
maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
4 [cũng không phải là không, nhưng nó không thể bằng không, vì điều đó là chẵn].

Một cách khác, gần hơn với các câu trả lời khác, trong đó nó coi các số là một chuỗi, nhưng vẫn tránh lưu trữ mọi thứ trong bộ nhớ, là sử dụng một trình tạo:

from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']

Điều này là "nâng cao" hơn, nhưng

maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
5 tạo ra một trình tạo và chuỗi chức năng
maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
6 gọi một cách hiệu quả cho mỗi giá trị [đơn giản hóa một chút]. Vì vậy, kết quả cuối cùng là một loại đường ống, một lần nữa, tránh giữ mọi thứ trong bộ nhớ.

[

maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
7 xảy ra khi
maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
8 không nhận được bất kỳ giá trị nào.]

Một cách viết khác sẽ là [lưu ý việc thiếu dấu ngoặc vuông]:

try:
    print['The largest odd value entered was',
          max[filter[odd,
                     map[int, [input['Enter a value: ']
                               for _ in range[10]]]]]]
except ValueError:
    print['No odd values were entered.']

trong đó

maxOdd = None
for _ in range[10]:
    value = int[input['Enter a value: ']]
    if [value % 2 and [maxOdd is None or value > maxOdd]]:
        maxOdd = value
if maxOdd:
    print['The largest odd value entered was', maxOdd]
else:
    print['No odd values were entered.']
9 là một biểu thức máy phát.

Chúng tôi có thể lấy một đầu vào cùng một lúc nhưng tôi muốn lấy hơn 10 số cùng một lúc. Tôi muốn tất cả các số trong một danh sách.

Bạn có thể lấy hơn 1 đầu vào chỉ cần lấy tất cả các số cùng một lúc; Số mới trên một dòng mới. Hãy thử nó như bên dưới hoặc một cái gì đó tương tự i1 = int [input []] i2 = int [input []] in [i1, i2] input => 1 2 output => 1 2

#Nó không hoạt động. Bạn đã cho 100 số tất cả trong dòng tiếp theo khi được yêu cầu đầu vào? A = [] cho i trong phạm vi [2]: x = int [input ["enter no."]] A.Append [x] in [a]

Cảm ơn Krishna. Nó hoạt động. Tôi đã viết mã bằng cách sử dụng đó. //code.sololearn.com/c0efjpeobfax/?ref=app

Số lượng đầu vào có thể thay đổi. Tôi muốn tất cả các số trong danh sách trong danh sách.

Tôi đã thử dưới một nhưng không làm việc tại sao? A = [] cho i trong phạm vi [100]: x = int [input ["enter no."]] A.Append [x]


  • Học thuyết
  • Các bước
  • Các vấn đề

1. Cách đọc và viết trong Python

Mỗi chương trình cuối cùng là bộ xử lý dữ liệu, vì vậy chúng ta nên biết cách nhập và xuất dữ liệu trong đó. Tồn tại một hàm,

from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
0, để xuất dữ liệu từ bất kỳ chương trình Python nào. Để sử dụng nó, hãy truyền một danh sách các đối số được phân tách bằng dấu phẩy mà bạn muốn in vào hàm
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
0. Hãy xem một ví dụ. Nhấn "Chạy" và sau đó "Tiếp theo" để xem chương trình được thực hiện theo từng dòng như thế nào:

print[5 + 10]
print[3 * 7, [17 - 2] * 8]
print[2 ** 16]  # two stars are used for exponentiation [2 to the power of 16]
print[37 / 3]  # single forward slash is a division
print[37 // 3]  # double forward slash is an integer division
        # it returns only the quotient of the division [i.e. no remainder]
print[37 % 3]  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value

Để nhập dữ liệu vào một chương trình, chúng tôi sử dụng

from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
2. Hàm này đọc một dòng văn bản duy nhất, dưới dạng chuỗi.

Đây là một chương trình đọc tên của người dùng và chào họ:

print['What is your name?']
name = input[]  # read a single line and store it in the variable "name"
print['Hi ' + name + '!']
    

Quảng cáo của Google, có thể dựa trên sở thích của bạn

2. Tổng số và chuỗi

Hãy cố gắng viết một chương trình nhập hai số và in tổng của chúng. Chúng tôi đọc hai số và lưu trữ chúng trong các biến

from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
3 và
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
4 bằng toán tử gán
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
5. Ở phía bên trái của toán tử gán, chúng tôi đặt tên của biến. Tên có thể là một chuỗi các ký tự Latin [
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
6,
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
7,
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
8,
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
9] nhưng phải bắt đầu bằng một chữ cái trong phạm vi
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
6 hoặc
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
7. Ở phía bên phải của toán tử gán, chúng tôi đặt bất kỳ biểu thức nào mà Python có thể đánh giá. Tên bắt đầu chỉ vào kết quả đánh giá. Đọc ví dụ này, chạy nó và xem đầu ra:must start with a letter in the range
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
6 or
from itertools import islice

def numbers[]:
    while True:
        yield input['Enter a value: ']

def odd[n]:
    return n % 2

try:
    print['The largest odd value entered was',
          max[filter[odd, map[int, islice[numbers[], 10]]]]]
except ValueError:
    print['No odd values were entered.']
7. On the right side of an assignment operator we put any expression that Python can evaluate. The name starts pointing to the result of the evaluation. Read this example, run it and look at the output:

a = input[]
b = input[]
s = a + b
print[s]
    

Sau khi chạy ví dụ, chúng ta có thể thấy rằng nó in

try:
    print['The largest odd value entered was',
          max[filter[odd,
                     map[int, [input['Enter a value: ']
                               for _ in range[10]]]]]]
except ValueError:
    print['No odd values were entered.']
2. Như chúng tôi đã được dạy ở trường,
try:
    print['The largest odd value entered was',
          max[filter[odd,
                     map[int, [input['Enter a value: ']
                               for _ in range[10]]]]]]
except ValueError:
    print['No odd values were entered.']
3 cho
try:
    print['The largest odd value entered was',
          max[filter[odd,
                     map[int, [input['Enter a value: ']
                               for _ in range[10]]]]]]
except ValueError:
    print['No odd values were entered.']
4. Vì vậy, chương trình là sai, và điều quan trọng là phải hiểu tại sao. Vấn đề là, trong dòng thứ ba
try:
    print['The largest odd value entered was',
          max[filter[odd,
                     map[int, [input['Enter a value: ']
                               for _ in range[10]]]]]]
except ValueError:
    print['No odd values were entered.']
5 Python đã "tổng hợp" hai chuỗi, thay vì hai số. Tổng của hai chuỗi trong Python hoạt động như sau: chúng chỉ được dán mắt từng người khác. Nó cũng đôi khi được gọi là "Concatenation chuỗi".

Bạn có thấy trong bộ kiểm tra biến, ở phía bên phải, các giá trị bị ràng buộc với các biến ____ 23 & nbsp; và & nbsp; ____ ____ 24 được bọc trong các trích dẫn? Điều đó có nghĩa là các giá trị có chuỗi, không phải số. Chuỗi và số được thể hiện bằng Python khác nhau.

Tất cả các giá trị trong Python được gọi là "đối tượng". Mỗi đối tượng có một loại nhất định. Số 2 tương ứng với một đối tượng "số 2" của loại "int" [tức là, số nguyên]. Chuỗi

try:
    print['The largest odd value entered was',
          max[filter[odd,
                     map[int, [input['Enter a value: ']
                               for _ in range[10]]]]]]
except ValueError:
    print['No odd values were entered.']
8 tương ứng với một đối tượng "chuỗi 'Hello'" của loại "str". Mỗi số điểm nổi được biểu diễn dưới dạng một đối tượng của loại "float". Loại đối tượng chỉ định loại hoạt động nào có thể được áp dụng cho nó. Chẳng hạn, nếu hai biến
try:
    print['The largest odd value entered was',
          max[filter[odd,
                     map[int, [input['Enter a value: ']
                               for _ in range[10]]]]]]
except ValueError:
    print['No odd values were entered.']
9 và
print[5 + 10]
print[3 * 7, [17 - 2] * 8]
print[2 ** 16]  # two stars are used for exponentiation [2 to the power of 16]
print[37 / 3]  # single forward slash is a division
print[37 // 3]  # double forward slash is an integer division
        # it returns only the quotient of the division [i.e. no remainder]
print[37 % 3]  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
0 đang chỉ vào các đối tượng của loại
print[5 + 10]
print[3 * 7, [17 - 2] * 8]
print[2 ** 16]  # two stars are used for exponentiation [2 to the power of 16]
print[37 / 3]  # single forward slash is a division
print[37 // 3]  # double forward slash is an integer division
        # it returns only the quotient of the division [i.e. no remainder]
print[37 % 3]  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
1, Python có thể nhân chúng. Tuy nhiên, nếu chúng chỉ vào các đối tượng của loại
print[5 + 10]
print[3 * 7, [17 - 2] * 8]
print[2 ** 16]  # two stars are used for exponentiation [2 to the power of 16]
print[37 / 3]  # single forward slash is a division
print[37 // 3]  # double forward slash is an integer division
        # it returns only the quotient of the division [i.e. no remainder]
print[37 % 3]  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
2, Python không thể làm điều đó:

first = 5
second = 7
print[first * second]

# you can use single or double quotes to define a string
first = '5'
second = "7"
print[first * second]
    

Để đúc [chuyển đổi] chuỗi các chữ số thành số nguyên, chúng ta có thể sử dụng hàm & nbsp; ________ 43. Ví dụ: & nbsp; ________ 44 cung cấp cho một đối tượng int & nbsp; với giá trị

print[5 + 10]
print[3 * 7, [17 - 2] * 8]
print[2 ** 16]  # two stars are used for exponentiation [2 to the power of 16]
print[37 / 3]  # single forward slash is a division
print[37 // 3]  # double forward slash is an integer division
        # it returns only the quotient of the division [i.e. no remainder]
print[37 % 3]  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
5.

Đưa ra thông tin ở trên, giờ đây chúng ta có thể sửa chữa đầu ra không chính xác và xuất tổng số của hai số một cách chính xác:

a = int[input[]]
b = int[input[]]
s = a + b
print[s]
    

Quảng cáo của Google, có thể dựa trên sở thích của bạn

Làm thế nào để bạn nhập 10 giá trị trong Python?

Nhận danh sách các số làm đầu vào từ người dùng..
Sử dụng hàm đầu vào []. Sử dụng hàm input [] để chấp nhận các phần tử danh sách từ người dùng ở định dạng của chuỗi được phân tách bằng không gian ..
Sử dụng chức năng chia [] của lớp chuỗi. ....
Sử dụng cho chức năng Loop và Range [] để lặp lại danh sách người dùng. ....
Chuyển đổi từng phần tử của danh sách thành số ..

Làm thế nào để bạn lấy các số 10 chữ số trong Python?

Loại: Phương thức_Descriptor Mẫu chuỗi: Không gian tên: Python xây dựng DocString: S. isDigit [] ->Lưu câu trả lời này.

Làm thế nào để bạn nhập số trong Python?

Như chúng ta biết rằng hàm đầu vào tích hợp [] của Python luôn trả về đối tượng lớp STR [Chuỗi].Vì vậy, để lấy đầu vào số nguyên, chúng ta phải gõ các đầu vào đó vào số nguyên bằng cách sử dụng hàm int int [] tích hợp python.type cast those inputs into integers by using Python built-in int[] function.

Làm thế nào để bạn nhập 3 số vào Python?

# Chương trình Python để nhập 3 số và hiển thị số lớn nhất..
num2 = int [input ["nhập số thứ hai"]] num3 = int [input ["nhập số thứ ba"]] ....
if [num1> num2 và num1> num3]: in ["số lớn nhất là", num1] ....
Elif [num2> num1 và num2> num3]: in ["số lớn nhất là", num2].

Bài Viết Liên Quan

Chủ Đề