Ở đây, chúng tôi đã sử dụng câu lệnh
Enter a number: 2 Positive number8. Chúng ta có thể làm điều tương tự bằng cách sử dụng các câu lệnh
Enter a number: 2 Positive number9 lồng nhau như sau
Mã nguồn. Sử dụng Nested nếu
num = float[input["Enter a number: "]]
if num >= 0:
if num == 0:
print["Zero"]
else:
print["Positive number"]
else:
print["Negative number"]
Đầu ra của cả hai chương trình sẽ giống nhau
Đầu ra 1
Enter a number: 2 Positive number
Đầu ra 2
Enter a number: 0 Zero
Một số dương nếu nó lớn hơn 0. Chúng tôi kiểm tra điều này trong biểu thức của
Enter a number: 2 Positive number9. Nếu là
Enter a number: 0 Zero1, số đó sẽ bằng 0 hoặc âm. Điều này cũng được kiểm tra trong biểu thức tiếp theo
Tôi là một sinh viên trong một khái niệm về lớp lập trình. Phòng thí nghiệm được điều hành bởi một TA và hôm nay trong phòng thí nghiệm, anh ấy đã chọn cho chúng tôi một chương trình nhỏ đơn giản thực sự để xây dựng. Đó là một nơi mà nó sẽ nhân lên bằng cách bổ sung. Dù sao, anh ấy đã sử dụng chúng tôi là đối tượng tuyệt vời để tránh phá vỡ chương trình với những tiêu cực. Tôi đã đánh nó thật nhanh và sau đó tranh luận với anh ta trong 10 phút rằng đó là toán học tồi. That is, 4 * -5 not by 20, it by -20. Anh ấy nói rằng anh ấy thực sự không quan tâm đến điều đó và dù sao thì anh ấy cũng sẽ quá khó để làm cho Prog xử lý những tiêu cực. Vì vậy, câu hỏi của tôi là làm thế nào để tôi đi về điều này
Nội dung chính Hiển thị
- Làm thế nào để bạn viết một số âm trong Python?
- Làm thế nào để bạn nhập một danh sách tiêu cực vào Python?
- Làm thế nào để Python lưu trữ số lượng âm thanh?
- Số âm có thể là số nguyên trong Python không?
Đây là chương trình tôi đã chuyển vào
#get user input of numbers as variables
numa, numb = input["please give 2 numbers to multiply seperated with a comma:"]
#standing variables
total = 0
count = 0
#output the total
while [count< abs[numb]]:
total = total + numa
count = count + 1
#testing statements
if [numa, numb 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
or could
a * b
Đã trả ngày 16 tháng 3 năm 2010 lúc 2. 52 16/03/2010 lúc 2. 52
Mike Grahammike Graham Mike Graham
71. 3K14 Huy hiệu vàng98 Huy hiệu bạc129 Huy hiệu đồng14 huy hiệu vàng98 huy hiệu bạc129 huy hiệu đồng
1
Quá khó? . tốt, cụm từ có thể sẽ khiến tôi bị cấm. Dù sao, hãy kiểm tra xem
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
0 has no sound. Nếu nó được nhântext = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
1 vớitext = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
2 và làmtext = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
3. Sau đó làm vòng lặpĐã trả ngày 16 tháng 3 năm 2010 lúc 2. 50 16/03/2010 lúc 2. 50
2
ABS [] trong điều kiện là cần thiết, bởi vì, tốt, nó kiểm soát số lần lặp [làm thế nào bạn sẽ xác định số lần lặp âm?]. Bạn có thể sửa nó bằng cách đảo ngược dấu hiệu của kết quả nếu
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
0 is soundVì vậy, đây là phiên bản sửa đổi mã của bạn. Lưu ý rằng tôi đã thay thế vòng lặp trong khi vòng lặp sạch hơn cho vòng lặp
Enter a number: 2 Positive number1
Đã trả lời ngày 16 tháng 3 năm 2010 lúc 3. 03 16/03/2010 lúc 3. 03
Người lười biếng người lười biếng
2. 13211 Huy hiệu bạc10 Huy hiệu đồng11 huy hiệu bạc10 huy hiệu đồng
Please try this thing on TA of you
Enter a number: 2 Positive number2
đầu ra
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
0Đã trả lời ngày 16 tháng 3 năm 2010 lúc 4. 13 16/03/2010 lúc 4. 13
John Machinjohn Machin John Machin
79,7K11 Huy hiệu vàng138 Huy hiệu bạc184 Huy hiệu đồng11 huy hiệu vàng138 huy hiệu bạc184 bronze badges
3
Làm thế nào về một cái gì đó như vậy?.
Ghi chú.
- Hàm abs [] chỉ được sử dụng cho thủ thuật tối ưu hóa. Đoạn này có thể bị loại bỏ hoặc mã hóa lại
- Logic kém hiệu quả hơn vì chúng tôi đang thử nghiệm dấu hiệu A và B với mỗi lần lặp [giá phải trả để tránh cả toán tử ABS [] và phép nhân]
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
1Đã trả lời ngày 16 tháng 3 năm 2010 lúc 3. 13 16/03/2010 lúc 3. 13
MJVMJV mjv
71. 4K14 Huy hiệu vàng111 Huy hiệu bạc156 Huy hiệu đồng14 Huy hiệu vàng111 Huy hiệu bạc156 Huy hiệu đồng
4
Cảm ơn tất cả mọi người, tất cả các bạn đã giúp tôi học hỏi rất nhiều. Đây là những gì tôi đã đưa ra bằng cách sử dụng một số đề xuất của bạn
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
2Cảm ơn mọi người vì tất cả sự giúp đỡ
Đã trả lời ngày 16 tháng 3 năm 2010 lúc 23. 42 16/03/2010 lúc 23. 42
dman762000dman762000 dman762000
Huy hiệu vàng 18111 huy hiệu vàng1 huy hiệu bạc6 huy hiệu đồng
5
Please try doing this thing
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
3Đã trả ngày 2 tháng 12 năm 2021 lúc 2. 46 2 tháng 12 năm 2021 lúc 2. 46
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
4Đã trả ngày 10 tháng 11 năm 2014 lúc 20. 34 10/11/2014 lúc 20. 34
Kitiyokitiyo Kitiyo
11 Huy hiệu vàng2 Huy hiệu bạc3 Huy hiệu đồng1 huy hiệu vàng2 huy hiệu bạc3 huy hiệu đồng
1
Làm thế nào để bạn viết một số âm trong Python?
Trong Python, các số dương có thể được thay đổi thành số âm với sự trợ giúp của phương thức được xây dựng được cung cấp trong thư viện Python có tên ABS []. Khi ABS [] được sử dụng, nó chuyển đổi âm lượng thành dương. Tuy nhiên, khi -abs [] được sử dụng, thì một số dương có thể được thay đổi thành số âm. abs [] . Khi abs[] được sử dụng, nó sẽ chuyển đổi số âm thành số dương. Tuy nhiên, khi -abs[] được sử dụng, thì số dương có thể được đổi thành số âm.
Làm thế nào để bạn nhập một danh sách tiêu cực vào Python?
int[] can convert a string to a integer. Bản đồ [int,. ] trả về một số lượng có thể áp dụng int [] cho mỗi "từ" của người dùng đầu vào. Cuộc gọi cuối cùng đến danh sách [] sẽ biến các số lượng khác nhau thành một danh sách. Điều này cũng nên xử lý các số âm là tốt. map[int,. ] trả về một lần lặp áp dụng int[] cho mỗi "từ" của đầu vào của người dùng. Lệnh gọi cuối cùng tới list[] sẽ biến iterable thành list . Điều này cũng sẽ xử lý các số âm.
Làm thế nào để Python lưu trữ số lượng âm thanh?
Các số âm được viết với một số hàng đầu thay vì số 0 hàng đầu. Vì vậy, nếu bạn chỉ sử dụng 8 bit cho hai số bổ sung của mình, thì bạn sẽ xử lý các mẫu từ "00000000" đến "01111111" như toàn bộ số từ 0 đến 127 và dự trữ "1xxxxxxxx" để viết